How to do a calculation between a POINT and vector data?

Hi, I have a POINT and a set of data get from a vector function that contained hwnd and rect.

std::vector<VECDATA> vecListData;

myVec->emplace_back(VECDATA(hwnd, rect));

1
2
3
4
for (UINT i = 0; i < vecListData.size(); i++) {

  //do the stuff
}


I would like to get the hwnd based on which rect (rect.left & rect.top) is the nearest to POINT (pt.x & pt.y). Generally, is to compare which object is the nearest object to the POINT.

To know which object is the nearest, each of the rect will have a subtraction with the POINT. Then, store the results as a set of values. At the end, we get the hwnd based on the minimum value.

Is it possible? Any idea? I'm using Visual Studio 2008, Visual C++ MFC Application.

Thanks.
Last edited on
The distance of two points can be calculated according to Pythagorean theorem/Euclidean distance. See:

https://en.wikipedia.org/wiki/Pythagorean_theorem

So using the hwnd where the distance has the smallest value.


Wouldn't it be better to use the center of the rectangle?
Hi @coder777, it could be better to use center of rectangle to compare. Thanks!

But perhaps that I could get the idea on how to compare those rectangles with the POINT and get the corresponding hwnd.
This might give you an idea:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
... result_hwnd{};
int distance = std::numeric_limits<int>::max(); // int or something else

for (UINT i = 0; i < vecListData.size(); i++) {

  x_center = ((vecListData.rect.right - vecListData.rect.left) / 2.0) + vecListData.rect.left; // rounding?
  y_center = ... // same like above just vertical

  x_dist = pt.x - x_center;
  y_dist = ... // same like above just vertical

  dist = static_cast<int>(sqrt((x_dist * x_dist) + (y_dist * y_dist)) + 0.5);

  if(dist < distance)
  {
    dist = distance;
    result_hwnd = vecListData.hwnd;
  }
}
I'm not sure how to define this line to get the largest distance (rect center and point) among those vector data. Since the calculation only start in for loop? Sorry I'm not sure how to apply the numeric_limits.

int distance = std::numeric_limits<int>::max();
Last edited on
I'm not sure what you mean by 'apply'? You need however #include <limits> . It just needs to be large number.

There is a mistake on line 16. Correctly it needs to be:

distance = dist;

I'm not sure how to define this line to get the largest distance (rect center and point) among those vector data.
largest? I thought nearest?
Yes, it works fine after having #include <limits> and adding () for it:

int distance = (std::numeric_limits<int>::max)();

I'm not sure why it need to add (), but now it works fine! Thanks
I'm not sure why I need to add (), but now it works fine! Thanks

Certain header files in the Win32 API define function-like macros named min and max.

max() can be treated as a function-like macro, but (max)() cannot. Strongly prefer the use of ALL_UPPER_CASE for macro names.

You could also say
1
2
#undef min
#undef max 

just once, after you've included all the Win32 headers you need.
Last edited on
Topic archived. No new replies allowed.