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<cairo.h><br>
#include<stdio.h><br>
#include<math.h><br>
<br>
int main()<br>
{<br>
double width=500.0;<br>
double height=500.0;<br>
double x1=0.0;<br>
double y1=0.0;<br>
double x2=0.0;<br>
double y2=0.0;<br>
cairo_surface_t *surface=cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);<br>
cairo_t *cr=cairo_create(surface);<br>
<br>
//Paint the background.<br>
cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);<br>
cairo_paint(cr);<br>
<br>
//Translate shape.<br>
cairo_save(cr);<br>
cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 1.0);<br>
cairo_translate(cr, width/4.0, height/4.0);<br>
cairo_move_to(cr, 100.0, 0.0);<br>
cairo_curve_to(cr, 50.0, -50.0, -50.0, -50.0, -100.0, 0.0);<br>
cairo_curve_to(cr, -50.0, 50.0, 50.0, 50.0, 100.0, 0.0);<br>
cairo_path_extents(cr, &x1, &y1, &x2, &y2);<br>
cairo_fill(cr);<br>
cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);<br>
cairo_rectangle(cr, x1, y1, x2-x1, y2-y1);<br>
cairo_stroke(cr);<br>
cairo_restore(cr);<br>
<br>
//Translate and scale shape.<br>
cairo_save(cr);<br>
cairo_set_source_rgba(cr, 0.0, 1.0, 0.0, 1.0);<br>
cairo_translate(cr, 3.0*width/4.0, height/4.0);<br>
cairo_scale(cr, 0.75, 0.75);<br>
cairo_move_to(cr, 100.0, 0.0);<br>
cairo_curve_to(cr, 50.0, -50.0, -50.0, -50.0, -100.0, 0.0);<br>
cairo_curve_to(cr, -50.0, 50.0, 50.0, 50.0, 100.0, 0.0);<br>
cairo_path_extents(cr, &x1, &y1, &x2, &y2);<br>
cairo_fill(cr);<br>
cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);<br>
cairo_rectangle(cr, x1, y1, x2-x1, y2-y1);<br>
cairo_stroke(cr);<br>
cairo_restore(cr);<br>
<br>
//Translate and rotate shape.<br>
cairo_save(cr);<br>
cairo_set_source_rgba(cr, 0.0, 0.0, 1.0, 1.0);<br>
cairo_translate(cr, width/4.0, 3.0*height/4.0);<br>
cairo_rotate(cr, M_PI/2.0);<br>
cairo_move_to(cr, 100.0, 0.0);<br>
cairo_curve_to(cr, 50.0, -50.0, -50.0, -50.0, -100.0, 0.0);<br>
cairo_curve_to(cr, -50.0, 50.0, 50.0, 50.0, 100.0, 0.0);<br>
cairo_path_extents(cr, &x1, &y1, &x2, &y2);<br>
cairo_fill(cr);<br>
cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);<br>
cairo_rectangle(cr, x1, y1, x2-x1, y2-y1);<br>
cairo_stroke(cr);<br>
cairo_restore(cr);<br>
<br>
printf("Output to bounding_box.png\n");<br>
cairo_surface_write_to_png(surface, "bounding_box.png");<br>
<br>
cairo_destroy(cr);<br>
cairo_surface_destroy(surface);<br>
<br>
return 0;<br>
}</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==--