ofPolyline closing?

Hey all.

I have created a polyline from a ofVec2f vector using curveTo. The line seems to be closing. How do I keep it a 4 point line without closing? I have tried ofpolyline.end() which doesnt work. Any help appreciated.

1 Like

Still struggling with this if anyone has any advice. I tired adding a start vert and an end vert, but the line is still closing. I also tried setClosed to false in setup(), no luck.

#include "ofApp.h"

ofPolyline straightline;
vector<ofVec2f> straightpoints;

//--------------------------------------------------------------
void ofApp::setup(){
    
    ofBackground(30, 30, 30);
    
    straightline.setClosed(false);
    
    for(int i = 0; i < 5; i++){
        straightpoints.push_back(ofVec2f(ofRandom(0, ofGetWidth()), ofRandom(0, ofGetHeight()) ));
    }

}

//--------------------------------------------------------------
void ofApp::update(){

}

//--------------------------------------------------------------
void ofApp::draw(){
    
    ofSetColor(255, 255, 255, 180);
    
    //first vert
    straightline.addVertex(ofVec2f(60,60));
    
    //mid verts
    for(int i = 0; i < straightpoints.size(); i++){
        straightline.addVertex(straightpoints[i]);
    }

    //end vert
    straightline.addVertex(ofVec2f(60,400));

    straightline.draw();
    straightline.end();
    
    //draw points
    vector<ofVec3f> straightvertices = straightline.getVertices();
    for (int i = 0; i < straightvertices.size(); i++) {
        ofSetColor(255, 255, 255, 180);
        ofDrawCircle(straightvertices[i], 6);
    }
    
}

Gives me:

Any help is SUPER appreciated.

I think this one works.

//--------------------------------------------------------------
void ofApp::setup(){

    ofBackground(30, 30, 30);
    
    for(int i = 0; i < 5; i++){
        straightpoints.push_back(ofVec2f(ofRandom(0, ofGetWidth()), ofRandom(0, ofGetHeight()) ));
    }
    //first vert
    straightline.addVertex(ofVec2f(60,60));
    
    //mid verts
    for(int i = 0; i < straightpoints.size(); i++){
        straightline.addVertex(straightpoints[i]);
    }
    //end vert
    straightline.addVertex(ofVec2f(60,400));
}

//--------------------------------------------------------------
void ofApp::update(){
    
}

//--------------------------------------------------------------
void ofApp::draw(){

    ofSetColor(255, 255, 255, 180);
    
    straightline.draw();
    
    //draw points
    vector<ofVec3f> straightvertices = straightline.getVertices();
    for (int i = 0; i < straightvertices.size(); i++) {
        ofSetColor(255, 255, 255, 180);
        ofDrawCircle(straightvertices[i], 6);
    }
}

Thank you! I didnt even think about moving the addverts to setup. Does that mean all verts need to be defined before drawing or updating?

The problem in your code is that you are adding the vertices to straightline on every draw() call without clearing it.

So if you call straightline.clear() before adding the first vertex, then it will work fine with the current code too.

But if you don’t intend to change contents of straightline once it’s loaded, it is better to call those codes in the setup() method.

And you don’t need straightline.end(); as it is just an iterator.

I hope that helps!

1 Like

Amazing thank you!