I have to subtract two vectors of type Point2f (both are of same size). I know it can be done by extracting the values at each index and then subtracting them in a loop but is there a direct method to it? Something like
Vector<Point2f> A[3];
A[2] = A[1] - A[0];
just for sports ;)
std::vector<Point2f> A,B;
A.push_back(Point2f(1,2));
A.push_back(Point2f(3,4));
A.push_back(Point2f(5,6));
B.push_back(Point2f(5,2));
B.push_back(Point2f(4,4));
B.push_back(Point2f(3,6));
// Mat C; subtract(A,B,C);
Mat C = Mat(A) - Mat(B);
cout<< A << endl << B << endl <<C<<endl;
[1, 2; 3, 4; 5, 6]
[5, 2; 4, 4; 3, 6]
[-4, 0; -1, 0; 2, 0]
As per the documentation link that you provided, subtraction of two points is supported. So the following should work:
std::transform (A[1].begin(), A[1].end(), A[0].begin(), A[2].begin(), std::minus<Point2f>());
Note that this assumes that A[2] is big enough to store the result.
Alternative, you could write your own overloaded operator-() for vector subtraction:
const vector<Point2f> operator-(const vector<Point2f>& lhs, const vector<Point2f>& rhs)
{ ... }
You would need to optimize the above function to avoid a copy of the vector when the function returns. This does not preclude the need to write the looping code that you want to avoid. But it will give you a cleaner syntax for vector subtractions.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.