Problem with insterting points into ofPolyline

Hello i’m trying to load points from an ofVec2f array into a ofPolyline, but i got a bit lost.

What i need to do is to make a “snake” composed by a known amount of segments. The following function updates the values of the ofVect2fs stored in points[], so the “snake” oscilates in a “sine-snakey-way” (this works).

void MyClass::updatePoints(){
	int seg = 30;
	angle = start;

	for (int i = 0; i < sizeof(points)/sizeof(*points); ++i)
	{	
		float y = ofMap(sin(angle),-1,1,0,amplitude); //calculates the sine
		points[i].set(i*seg, y); //asign the value to the ofVec2f
		line.insertVertex(points[i],i); //inserts the points in my ofPolyline  called "line"
		angle += speed;
	}

	start += 0.02;
}

what is not working, is how these ofVec2f are loaded in my ofPolyline called “line”. In the function above, each time i re-asign the value of each ofVec2f in points[], the vector is passed to the polyline using an index, so, if i’m understading everything correctly, it should “overwrite” the value of the point at the given index, for example: if ido, line.insertVertex(0,0,0) and afterwards, line.insertVertex(1,1,0), the value for the point stored at index 0 should be (1,1), instead of adding a new point with the value (1,1).

question:
How do i update the values of the already existing points within a ofPolyline?

thanks in advance.

< sizeof(points)/sizeof(*points)

I do not know what you wanted to achieve with this. If you want to add, let’s say, 30 points, then:

int i = 0; i < 30; ++i

If you want to iterate through your array of points, that I assume it is define as vector<ofVec2f> points, then:

for(auto & p:points){
    float y = ofMap(sin(angle),-1,1,0,amplitude);
    // change your point p in the array, es p.set(200, 1)
   // insert it into the line
}

While iterating through the points, you are not incrementing start because it is out of the loop.

I assume start, speed and line are defined in your header file, when posting to the forum, post also the definition of the variables interested that are contained into the header.

Also, did you read this http://openframeworks.cc/ofBook/chapters/lines.html ? it explain more or less what are you trying to do.

I hope this helps!

Thanks for your answear, i did a really poor explanation of the problem, I apologize about that.
I edited the post, and i hope is clearer now.

Actually that line example is what i was following, but i dont really know if the vector example is what i actually need.

Thanks so much for your time and help!

In the link to the ofBook, there is an example that does exactly that:

for (auto &vert : line.getVertices()){
    vert.x += ofRandom(-1,1);
    vert.y += ofRandom(-1,1);
}

The key thing is the & operator. That means “give me a reference of a vertex in the line, not a copy”.

1 Like

yes! i realized last night that is what i was looking for!

thanks for the answear ! :smiley:

you’re welcome :wink: