[cairo] Extents under rotation

Donn donn.ingle at gmail.com
Sun Dec 8 07:19:25 PST 2013


On 07/12/2013 20:22, Uli Schlachter wrote:
> Uhm. So the above code should do just this, right?
Uli,thanks for your input again.

I put together this code (in Vala) using your idea:

--

// valac --pkg cairo test_extents_under_rotation.vala -o test
// View the img.png file after running "test"

using Cairo;
void main(string[] args) {

   double x1;
   double x2;
   double y1;
   double y2;

	Cairo.ImageSurface surface = new Cairo.ImageSurface 
(Cairo.Format.ARGB32, 500, 500);
	Cairo.Context cr = new Cairo.Context (surface);

   //centre the axes - 0,0 in middle
   var matrix = Cairo.Matrix (1, 0, 0, 1, 250,250 );
   matrix.scale (1, 1);
	cr.transform(matrix);


     cr.arc (0.0, 0.0, 30.0, 0, 2*Math.PI);
     cr.set_source_rgb(1,0,0);
     cr.fill_preserve();

     cr.path_extents (out x1, out y1, out x2, out y2);
     stdout.printf("Extents: %G|%G|%G|%G\n", x1,y1,x2,y2);
     //Draw the bbox as calculated by extents
     cr.rectangle(x1,y1,(x2-x1),(y2-y1));
     cr.set_source_rgb(0,0,0);
     cr.stroke();

     //Now, rotate that circle 45 degrees (and move it over a bit)
     cr.translate(90,0);
     cr.rotate((Math.PI/180.0)*45); //45 degree rot

       cr.arc (0.0, 0.0, 30.0, 0, 2*Math.PI);
       cr.set_source_rgb(1,0,0);
       cr.fill_preserve();

       cr.path_extents (out x1, out y1, out x2, out y2);
       stdout.printf("Extents: %G|%G|%G|%G\n", x1,y1,x2,y2);
       //Draw the bbox as calculated by extents -- this is wrong.
       //Why is the rect so much bigger?
       //Is there any way to get the proper user-space coords?
       cr.rectangle(x1,y1,(x2-x1),(y2-y1));
       cr.set_source_rgb(0,0,0);
       cr.stroke();


     //Test Uli's idea
     cr.translate(0,120);
     cr.rotate((Math.PI/180.0)*67);
     //Unusual rot. 45 degrees sometimes disguises things.

           cr.arc (0.0, 0.0, 30.0, 0, 2*Math.PI);
           cr.set_source_rgb(1,1,0);
           cr.fill_preserve();

           cr.save();
           cr.identity_matrix();
           cr.path_extents(out x1, out y1, out x2, out y2);

             //Draw this device rect in green
             cr.new_path();
             cr.rectangle(x1,y1,(x2-x1),(y2-y1));
             cr.set_source_rgb(0,1,0);
             cr.set_line_width(3);
             cr.stroke();

           cr.restore();

           //Go from device into user
           cr.device_to_user(ref x1, ref y1);
           cr.device_to_user(ref x2, ref y2);

           //Draw this user rect in white.
           cr.new_path();
           cr.move_to(x1,y1);
           cr.line_to(x2,y1);
           cr.line_to(x2,y2);
           cr.line_to(x1,y2);
           cr.close_path();

           cr.set_source_rgb(1,1,1);
           cr.stroke();


	surface.write_to_png ("img.png");

}
--

Attached is the image too. The yellow circle is the one to look at.
1. It's rotated at 67 degrees (45 degrees is tricksy.)
2. The green outline is the device-space.
3. The white is the user-space. Why it's all squashed is beyond me.

Any clues?

\d
-------------- next part --------------
A non-text attachment was scrubbed...
Name: img.png
Type: image/png
Size: 8205 bytes
Desc: not available
URL: <http://lists.cairographics.org/archives/cairo/attachments/20131208/89427ef4/attachment.png>


More information about the cairo mailing list