美文网首页C/CPLUS
C语言使用Cairo实现形状组合效果

C语言使用Cairo实现形状组合效果

作者: 一路向后 | 来源:发表于2022-03-14 22:44 被阅读0次

    1.源码实现

    #include <stdio.h>
    #include <cairo/cairo.h>
    
    #define M_PI 3.141592654
    
    int main()
    {
        cairo_surface_t *surface, *surface2;
        cairo_t *cr, *cr2;
    
        surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 300, 300);
        surface2 = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 300, 300);
        cr = cairo_create(surface);
        cr2 = cairo_create(surface2);
    
        cairo_set_source_rgb(cr, 0, 1, 0);
        cairo_arc(cr, 100, 150, 80, 0, 2 * M_PI);
        cairo_fill(cr);
    
        cairo_set_operator(cr, CAIRO_OPERATOR_DEST_OUT);
    
        cairo_set_source_rgb(cr, 1, 1, 0);
        cairo_arc(cr, 200, 150, 80, 0, 2 * M_PI);
        cairo_fill(cr);
    
        cairo_surface_flush(surface);
    
    
    
        cairo_set_source_rgb(cr2, 0, 1, 0);
        cairo_arc(cr2, 100, 150, 80, 0, 2 * M_PI);
        cairo_fill(cr2);
    
        cairo_set_operator(cr2, CAIRO_OPERATOR_OUT);
    
        cairo_set_source_rgb(cr2, 1, 1, 0);
        cairo_arc(cr2, 200, 150, 80, 0, 2 * M_PI);
        cairo_fill(cr2);
    
        cairo_surface_flush(surface2);
    
        cairo_set_operator(cr2, CAIRO_OPERATOR_ADD);
    
        cairo_set_source_surface(cr2, surface, 0, 0);
        cairo_paint(cr2);
        cairo_surface_flush(surface2);
    
        cairo_surface_write_to_png(surface2, "a.png");
    
        cairo_surface_destroy(surface);
        cairo_surface_destroy(surface2);
    
        cairo_destroy(cr);
        cairo_destroy(cr2);
    
        return 0;
    }
    

    2.编译源码

    $ gcc -o test test.c -lcairo
    

    3.运行程序

    $ ./test
    

    4.结果展示

    a.png

    相关文章

      网友评论

        本文标题:C语言使用Cairo实现形状组合效果

        本文链接:https://www.haomeiwen.com/subject/ylqedrtx.html