Hi!
Suppose i have a polygon with 5 points. They are constant.
I use "gotoxy()" to define the point positions.
Something like this:

x1=30; y1=10;
gotoxy(x1,y1);
cout<<"A";

x2=50; y2=10;
gotoxy(x2,y2);
cout<<"B";
//.....and so one up five

Now i should to give a another point (X,Y), and calculate with wich point defined early is too near.
For the first i thinkd that i cand substract X from earch point x1, x2....x5 and Y from y1, y2... y5 and to see the almost points, or who have less difference between coordinate. But i wrong.
How cand i do this? Some ideas please!

Recommended Answers

All 6 Replies

Hi!
Suppose i have a polygon with 5 points. They are constant.
I use "gotoxy()" to define the point positions.
Something like this:

x1=30; y1=10;
gotoxy(x1,y1);
cout<<"A";

x2=50; y2=10;
gotoxy(x2,y2);
cout<<"B";
//.....and so one up five

Now i should to give a another point (X,Y), and calculate with wich point defined early is too near.
For the first i thinkd that i cand substract X from earch point x1, x2....x5 and Y from y1, y2... y5 and to see the almost points, or who have less difference between coordinate. But i wrong.
How cand i do this? Some ideas please!

Not sure I follow what the problem is or what this code/algorithm is and what gotoxy (int, int) does. Is this a function you have written? Is the question a mathematical question of how to calculate the points in a polygon, how to code it, what the formula for distance between two points is, what? Please elaborate on what the problem is and you may want to post more code.

gotoxy(), is a predifined function, is position cursor in text window.

Analytical geomentry, gives variousways to calculate distance. In this case, we can use

sqrt( (x1- x2)*(x1-x2) + (y1- y2)*(y1-y2) )

Analytical geomentry, gives variousways to calculate distance. In this case, we can use

sqrt( (x1- x2)*(x1-x2) + (y1- y2)*(y1-y2) )

hey ,
Prabhakar's qoute is right. Let me elaborate this solution for you. I think to calculate nearest point, u should take following approach.
Lets assume that you have 3 points (x1,y1)(x2,y2)(x3,y3). Now you have a point (X,Y) from which you have to search nearest point from mentioned 3 points.
Simply take diff between X-x1, X-x2, X-x3, (If ans is negative , multiply by -1)
Same with Y co-ordinates. Y-y1, Y-y2, Y-y3.

Then Do addition of all respective diff. Like (X-x1)+(Y-y1), (X-x2)+(Y-y2) & so on. The min addition that u got is the nearest point from (X,Y). e.g. if (X-x1)+(Y-y1) is min. then (x1,y1) is the nearest point.

hey ,
Prabhakar's qoute is right. Let me elaborate this solution for you. I think to calculate nearest point, u should take following approach.
Lets assume that you have 3 points (x1,y1)(x2,y2)(x3,y3). Now you have a point (X,Y) from which you have to search nearest point from mentioned 3 points.
Simply take diff between X-x1, X-x2, X-x3, (If ans is negative , multiply by -1)
Same with Y co-ordinates. Y-y1, Y-y2, Y-y3.

Then Do addition of all respective diff. Like (X-x1)+(Y-y1), (X-x2)+(Y-y2) & so on. The min addition that u got is the nearest point from (X,Y). e.g. if (X-x1)+(Y-y1) is min. then (x1,y1) is the nearest point.

Well Vector Algebra and translation of axis has their advantages.

Nice Explaination sacdpathade.

Place your points in an array and use a loop based on the number of points to calculate the distances, checking each result with the last to see if it is less. If so, it should become the new "shortest distance"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.