Use the Cairo graphics library in C to draw a pie chart

Source: Internet
Author: User

Many people still stick to the graphics of Turbo C by directly drawing in C language. in the age of H, there may also be reasons for the aging of education. After all, the classic has become a thing of the past. Instead of trying every means to find a compatible graphics library, it is better to embrace the bright future. Cario (http://cairographics.org/) is a very good 2D graphics library, the famous GTK + 3.0 fully uses Cario as the drawing engine, it shows its strong and attractive.

Cario supports multiple output devices, including X Window, quartz, Win32, image. buffers, postscript, PDF, and SVG files. It is open source and cross-platform. Some people may think that such a powerful thing will be very tricky to use. In fact, the API functions it provides are unexpectedly simple and easy to use, the author demonstrates an example of drawing a plot for your reference.

# Include <Cairo. h> # define size (640)/* resize image size */# define Cx (size/2)/* horizontal coordinate of the Center of Taiji circle */# define cy (size/2) /* Y coordinate of the center of the Taiji circle */# define R (size/2) /* * // * converts the angle to the corresponding radian */# define angle (ANG * 3.1415926/180.0) int main (INT argc, char * argv []) {/* Create a 32-bit rgba color Cairo drawing environment and render it directly in memory */cairo_surface_t * surface = cairo_image_surface_create (cairo_format_argb32, size + 1, size + 1); cairo_t * Cr = cairo_create (surface);/* draw a taiji border */cairo_set_line_width (Cr, 2.0); cairo_set_source_rgba (Cr, 0, 0, 0, 0, 1); cairo_arc (Cr, CX, Cy, R, angle (0), angle (360); cairo_stroke (CR);/* plot yin and yang circles */cairo_set_source_rgba (Cr, 0, 0, 0, 1); cairo_arc (Cr, CX, Cy, R, angle (90), angle (270); cairo_fill (CR); cairo_set_source_rgba (Cr, 1, 1, 1, 1); cairo_arc (Cr, CX, Cy, R, angle (-90), angle (90); cairo_fill (CR ); /* draw Yin/yang line */cairo_set_source_rgba (Cr, 0, 0, 0, 1); cairo_arc (Cr, CX, cy-r/2, R/2, angle (-90), angle (90); cairo_fill (CR); cairo_set_source_rgba (Cr, 1, 1, 1); cairo_arc (Cr, CX, cy + R/2, R/2, angle (90), angle (270); cairo_fill (CR);/* draw Taiji eyes */cairo_set_source_rgba (Cr, 1, 1, 1, 1); cairo_arc (Cr, CX, cy-r/2, R/10, angle (0), angle (360); cairo_fill (CR ); cairo_set_source_rgba (Cr, 0, 0, 0, 1); cairo_arc (Cr, CX, Cy + R/2, R/10, angle (0), angle (360 )); cairo_fill (CR);/* store the rendering Effect of memory to the image */cairo_surface_write_to_png (surface, "taichi.png "); /* destroy and exit the Cairo drawing environment */cairo_destroy (CR); cairo_surface_destroy (surface); Return 0 ;}

The code is very simple. I hope more people will like the Cario graphics library. For GCC compilation, refer to the following command.

gcc -o taichi $(pkg-config --cflags --libs cairo) taichi.c

This is the effect after running. You can try it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.