Search code examples
cmathdrawingcairopycairo

How to use cairo to draw rectangle with top left and bottom right cut Corner?


I tried to use cairo to draw a rectangle with top left and bottom right cut Corner, similar to Qt logo.

Here is what I want: enter image description here Here is what I get:

enter image description here

Here is the code:

#include <stdio.h>
#include <stdlib.h>
#include <cairo.h>

int main()
{
    double height = 500, width = 250;

    cairo_surface_t *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 500, 500);
    cairo_t *cr = cairo_create (surface);

    cairo_move_to(cr,0,0);
    cairo_line_to(cr,width,0);
    cairo_line_to(cr,width,height - height/4);
    cairo_line_to(cr,width - height/4,height);
    cairo_line_to(cr,0,height);

    cairo_set_line_width (cr, 10.0);
    cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
    cairo_stroke (cr);

    cairo_destroy (cr);
    cairo_surface_write_to_png (surface, "hello.png");
    cairo_surface_destroy (surface);

    return 0;
}

I do not get the result that I want, someone can give a tip? Whatever the programming language I take.


Solution

  • With your updated shape you are simply missing first stroke. The remaining strokes need to be shifted to the right accordingly.

    double cutoff = height/4.0;
    cairo_move_to(cr, 0, cutoff);
    cairo_line_to(cr, cutoff, 0);
    cairo_line_to(cr, width + cutoff, 0);
    cairo_line_to(cr, width + cutoff, height - cutoff);
    cairo_line_to(cr, width, height);
    cairo_line_to(cr, cutoff, height);