fill polyline

Hi,

How do I fill a closed polyline?

This’ what I’m trying, but no luck … only the contour is drawn.

  
  
        vector<ofPolyline> polylines = contourFinder.getPolylines();  
        for(int i = 0; i < polylines.size(); i++) {  
            ofColor(0,0,255);  
            ofFill();  
            polylines[i].simplify();  
	    polylines[i].draw();  
	}  
  

To draw filled shapes you can either copy those ofPolylines into an ofPath instance:

  
  
    ofPath path;  
  
    for( int i = 0; i < poly.getVertices().size(); i++) {  
        if(i == 0) {  
            path.newSubPath();  
            path.moveTo(poly.getVertices()[i] );  
        } else {  
            path.lineTo( poly.getVertices()[i] );  
        }  
    }  
  
    path.close();  
    path.simplify();  
  

Then you can draw the fill just by doing:

  
path.draw();  

or you can draw them the old fashioned OpenGL way:

  
  
ofBeginShape();  
    for( int i = 0; i < poly.getVertices().size(); i++) {  
        ofVertex(poly.getVertices().at(i).x, poly.getVertices().at(i).y);  
    }  
ofEndShape();  
  

3 Likes

@joshuajnoble thanks a lot,I’ve just used your code :v:
OpenGL way much faster btw :smile: