question

DangDKhanh-2637 avatar image
0 Votes"
DangDKhanh-2637 asked BarrySchwarz-8780 answered

find maximum rectang of vector points?

Hi,
My problem is: i have 2 group points green and yellow color,
now i need to find the lowest point P1(left side) and highest point P2(right side)

For the green group
The logic that I use is sort to find the point with the smallest X is M to get P1.x=M.x
then sort further to find the point with the smallest Y is N to get P1.y=N.y
The code that I use is as follows and similar to the yellow group:

PT3D ptmin, ptmax;
std::sort(vecptgreen.begin(), vecptgreen.end(), [](PT3D& a, PT3D& b) {
return a.x <= b.x;
});
ptmin.x = vecptgreen[0].x;
std::sort(vecptgreen.begin(), vecptgreen.end(), [](PT3D& a, PT3D& b) {
return a.y <= b.y;
});
ptmin.y = vecptgreen[0].y;

100723-image.png

I want to ask is there any way to shorten this process? (Previously these 2 groups were one but I have classified them to make it easier to do)

Thanks you!


c++
image.png (10.0 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

BarrySchwarz-8780 avatar image
1 Vote"
BarrySchwarz-8780 answered

Unless you later take advantage of the fact the the vector is sorted by y, sorting involves the unnecessary processing to rearrange the elements of the vector. Doing so twice for each vector doubles the inefficiency.

Wouldn't it be much faster to make one pass through the vector and extract the minimum x and y as you go. Something like

     ptmin.x = ptmin.y = INT_MAX;
     for (auto v = vecptgreen.begin(); v < vecptgreen.end(); v++)
        {
        if (v->x < ptmin.x) ptmin.x = v->x;
        if (v->y < ptmin.y) ptmin.y = v->y;
        }

I don't do much plotting but isn't the point P1 the maximum x and minimum y while P2 is the minimum x and maximum y?



· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,
my desired result is P1(minx,miny) P2(maxx,maxy)
Thanks for your answer, I didn't think of your approach, using loop works for me now !

0 Votes 0 ·