[cairo] translating from coordinates to user space

Bill Spitzak spitzak at thefoundry.co.uk
Tue Mar 10 16:14:17 PDT 2009


Matt Bartolome wrote:
> Hi,
> I'm new to cairo and had a couple questions about scale. What I would
> like to do is draw my image in the data's geographic coordinate system
> then translate it to pixel space. Lets say the extent of the data is
> 2000,1000, 4000, 2000 (xmin,ymin,xmax,ymax). Once I have finished
> drawing all the vectors in that coordinate system I would like to just
> translate it to a 0,0 origin with appropriate x and y max so it fits
> on the image (using svgsurface). Or, maybe even scale the drawing to a
> predefined width like 1200px 400px and distort the x and y to fit the
> space. I've been looking at the scale and translate functions but I
> don't quite get it.
> 
> Does anybody have an example of this type of thing? Any advice would
> be appreciated!

I'm guessing you don't want to distort the image, and that instead you 
want to center it in the output.

If the output rectangle has a size width,height:

   // save the transform before messing with it:
   cairo_save(cr);

   // Find the center of your data:
   center_x = (xmax+xmin)/2;
   center_y = (ymax+ymin)/2;

   // Translate that to 0,0:
   cairo_translate(cr, -center_x, -center_y);

   // find the scaling factor:
   scale_x = width/(xmax-xmin);
   scale_y = height/(ymax-ymin);
   scale = min(scale_x, scale_y);

   // scale the image to fit:
   cairo_scale(cr, scale, scale);

   // put it back in center of output rectangle:
   cairo_translate(cr, width/2, height/2);

   // now draw all your diagram using the original coordiantes:
   draw_the_map(cr);

   // put the transform back
   cairo_restore(cr);

-- 
Bill Spitzak, Senior Software Engineer
The Foundry, 618 Hampton Drive, Venice, CA, 90291, USA
Tel: +1 310 399-4555 * Fax: +1 310 450-4516 * Web: www.thefoundry.co.uk
The Foundry Visionmongers Ltd * Registered in England and Wales No: 4642027


More information about the cairo mailing list