0

I have a ton of holes in some of my basics, but here is my problem.

I have a for loop. It loops through all of the instances contourFinder (which is working well so far), but once it tries to build polyline and pathFromContour, I can't figure out a way to link the contour ID ('i') to the newly created polyline and path.

void draw(){
for(int i = 0; i < n; i++) {

    //FOR FILLING
    ofPolyline polyline = contourFinder.getPolyline(i);//to convert
    ofPath pathFromContour;//path to be built

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

    ofColor pathColor(ofRandom(255),ofRandom(255),ofRandom(255));
    pathFromContour.setFillColor(pathColor);
    pathFromContour.draw();

}
}

Because of this, I can't seem to treat my paths or polylines differently. Could anybody explain what I might be doing wrong?

Thanks

2
  • @aruisdante Although he's not reassigning i (the declaration in the inner for "trumps" the outer i in the inner scope of the loop), that's certainly bad for readability. Commented Mar 17, 2014 at 22:42
  • Your code creates a new pathFromContour object on each iteration through the loop. After the draw(), there's nothing hanging around to be "link"ed to by a contour id. If you want these object to continue to exist after the loop, then push_back them into a vector... the vector index will be the same as your contour id. Commented Mar 17, 2014 at 22:58

2 Answers 2

2

Try changing your counter on the inner for loop to something like 'j', or 'k'. It is bad practice to use i for the outer and inner loop.

Sign up to request clarification or add additional context in comments.

Comments

2

Something like this?

void draw(){
    ofPolyline* polylines = new ofPopyline[n];
    ofPath* path = new ofPath[n];
    for(int i = 0; i < n; i++) {

        //FOR FILLING
        ofPolyline& polyline = polylines[i];
        polyline = contourFinder.getPolyline(i);//to convert
        ofPath& pathFromContour = ofPath[i];//path to be built

        for(int j = 0; j < polyline.getVertices().size(); j++) {
            if(j == 0) {
                pathFromContour.newSubPath();
                pathFromContour.moveTo(polyline.getVertices()[j]);
            } else {
                pathFromContour.lineTo(polyline.getVertices()[j]);
            }
        }
        pathFromContour.close();
        pathFromContour.simplify();

        ofColor pathColor(ofRandom(255),ofRandom(255),ofRandom(255));
        pathFromContour.setFillColor(pathColor);
        pathFromContour.draw();

    }
    // free resources if they are no longer needed
    delete[] polylines;
    delete[] path;
}

UPDATE: alternative variant, using vectors

void draw(){
    vector<ofPolyline> polylines;
    vector<ofPath> paths;
    for(int i = 0; i < n; i++) {

        //FOR FILLING
        ofPolyline polyline = contourFinder.getPolyline(i);//to convert
        polylines.push_back(polyline);

        ofPath pathFromContour;//path to be built

        for(int j = 0; j < polyline.getVertices().size(); j++) {
            if(j == 0) {
                pathFromContour.newSubPath();
                pathFromContour.moveTo(polyline.getVertices()[j]);
            } else {
                pathFromContour.lineTo(polyline.getVertices()[j]);
            }
        }
        pathFromContour.close();
        pathFromContour.simplify();

        ofColor pathColor(ofRandom(255),ofRandom(255),ofRandom(255));
        pathFromContour.setFillColor(pathColor);
        pathFromContour.draw();
        paths.push_back(pathFromContour);

    }
    // no need to explicitly free the resources
  }

3 Comments

So in that case, how would I do something such as just changing the pathColor for each path? Draw() is executing at 60fps so the colors are changing way too fast and not sticking to just one of the paths
ofColor newColor; for (int i=0;i<n;i++) paths[i].setFillColor(newColor);
you have to actually initialize the newColor variable, of course. Also, do you do it in the same function? otherwise you have to return the paths vector so that it is not lost. And do you use the version with new/delete or with vectors?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.