Cairo context unable to fill color inside the second drawing which is inside/top of the first drawing

Rectangle on the left is the desired output,
where the rectangle is filled with blue color and a random shape filled with yellow.

Rectangle on the right is the what I can produce,
where the rectangle is filled with blue color and a random shape only the outline is yellow, inside it shows the blue color only not yellow.

Here the code

Rectangle

// For drawing the rectangle
  cr->set_line_width(1);
  cr->move_to(int(rectangle.x ), int(rectangle.y ));
  cr->rectangle(int(rectangle.x ), int(rectangle.y ), int(rectangle.width ), int(rectangle.height ));
  cr->set_source_rgba(getColor().r, getColor().g, getColor().b, getColor().a);
  cr->fill_preserve();
  cr->stroke();
  // cr->save();
  if(subtractionDrawing.size() != 0)
  {
    for (int i = 0; i < subtractionDrawing.size(); ++i)
    {
      subtractionDrawing[i]->drawTo(cr);
    }
  }

Subtraction Region

cr->set_source_rgba(1.0,1.0,1.0, 1.0);
      for(size_t i = 0; i < contourPoints.size(); i++)
      {
        if(i == contourPoints.size() - 1)
        {
          // std::cout<<"HERE DO NOT PLOT\n";
        }
        else
        {
          cr->move_to(contourPoints[i].x, contourPoints[i].y );
          cr->line_to(contourPoints[i+1].x, contourPoints[i+1].y );
        }
      }
      // cr->set_fill_rule(Cairo::WINDING);
      cr->close_path();
      cr->stroke_preserve();
      cr->fill_preserve();

Could anyone point out where I am getting this wrong?

What happens if you remove the move_to() call?

for(size_t i = 0; i < contourPoints.size(); i++)
  {
    if(i == contourPoints.size() - 1)
      {
        // std::cout<<"HERE DO NOT PLOT\n";
      }
    else
      {
        cr->move_to(contourPoints[i].x, contourPoints[i].y );
        cr->line_to(contourPoints[i+1].x, contourPoints[i+1].y );
      }
  }

E.g something like:

assert(contourPoints.size() > 0);
cr->move_to(contourPoints[0].x, contourPoints[0].y );
for (size_t i = 1; i < contourPoints.size(); i++)
  {
    cr->line_to(contourPoints[i].x, contourPoints[i].y);
  }

move_to() always starts a new path, so shouldn’t be called in the loop.

It does fill, but some random shapes are getting filled all over the image.

1 Like

Could you post a screenshot? Could you also try with a basic set of points (e.g a triangle or a rectangle)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.