Re: Question about extents and transformations

[email protected] Thu, 12 Dec 2019 20:38:43 +0000 (UTC)
Newsgroups gmane.comp.lib.cairo
Message-ID <[email protected]>
--===============0606162707==
Content-Type: multipart/alternative; 
	boundary="----=_Part_7585081_1986073936.1576183123320"

------=_Part_7585081_1986073936.1576183123320
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

=20
A path in user space can change it's bounding box size with a transform. A =
scale or rotation can change the size of a bounding box.

With your code, test the output by drawing it. That is one of the cool thin=
gs about drawing. If you are working on a tough drawing you can often see m=
istakes that you might miss otherwise.

Eric

//gcc -Wall bounding_box1.c -o bounding_box1 `pkg-config --cflags --libs ca=
iro` -lm

#include<cairo.h>
#include<stdio.h>
#include<math.h>

int main()
=C2=A0 {
=C2=A0=C2=A0=C2=A0 double width=3D500.0;
=C2=A0=C2=A0=C2=A0 double height=3D500.0;
=C2=A0=C2=A0=C2=A0 double x1=3D0.0;
=C2=A0=C2=A0=C2=A0 double y1=3D0.0;
=C2=A0=C2=A0=C2=A0 double x2=3D0.0;
=C2=A0=C2=A0=C2=A0 double y2=3D0.0;
=C2=A0=C2=A0=C2=A0 cairo_surface_t *surface=3Dcairo_image_surface_create(CA=
IRO_FORMAT_ARGB32, width, height);
=C2=A0=C2=A0=C2=A0 cairo_t *cr=3Dcairo_create(surface);
=C2=A0=C2=A0=20
=C2=A0=C2=A0=C2=A0 //Paint the background.
=C2=A0=C2=A0=C2=A0 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
=C2=A0=C2=A0=C2=A0 cairo_paint(cr);

=C2=A0=C2=A0=C2=A0 //Translate shape.
=C2=A0=C2=A0=C2=A0 cairo_save(cr);
=C2=A0=C2=A0=C2=A0 cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 1.0);
=C2=A0=C2=A0=C2=A0 cairo_translate(cr, width/4.0, height/4.0);
=C2=A0=C2=A0=C2=A0 cairo_move_to(cr, 100.0, 0.0);
=C2=A0=C2=A0=C2=A0 cairo_curve_to(cr, 50.0, -50.0, -50.0, -50.0, -100.0, 0.=
0);
=C2=A0=C2=A0=C2=A0 cairo_curve_to(cr, -50.0, 50.0, 50.0, 50.0, 100.0, 0.0);
=C2=A0=C2=A0=C2=A0 cairo_path_extents(cr, &x1, &y1, &x2, &y2);
=C2=A0=C2=A0=C2=A0 cairo_fill(cr);
=C2=A0=C2=A0=C2=A0 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
=C2=A0=C2=A0=C2=A0 cairo_rectangle(cr, x1, y1, x2-x1, y2-y1);
=C2=A0=C2=A0=C2=A0 cairo_stroke(cr);
=C2=A0=C2=A0=C2=A0 cairo_restore(cr);

=C2=A0=C2=A0=C2=A0 //Translate and scale shape.
=C2=A0=C2=A0=C2=A0 cairo_save(cr);
=C2=A0=C2=A0=C2=A0 cairo_set_source_rgba(cr, 0.0, 1.0, 0.0, 1.0);
=C2=A0=C2=A0=C2=A0 cairo_translate(cr, 3.0*width/4.0, height/4.0);
=C2=A0=C2=A0=C2=A0 cairo_scale(cr, 0.75, 0.75);
=C2=A0=C2=A0=C2=A0 cairo_move_to(cr, 100.0, 0.0);
=C2=A0=C2=A0=C2=A0 cairo_curve_to(cr, 50.0, -50.0, -50.0, -50.0, -100.0, 0.=
0);
=C2=A0=C2=A0=C2=A0 cairo_curve_to(cr, -50.0, 50.0, 50.0, 50.0, 100.0, 0.0);
=C2=A0=C2=A0=C2=A0 cairo_path_extents(cr, &x1, &y1, &x2, &y2);
=C2=A0=C2=A0=C2=A0 cairo_fill(cr);
=C2=A0=C2=A0=C2=A0 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
=C2=A0=C2=A0=C2=A0 cairo_rectangle(cr, x1, y1, x2-x1, y2-y1);
=C2=A0=C2=A0=C2=A0 cairo_stroke(cr);
=C2=A0=C2=A0=C2=A0 cairo_restore(cr);

=C2=A0=C2=A0=C2=A0 //Translate and rotate shape.
=C2=A0=C2=A0=C2=A0 cairo_save(cr);
=C2=A0=C2=A0=C2=A0 cairo_set_source_rgba(cr, 0.0, 0.0, 1.0, 1.0);
=C2=A0=C2=A0=C2=A0 cairo_translate(cr, width/4.0, 3.0*height/4.0);
=C2=A0=C2=A0=C2=A0 cairo_rotate(cr, M_PI/2.0);
=C2=A0=C2=A0=C2=A0 cairo_move_to(cr, 100.0, 0.0);
=C2=A0=C2=A0=C2=A0 cairo_curve_to(cr, 50.0, -50.0, -50.0, -50.0, -100.0, 0.=
0);
=C2=A0=C2=A0=C2=A0 cairo_curve_to(cr, -50.0, 50.0, 50.0, 50.0, 100.0, 0.0);
=C2=A0=C2=A0=C2=A0 cairo_path_extents(cr, &x1, &y1, &x2, &y2);
=C2=A0=C2=A0=C2=A0 cairo_fill(cr);
=C2=A0=C2=A0=C2=A0 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
=C2=A0=C2=A0=C2=A0 cairo_rectangle(cr, x1, y1, x2-x1, y2-y1);
=C2=A0=C2=A0=C2=A0 cairo_stroke(cr);
=C2=A0=C2=A0=C2=A0 cairo_restore(cr);
=C2=A0=C2=A0=C2=A0=20
=C2=A0=C2=A0=C2=A0 printf("Output to bounding_box.png\n");
=C2=A0=C2=A0=C2=A0 cairo_surface_write_to_png(surface, "bounding_box.png");

=C2=A0=C2=A0=C2=A0 cairo_destroy(cr);
=C2=A0=C2=A0=C2=A0 cairo_surface_destroy(surface);
=C2=A0
=C2=A0=C2=A0=C2=A0 return 0;
=C2=A0 }
=20


------=_Part_7585081_1986073936.1576183123320
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 7bit


<div style="color:black;font: 10pt arial;">
<div> <font size="2"><br>
A path in user space can change it's bounding box size with a transform. A scale or rotation can change the size of a bounding box.<br>
<br>
With your code, test the output by drawing it. That is one of the cool things about drawing. If you are working on a tough drawing you can often see mistakes that you might miss otherwise.<br>
<br>
Eric</font></div>

<div><font size="2"><br>
</font></div>

<div><font size="2"><br>
//gcc -Wall bounding_box1.c -o bounding_box1 `pkg-config --cflags --libs cairo` -lm<br>
<br>
#include&lt;cairo.h&gt;<br>
#include&lt;stdio.h&gt;<br>
#include&lt;math.h&gt;<br>
<br>
int main()<br>
&nbsp; {<br>
&nbsp;&nbsp;&nbsp; double width=500.0;<br>
&nbsp;&nbsp;&nbsp; double height=500.0;<br>
&nbsp;&nbsp;&nbsp; double x1=0.0;<br>
&nbsp;&nbsp;&nbsp; double y1=0.0;<br>
&nbsp;&nbsp;&nbsp; double x2=0.0;<br>
&nbsp;&nbsp;&nbsp; double y2=0.0;<br>
&nbsp;&nbsp;&nbsp; cairo_surface_t *surface=cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);<br>
&nbsp;&nbsp;&nbsp; cairo_t *cr=cairo_create(surface);<br>
&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; //Paint the background.<br>
&nbsp;&nbsp;&nbsp; cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);<br>
&nbsp;&nbsp;&nbsp; cairo_paint(cr);<br>
<br>
&nbsp;&nbsp;&nbsp; //Translate shape.<br>
&nbsp;&nbsp;&nbsp; cairo_save(cr);<br>
&nbsp;&nbsp;&nbsp; cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 1.0);<br>
&nbsp;&nbsp;&nbsp; cairo_translate(cr, width/4.0, height/4.0);<br>
&nbsp;&nbsp;&nbsp; cairo_move_to(cr, 100.0, 0.0);<br>
&nbsp;&nbsp;&nbsp; cairo_curve_to(cr, 50.0, -50.0, -50.0, -50.0, -100.0, 0.0);<br>
&nbsp;&nbsp;&nbsp; cairo_curve_to(cr, -50.0, 50.0, 50.0, 50.0, 100.0, 0.0);<br>
&nbsp;&nbsp;&nbsp; cairo_path_extents(cr, &amp;x1, &amp;y1, &amp;x2, &amp;y2);<br>
&nbsp;&nbsp;&nbsp; cairo_fill(cr);<br>
&nbsp;&nbsp;&nbsp; cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);<br>
&nbsp;&nbsp;&nbsp; cairo_rectangle(cr, x1, y1, x2-x1, y2-y1);<br>
&nbsp;&nbsp;&nbsp; cairo_stroke(cr);<br>
&nbsp;&nbsp;&nbsp; cairo_restore(cr);<br>
<br>
&nbsp;&nbsp;&nbsp; //Translate and scale shape.<br>
&nbsp;&nbsp;&nbsp; cairo_save(cr);<br>
&nbsp;&nbsp;&nbsp; cairo_set_source_rgba(cr, 0.0, 1.0, 0.0, 1.0);<br>
&nbsp;&nbsp;&nbsp; cairo_translate(cr, 3.0*width/4.0, height/4.0);<br>
&nbsp;&nbsp;&nbsp; cairo_scale(cr, 0.75, 0.75);<br>
&nbsp;&nbsp;&nbsp; cairo_move_to(cr, 100.0, 0.0);<br>
&nbsp;&nbsp;&nbsp; cairo_curve_to(cr, 50.0, -50.0, -50.0, -50.0, -100.0, 0.0);<br>
&nbsp;&nbsp;&nbsp; cairo_curve_to(cr, -50.0, 50.0, 50.0, 50.0, 100.0, 0.0);<br>
&nbsp;&nbsp;&nbsp; cairo_path_extents(cr, &amp;x1, &amp;y1, &amp;x2, &amp;y2);<br>
&nbsp;&nbsp;&nbsp; cairo_fill(cr);<br>
&nbsp;&nbsp;&nbsp; cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);<br>
&nbsp;&nbsp;&nbsp; cairo_rectangle(cr, x1, y1, x2-x1, y2-y1);<br>
&nbsp;&nbsp;&nbsp; cairo_stroke(cr);<br>
&nbsp;&nbsp;&nbsp; cairo_restore(cr);<br>
<br>
&nbsp;&nbsp;&nbsp; //Translate and rotate shape.<br>
&nbsp;&nbsp;&nbsp; cairo_save(cr);<br>
&nbsp;&nbsp;&nbsp; cairo_set_source_rgba(cr, 0.0, 0.0, 1.0, 1.0);<br>
&nbsp;&nbsp;&nbsp; cairo_translate(cr, width/4.0, 3.0*height/4.0);<br>
&nbsp;&nbsp;&nbsp; cairo_rotate(cr, M_PI/2.0);<br>
&nbsp;&nbsp;&nbsp; cairo_move_to(cr, 100.0, 0.0);<br>
&nbsp;&nbsp;&nbsp; cairo_curve_to(cr, 50.0, -50.0, -50.0, -50.0, -100.0, 0.0);<br>
&nbsp;&nbsp;&nbsp; cairo_curve_to(cr, -50.0, 50.0, 50.0, 50.0, 100.0, 0.0);<br>
&nbsp;&nbsp;&nbsp; cairo_path_extents(cr, &amp;x1, &amp;y1, &amp;x2, &amp;y2);<br>
&nbsp;&nbsp;&nbsp; cairo_fill(cr);<br>
&nbsp;&nbsp;&nbsp; cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);<br>
&nbsp;&nbsp;&nbsp; cairo_rectangle(cr, x1, y1, x2-x1, y2-y1);<br>
&nbsp;&nbsp;&nbsp; cairo_stroke(cr);<br>
&nbsp;&nbsp;&nbsp; cairo_restore(cr);<br>
&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; printf("Output to bounding_box.png\n");<br>
&nbsp;&nbsp;&nbsp; cairo_surface_write_to_png(surface, "bounding_box.png");<br>
<br>
&nbsp;&nbsp;&nbsp; cairo_destroy(cr);<br>
&nbsp;&nbsp;&nbsp; cairo_surface_destroy(surface);<br>
&nbsp;<br>
&nbsp;&nbsp;&nbsp; return 0;<br>
&nbsp; }</font><br>
</div>

<div> <br>
</div>
<br>
</div>

------=_Part_7585081_1986073936.1576183123320--

--===============0606162707==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

-- 
cairo mailing list
[email protected]
https://lists.cairographics.org/mailman/listinfo/cairo

--===============0606162707==--