![]() |
| | #1 |
| Registered User Join Date: Dec 2007
Posts: 16
| finding nearest neighbour distance my problem is as follows: I have a list of N pairs of coordinates labelled (x[i],y[i]) i=1,...,N. What I want to do is to take every point (x[i],y[i]) and find the Pythagorean distance to it's nearest neighbour. So I have to loop over all the points, and then take that minimum distance for each point.. the distance will be given by sqrt((x-x1)^2 +(y-y1)^2) I am new to C and have no idea how to do this! Any help would be appreciated thanks a lot mo786 |
| zesty is offline | |
| | #2 |
| Registered User Join Date: Nov 2009
Posts: 60
| I would start making a struct "point" which has two members: x and y (floating point numbers). Then you proceed with making an array of "points". I hope this helps you to start... |
| hilarius is offline | |
| | #3 |
| Registered User Join Date: Jun 2008
Posts: 1,266
| I assume that x[] and y[] are arrays. In which case you have N points. Each point (i) has the coordinate (x[i], y[i]) Find the distance of its nearest neighboor? Do you just mean find the nearest neighboor? I mean you cannot know which is the nearest if you don't calculate distances. The algorithm is as follows in pseudo-code. Assume sqrt and pow (power)functions Code: for(i : 1...N) distance = sqrt(pow(x[i], 2) + pow(y[i], 2)); store distance; Post what code you have until now |
| C_ntua is offline | |
| | #4 | |
| Algorithm Dissector Join Date: Dec 2005 Location: New Zealand
Posts: 2,654
| First some questions and an observation from what has been posted already:
There's no need to use a sqrt at all here. Just compare distances-squared. Quote:
__________________ My homepage Advice: Take only as directed - If symptoms persist, please see your debugger | |
| iMalc is offline | |
| | #5 |
| Registered User Join Date: Jan 2008 Location: Seattle
Posts: 575
| When you plot your 3 points, for your pythagorean theorem to work, these points must form a right triagle (90 deg). I guess your requirements or needing you to calculate the destance of the hypotenuse and use this as your distance measurement? There is a lot more at play here unless the points given in your array are guaranteed to form right angled triangles. |
| slingerland3g is offline | |
| | #6 |
| Registered User Join Date: Nov 2009
Posts: 60
| For the distance between two points, you don't need a third point. If you take two perpendicular axes X and Y, then the points (x,y) and (x1, y1) are at distance: sqrt((x-x1)^2 +(y-y1)^2): this formula is based on Pythagoras, but the right angle comes from the angle between the axes. So, zesty was right with his formula. |
| hilarius is offline | |
| | #7 | |
| Registered User Join Date: Aug 2006 Location: Liverpool UK
Posts: 380
| Quote:
| |
| rogster001 is offline | |
| | #8 |
| Registered User Join Date: Nov 2009
Posts: 60
| Here is my attempt for a function that takes the distance between two points: Code: #include <stdio.h>
#include <math.h>
struct point{
double x;
double y;
};
typedef struct point point;
double distance (point a, point b);
int main(){
point a = {1.2, 2.3};
point b = {3.0, 3.1};
printf("%f\n", distance(a,b));
return 0;
}
double distance (point a, point b){
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
Last edited by hilarius; 11-27-2009 at 02:06 AM. Reason: explanation |
| hilarius is offline | |
| | #9 | ||
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,923
| Quote:
Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | ||
| laserlight is online now | |
| | #10 | |||
| The larch Join Date: May 2006
Posts: 3,175
| Quote:
However, in this excercise, I suppose a N^2 algorithm is acceptable and expected. Quote:
__________________ I might be wrong. Quote:
| |||
| anon is offline | |
| | #11 |
| Registered User Join Date: Nov 2009
Posts: 60
| I think this program takes the shortest distance between a list of points. My list is a simple array of points. The point is a simple struct with 2 members: x and y coordinate. Code: #include <stdio.h>
#include <math.h>
struct point{
double x;
double y;
};
typedef struct point point;
double distance (point a, point b);
int main(){
int i = 0, j = 0;
point myPoints[5] = {{1.0, 2.0}, {1.0, 4.0}, {2.0,3.0}, {1.0, 3.0}, {3.0, 7.0}};
double result = distance(myPoints[0], myPoints[1]);
printf("%s%f\n", "eerste: ", result);
for (i = 0; i < 5; i++){
for (j = 0; j < 5; j++){
if (i == j)
continue;
if (result > distance(myPoints[i], myPoints[j]))
result = distance(myPoints[i], myPoints[j]);
printf("%d %d\n", i, j);
printf("%f\n", result);
}
}
return 0;
}
double distance (point a, point b){
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
If the new distance is smaller than the result so far, the result gets this smaller value. At the end of the for-loops, result has the smallest distance. |
| hilarius is offline | |
| | #12 |
| Registered User Join Date: Jun 2008
Posts: 1,266
| Some minor optimizations Code: int dis; .... //in for-loop dis = distance(myPoints[i], myPoints[j]); if (result > dis) result = dis; Code: double distance (point a, point b){
int t1 = a.x-b.x;
int t2 = a.y-b.y;
return sqrt(t1*t1, t2*t2);
}
|
| C_ntua is offline | |
| | #13 | |
| Registered User Join Date: Nov 2009
Posts: 60
| Quote:
| |
| hilarius is offline | |
| | #14 | ||
| Algorithm Dissector Join Date: Dec 2005 Location: New Zealand
Posts: 2,654
| Quote:
Sure I could have waited to get an answer to how many points there are first before asking that one, but as we can see it's taking long enough to get a second post as it is. Quote:
__________________ My homepage Advice: Take only as directed - If symptoms persist, please see your debugger | ||
| iMalc is offline | |
| | #15 |
| Registered User Join Date: Dec 2007
Posts: 16
| thanks everyone for all your help! sorry it has taken me so long to reply.. at the moment, i am simply looking for a computation of the order of N^2 where N is the number of points labelled (x(i),y(i)) that I have. Once I have done this, then I will investigate ways to improve my algorithm to lower orders, but just want to deal with the simplest case at the moment. As, I only have a 2D array, the distance between any two points is of the form as I wrote in my original post. My aim was to take each point, compute the DISTANCES to every other point, and then take the minimum of these distances in order to find it's nearest neighbour. The value of this minimum distance would then be substituted into a seperate formula. All your posts have been a great help, thanks so much! zesty xx |
| zesty is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| For the numerical recipes in C types! | Smattacus | C Programming | 5 | 10-28-2008 07:57 PM |
| Distance Formula in my program..... I need help fast!!! | Mackology101 | C Programming | 3 | 09-23-2004 10:10 PM |
| shortest path problems | talz13 | C++ Programming | 7 | 05-08-2004 06:13 AM |
| Distance Formula Implecations: Urgent! | KneeLess | C Programming | 6 | 03-20-2004 10:52 PM |
| Fuzzy Logic | ygfperson | A Brief History of Cprogramming.com | 10 | 10-13-2002 04:58 PM |