How to get average of vector<ofVec2f>?

Hi, I’m trying to find out the average ofVec2f value from vector<ofVec2f>.

I found out there is ofVec2f::average(const ofVec2f *points, int num) method which does the job but I can’t figure out how to convert vector<ofVec2f> to const ofVec2f* so I can pass the vector of ofVec2f as an argument.

How can I pass vector<ofVec2f> to the ofVec2f::average() method?
Thanks in advance!

Hi,
You can do something like this:

ofVec2f avg;
avg.average(&vector[0], numPoints);

Here is a simple code:

    vector<ofVec2f> testVec;
	testVec.push_back(ofVec2f(5, 0));
	testVec.push_back(ofVec2f(6, 5));
	testVec.push_back(ofVec2f(7, 10));
	testVec.push_back(ofVec2f(8, 15));


	ofVec2f avg;
	int numPoints = 4;
	ofVec2f points[4];
	for (int i = 0; i<numPoints; i++) {
		points[i].set(i+5,i*5);
		ofLogNotice("point") << points[i];
	}
	
	avg.average(points, numPoints);
	ofLogNotice("Average array") << avg;

	avg.average(&testVec[0], numPoints);
	ofLogNotice("Average vector") << avg;
1 Like

if you’re using c++11 or newer, you could also use:

avg.average(testVec.data(), testVec.size());
2 Likes

Thank you so much guys!