#include #include #include #include void plot (cairo_t* cr, int n, double r, int width, int height) { int i; double pi = 3.141592653589; double d_theta = 2 * pi / n; cairo_set_source_rgba (cr, 1, 0, 0, 1); cairo_move_to (cr, width/2 + r * cos (0), height/2 + r * sin (0)); for (i = 0; i < n; i++) { double theta = i * d_theta; cairo_line_to (cr, width/2 + r * cos (theta), height/2 + r * sin (theta)); } cairo_close_path (cr); cairo_fill (cr); //cairo_stroke (cr); } void plot_png (int n, double r, int width, int height) { cairo_t* cr; cairo_surface_t* cs_ptr; cs_ptr = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height); cr = cairo_create (cs_ptr); cairo_set_fill_rule (cr, CAIRO_FILL_RULE_EVEN_ODD); plot (cr, n, r, width, height); cairo_surface_write_to_png (cs_ptr, "test.png"); cairo_surface_destroy (cs_ptr); cairo_destroy (cr); } void plot_x (int n, double r, int width, int height) { GC gc; int screen; Pixmap pixmap; Display* display; Window root, window; unsigned long pixel; cairo_t* cr; cairo_surface_t* cs_ptr; display = XOpenDisplay (NULL); screen = DefaultScreen (display); root = DefaultRootWindow (display); pixel = WhitePixel (display, screen); window = XCreateSimpleWindow (display, root, 0, 0, width, height, 0, 0, pixel); XMapWindow (display, window); XSelectInput (display, window, StructureNotifyMask); pixmap = XCreatePixmap (display, window, width, height, DefaultDepth (display, screen)); cs_ptr = cairo_xlib_surface_create (display, pixmap, DefaultVisual (display, screen), width, height); gc = XCreateGC (display, window, 0, 0); XCopyArea (display, pixmap, window, gc, 0, 0, width, height, 0, 0); XFlush (display); cr = cairo_create (cs_ptr); cairo_set_fill_rule (cr, CAIRO_FILL_RULE_EVEN_ODD); plot (cr, n, r, width, height); XCopyArea (display, pixmap, window, gc, 0, 0, width, height, 0, 0); XFlush (display); sleep (2); XFreePixmap (display, pixmap); XDestroyWindow (display, window); cairo_destroy (cr); cairo_surface_destroy (cs_ptr); } int main (int argc, char** argv) { int width = 800; int height = 600; int n = atoi (argv[1]); plot_x (n, 200, width, height); // plot_png (n, 200, width, height); };