[cairo] Fwd: Regarding problem filling a closed curve.

Gaurav Pruthi gaurav.pruthi88 at gmail.com
Wed Jul 3 02:11:21 PDT 2013


Hi,

I had just started using cairo library... I have seen examples to fill a
closed curve, where I found the method is repetitive of move_to() and
line_to()
calls.
When I tried following snippet of code which is mentioned in most of the
tutorials, it didn't worked

void drawRectangle(cairo_t* cr)
{
    cairo_path_t path;
    cairo_path_data_t pathData[4];
    pathData[0].point.x = 10 ;
    pathData[0].point.y = 20;

    pathData[1].point.x = 210;
    pathData[1].point.y = 20;

    pathData[2].point.x = 210;
    pathData[2].point.y = 320;

    pathData[3].point.x = 10;
    pathData[3].point.y =  320;
    //Set path data in path
    path.data = pathData;
    path.num_data = 4;
    path.status = CAIRO_STATUS_SUCCESS;

    cairo_move_to(cr, pathData[0].point.x, pathData[0].point.y);
    for(int i =0 ; i<= path.num_data -1; ++i)
    {
        cairo_move_to(cr, pathData[i].point.x, pathData[i].point.y);
        if(i == 3){
            cairo_line_to(cr, pathData[0].point.x, pathData[0].point.y);
        }
        else
            cairo_line_to(cr, pathData[i+1].point.x, pathData[i+1].point.y);
    }

    cairo_close_path(cr);

    cairo_set_source_rgb(cr,1.0,0.0,0.0);
    cairo_fill_preserve(cr);

    cairo_set_source_rgba(cr,0.0,1.0,0.0,0.5);
    cairo_set_line_width(cr,10.0);
    cairo_stroke(cr);

}


But this one worked where I am moving to the first point and just making
line_to() calls :

void drawRectangle(cairo_t* cr)
{
    cairo_path_t path;
    cairo_path_data_t pathData[4];
    pathData[0].point.x = 10 ;
    pathData[0].point.y = 20;

    pathData[1].point.x = 210;
    pathData[1].point.y = 20;

    pathData[2].point.x = 210;
    pathData[2].point.y = 320;

    pathData[3].point.x = 10;
    pathData[3].point.y =  320;
    //Set path data in path
    path.data = pathData;
    path.num_data = 4;
    path.status = CAIRO_STATUS_SUCCESS;

    cairo_move_to(cr, pathData[0].point.x, pathData[0].point.y);
    for(int i =0 ; i< path.num_data; ++i)
    {

        cairo_line_to(cr, pathData[i].point.x, pathData[i].point.y);
    }

    cairo_close_path(cr);

    cairo_set_source_rgb(cr,1.0,0.0,0.0);
    cairo_fill_preserve(cr);

    cairo_set_source_rgba(cr,0.0,1.0,0.0,0.5);
    cairo_set_line_width(cr,10.0);
    cairo_stroke(cr);

}


Can somebody please tell why its showing different behavious than what is
mentioned in most of the places. even here also:
http://cairographics.org/samples/

 Thanks

 Gaurav Pruthi

 http://en.wikipedia.org/wiki/User:Gaurav_Pruthi<http://en.wikipedia.org/wiki/User:Gaurav_Pruthi>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cairographics.org/archives/cairo/attachments/20130703/df90b6a3/attachment.html>


More information about the cairo mailing list