Question about CPoint objects...
-
Hi I'm new to this forum, but I think it's pretty cool. I had a question about CPoint object. I need to be able to randomly generate an array of them. Which I got that far, but my problem was when the user entered a point, and I was supposed to tell the program to find the closest point in the array. Do any of you know how to do that? I tried using like the '<' compare operator but the CPoint class doesn't have that operator. Don't worry I'm not trying to get free homework (I'm using Introduction to MFC with Visual C++ to learn MFC) or anything I just don't understand and I would like to understand it. Thanks in advance for your help. -CDudd
-
Hi I'm new to this forum, but I think it's pretty cool. I had a question about CPoint object. I need to be able to randomly generate an array of them. Which I got that far, but my problem was when the user entered a point, and I was supposed to tell the program to find the closest point in the array. Do any of you know how to do that? I tried using like the '<' compare operator but the CPoint class doesn't have that operator. Don't worry I'm not trying to get free homework (I'm using Introduction to MFC with Visual C++ to learn MFC) or anything I just don't understand and I would like to understand it. Thanks in advance for your help. -CDudd
You need to use < on the members of CPoint, which are x and y. And I'd answer this question if it *was* homework, because you're trying to do something, not just asking for a complete solution on a plate. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little.
-
You need to use < on the members of CPoint, which are x and y. And I'd answer this question if it *was* homework, because you're trying to do something, not just asking for a complete solution on a plate. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little.
-
Hi I'm new to this forum, but I think it's pretty cool. I had a question about CPoint object. I need to be able to randomly generate an array of them. Which I got that far, but my problem was when the user entered a point, and I was supposed to tell the program to find the closest point in the array. Do any of you know how to do that? I tried using like the '<' compare operator but the CPoint class doesn't have that operator. Don't worry I'm not trying to get free homework (I'm using Introduction to MFC with Visual C++ to learn MFC) or anything I just don't understand and I would like to understand it. Thanks in advance for your help. -CDudd
CDuddley wrote: to find the closest point If i understand correctly, you need to define a measure of distance between CPoints to do that. Most probably that would be euclidean distance , i.e. d = sqrt((x1 - x2)^2 + (y1 - y2)^2) So define a function that calculates the euclidean distance between two CPoints, maybe something like this:
#include <math.h> // or <cmath> and std:: if your compiler/library isn't broken
double distance(const CPoint& p1, const CPoint& p2)
{
return sqrt((p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y));
}a nicer variant (performance?) of this would be
double distance(const CPoint& p1, const CPoint& p2)
{
CSize diff = p1 - p2;
return sqrt(diff.x * diff.x + diff.y * diff.y);
}(of course, for your purpose, you don't need to calculate the sqrt, but now i'm blabbering) Now all you's gotta do is iterate over the array of CPoints, calculating the distance between your given test point and each point in the array and keeping track of the array index of the point that is the closest to the test point (now read this paragraph again). that-exercise-is-left-to-the-OP-ly y'rs --pg :)
-
CDuddley wrote: to find the closest point If i understand correctly, you need to define a measure of distance between CPoints to do that. Most probably that would be euclidean distance , i.e. d = sqrt((x1 - x2)^2 + (y1 - y2)^2) So define a function that calculates the euclidean distance between two CPoints, maybe something like this:
#include <math.h> // or <cmath> and std:: if your compiler/library isn't broken
double distance(const CPoint& p1, const CPoint& p2)
{
return sqrt((p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y));
}a nicer variant (performance?) of this would be
double distance(const CPoint& p1, const CPoint& p2)
{
CSize diff = p1 - p2;
return sqrt(diff.x * diff.x + diff.y * diff.y);
}(of course, for your purpose, you don't need to calculate the sqrt, but now i'm blabbering) Now all you's gotta do is iterate over the array of CPoints, calculating the distance between your given test point and each point in the array and keeping track of the array index of the point that is the closest to the test point (now read this paragraph again). that-exercise-is-left-to-the-OP-ly y'rs --pg :)
Thanks a bunch for the post. I was having trouble with the fact that if a random point was greater than the given point the point would be negative when I took the difference. I forgot all about the euclidian distance formula thanks a ton! I'll try to get the test program working now! Thanks again, CDudd
-
CDuddley wrote: to find the closest point If i understand correctly, you need to define a measure of distance between CPoints to do that. Most probably that would be euclidean distance , i.e. d = sqrt((x1 - x2)^2 + (y1 - y2)^2) So define a function that calculates the euclidean distance between two CPoints, maybe something like this:
#include <math.h> // or <cmath> and std:: if your compiler/library isn't broken
double distance(const CPoint& p1, const CPoint& p2)
{
return sqrt((p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y));
}a nicer variant (performance?) of this would be
double distance(const CPoint& p1, const CPoint& p2)
{
CSize diff = p1 - p2;
return sqrt(diff.x * diff.x + diff.y * diff.y);
}(of course, for your purpose, you don't need to calculate the sqrt, but now i'm blabbering) Now all you's gotta do is iterate over the array of CPoints, calculating the distance between your given test point and each point in the array and keeping track of the array index of the point that is the closest to the test point (now read this paragraph again). that-exercise-is-left-to-the-OP-ly y'rs --pg :)