How to draw a closed ofPolyline using its curveTo method?

I would like to know hot to use the curveTo() of ofPolyline to draw a closed polygon, I read the documentation of it but I can work my head around it

Any help will be much appreciated

@nardove there was a helpful thread on this topic recently: ofPath and lineTo vs. curveTo

if you’re working with a collection of vertices and want to create a closed shape, you need to add repeated vertices at the beginning and end of the shape. like a bezier curve, ofPolyline::curveTo() uses interpolation and requires additional points before and after a segment in order to calculate and draw that shape.

compare these 3 examples:

ofPolyline line1, line2, line3;

ofSetColor(255);

// requires 4 points in order to draw the shape (begin, end, 1 before, 1 after)

for(int i=0; i<=90; i+=30){
 
    float x = ofGetWidth()/4 + cos( DEG_TO_RAD * i ) * 100;
    float y = ofGetHeight()/2 + sin( DEG_TO_RAD * i ) * 100;
    
    ofCircle(x, y, 5);
    
    line1.curveTo( x, y );
}

// counts from 0 to 360 but does not close

for(int i=0; i<=360; i+=30){
    
    float x = ofGetWidth()*2/4 + cos( DEG_TO_RAD * i ) * 100;
    float y = ofGetHeight()/2 + sin( DEG_TO_RAD * i ) * 100;
    
    ofCircle(x, y, 5);
    
    line2.curveTo( x, y );
}

// by adding a point before and after, the circle is closed

for(int i=-30; i<=360+30; i+=30){
    
    float x = ofGetWidth()*3/4 + cos( DEG_TO_RAD * i ) * 100;
    float y = ofGetHeight()/2 + sin( DEG_TO_RAD * i ) * 100;
    
    ofCircle(x, y, 5);
    
    line3.curveTo( x, y );
}

line1.draw();
line2.draw();
line3.draw();
1 Like