Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding closest CPoint to an array of random CPoint objects

Status
Not open for further replies.

CDuddley

Programmer
Feb 4, 2002
26
0
0
US
[tt]
#include "AnArray.h"
#include <iostream.h>

int main()
{
CAnArray CPointS;
CPoint pt, spt;

srand( (unsigned)time( NULL ) );
for(int i = 0; i < 20; i++)
{
pt.x = rand()%100;
pt.y = rand()%100;
CPointS.Add(pt);
cout << &quot;(&quot; << pt.x << &quot;,&quot; << pt.y << &quot;)&quot; << endl;
}

cout << &quot;Enter an x coordinate: &quot;;
cin >> spt.x;

cout << &quot;Enter a y coordinate: &quot;;
cin >> spt.y;
[/tt]
This is what I have so far. How do I sort through the array to find the closest random CPoint object to the CPoint object the user enters? Could you include comments so I could understand I really want to learn this stuff :). Thanks for you time guys!

-CDudd
 
double MinDistance = sqrt((spt.y-CPoins[0].y)**2 +
(spt.x-CPoins[0].x)**2);
//this computes the distnace between the first
// point in the array to the point the user eneterd
// and initializes it as the MinDistance
// now loop over all the rset of the points and
// check if there is one that is closer
for (int j=1;i<20;i++)
{
// I assume the [] operator is defined
double Check = sqrt((spt.y-CPoins[j].y)**2 +
(spt.x-CPoins[j].x)**2);
if (Check < MinDistance)
MinDistance = Check;
}

Good Luck
======
SeekerOfKnowledge
======
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top