[cairo] Cannot get transformations to work

ashley at ashleymills.com ashley at ashleymills.com
Tue Oct 20 05:31:55 PDT 2009


Hi, I'm using cairo, compiled on Windows under Visual Studio Express 2008.
Pretty much everything works, and its possible this could be a bug due to
my compilation. I think its more likely I'm just doing something dumb.

I can't seem to get any transformations to work, here is my code:

	// create 24bit RGB drawing surface of the appropriate size
	cairo_surface_t *cairoSurface =
		cairo_image_surface_create(CAIRO_FORMAT_RGB24, 500, 500);
	cairo_t *cairoState = cairo_create(cairoSurface);

	// set its background to white
	cairo_set_source_rgb(cairoState,1, 1, 1);
	cairo_rectangle(cairoState,0,0,500,500);
	cairo_fill(cairoState);

	// define arrow object
	double arrowHeadAngle = 2*M_PI/360*45;
	double arrowLength = 50;
	cairo_t *arrowState = cairo_create(cairoSurface);
	cairo_set_line_width (arrowState, 1);
	// define arrow body
	cairo_new_path(arrowState);
	double arrowStartX = 0;
	double arrowStartY = 0;
	cairo_move_to(arrowState,arrowStartX,arrowStartY);
	double arrowEndX = arrowStartX + arrowLength;
	double arrowEndY = arrowStartY;
	cairo_line_to(arrowState,arrowEndX,arrowEndY);
	// define arrow head left
	cairo_arc(arrowState,arrowEndX,arrowEndY,arrowLength*0.2,M_PI-arrowHeadAngle,M_PI);
	cairo_line_to(arrowState,arrowEndX,arrowEndY);
	// define arrow head right
	cairo_arc(arrowState,arrowEndX,arrowEndY,arrowLength*0.2,M_PI,M_PI+arrowHeadAngle);
	cairo_line_to(arrowState,arrowEndX,arrowEndY);

	// draw arrow
	cairo_matrix_t* cairoMatrix = new cairo_matrix_t;
	cairo_matrix_init_identity(cairoMatrix);
	cairo_set_matrix(arrowState,cairoMatrix);
	cairo_set_source_rgb(arrowState,0,1,0);
	cairo_stroke_preserve(arrowState);

	// draw rotated and translated version of arrow
	cairo_translate(arrowState,100,100);
	cairo_rotate(arrowState,M_PI/2);
	cairo_set_source_rgb(arrowState,1,0,0);
	cairo_stroke_preserve(arrowState);

	// cleanup the cairo stuff
	cairo_surface_write_to_png (cairoSurface, "test.png");
    	cairo_surface_destroy(cairoSurface);
	cairo_destroy(cairoState);
	cairo_destroy(arrowState);

No matter what I do, I cannot seem to get the arrow to be drawn anywhere
else but the origin without rotation. The calls to translate and rotate
appear to do nothing. I checked its error status and all calls return
success. The arrow is red, so the second call to cairo_stroke_preserve is
definitely writing to the surface.

Am I doing something obviously incorrect here in my usage?

Thanks

Ashley Mills

P.S. Great library :)





More information about the cairo mailing list