[cairo] Rotation around a point

Carl Worth cworth at cworth.org
Mon Nov 26 16:16:21 PST 2007


On Sat, 24 Nov 2007 21:55:51 +0200, Donn wrote:
> > cairo.Matrix.translate( ThingMatrix, x, y ) somewhere, it should be rotated
> > around this point.
> This is the one I don't grok. Won't this just move it again, to some other
> x.y?

I think there was just a typo in the example (a couple of missing
minus signs). Let me try.

First, I'm going to avoid a separate matrix object[*], since that's
usually more of a pain to use than just manipulating the cairo
transform directly. Second, I'm going to write in C---hopefully that's
not a problem for you.

If you've got some function draw_shape(cr) that draws a shape, (or
sets a shape as the current path or whatever), and you want to rotate
that shape by a given angle about the point x,y then do:

	cairo_translate (cr, x, y);
	cairo_rotate (cr, angle);
	cairo_translate (cr, -x, -y);

	draw_shape (cr);

That's the basic recipe for "rotate about a point". Just translate to
put the origin at the point, do the rotation, then translate back.

-Carl

[*] PS. I'd recommend you also manipulate the cairo transformation
directly rather than initializing a matrix, using the matrix
transformation functions, and then calling cairo_transform. It really
is much simpler to just call cairo_translate, cairo_rotate, and
cairo_scale.

As a tip---if you're wanting to do separate translate, rotate, and
scale operations its often quite best to call them in exactly that
order: translate, rotate, scale. This avoids you having to account for
the scale value when passing the translate values, etc.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://lists.cairographics.org/archives/cairo/attachments/20071126/a63ad1df/attachment.pgp 


More information about the cairo mailing list