[cairo] Question about extents and transformations
cecashon at aol.com
cecashon at aol.com
Thu Dec 12 20:38:43 UTC 2019
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 things about drawing. If you are working on a tough drawing you can often see mistakes that you might miss otherwise.
Eric
//gcc -Wall bounding_box1.c -o bounding_box1 `pkg-config --cflags --libs cairo` -lm
#include<cairo.h>
#include<stdio.h>
#include<math.h>
int main()
{
double width=500.0;
double height=500.0;
double x1=0.0;
double y1=0.0;
double x2=0.0;
double y2=0.0;
cairo_surface_t *surface=cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
cairo_t *cr=cairo_create(surface);
//Paint the background.
cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
cairo_paint(cr);
//Translate shape.
cairo_save(cr);
cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 1.0);
cairo_translate(cr, width/4.0, height/4.0);
cairo_move_to(cr, 100.0, 0.0);
cairo_curve_to(cr, 50.0, -50.0, -50.0, -50.0, -100.0, 0.0);
cairo_curve_to(cr, -50.0, 50.0, 50.0, 50.0, 100.0, 0.0);
cairo_path_extents(cr, &x1, &y1, &x2, &y2);
cairo_fill(cr);
cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
cairo_rectangle(cr, x1, y1, x2-x1, y2-y1);
cairo_stroke(cr);
cairo_restore(cr);
//Translate and scale shape.
cairo_save(cr);
cairo_set_source_rgba(cr, 0.0, 1.0, 0.0, 1.0);
cairo_translate(cr, 3.0*width/4.0, height/4.0);
cairo_scale(cr, 0.75, 0.75);
cairo_move_to(cr, 100.0, 0.0);
cairo_curve_to(cr, 50.0, -50.0, -50.0, -50.0, -100.0, 0.0);
cairo_curve_to(cr, -50.0, 50.0, 50.0, 50.0, 100.0, 0.0);
cairo_path_extents(cr, &x1, &y1, &x2, &y2);
cairo_fill(cr);
cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
cairo_rectangle(cr, x1, y1, x2-x1, y2-y1);
cairo_stroke(cr);
cairo_restore(cr);
//Translate and rotate shape.
cairo_save(cr);
cairo_set_source_rgba(cr, 0.0, 0.0, 1.0, 1.0);
cairo_translate(cr, width/4.0, 3.0*height/4.0);
cairo_rotate(cr, M_PI/2.0);
cairo_move_to(cr, 100.0, 0.0);
cairo_curve_to(cr, 50.0, -50.0, -50.0, -50.0, -100.0, 0.0);
cairo_curve_to(cr, -50.0, 50.0, 50.0, 50.0, 100.0, 0.0);
cairo_path_extents(cr, &x1, &y1, &x2, &y2);
cairo_fill(cr);
cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
cairo_rectangle(cr, x1, y1, x2-x1, y2-y1);
cairo_stroke(cr);
cairo_restore(cr);
printf("Output to bounding_box.png\n");
cairo_surface_write_to_png(surface, "bounding_box.png");
cairo_destroy(cr);
cairo_surface_destroy(surface);
return 0;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.cairographics.org/archives/cairo/attachments/20191212/c3ec7a66/attachment.htm>
More information about the cairo
mailing list