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));
}
I compare the counters i and j, because it doesn't make sense to compare the distance between a point and itself. So in case i == j, the program continues with a next j.
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.