Home > Net >  Find the distance of the closest pair of points ( c )
Find the distance of the closest pair of points ( c )

Time:11-20

#include <stdio.h>
#include <math.h>

typedef struct
{
    int x,y;
}point;

double distance(point p1[], point p2[], int i)
{
    double d = sqrt((pow(p2[i 1].x-p1[i].x,2))   (pow(p2[i 1].y-p1[i].y,2)));
    return d;
}

int main()
{
    int size,i;
    double d;
    printf("Enter number of point: ");
    scanf("%d",&size);
    point p[size];
    for(i=0;i<size;i  )
    {
        printf("Enter point %d: ",i 1);
        scanf("%d,%d",&p[i].x,&p[i].y);
    }
    d = distance(p[0].x,p[0].y,0);
    for(i=0;i<size-1;i  )
    {
        if( d > distance(p[i 1].x,p[i 1].y,i))
           {
               d = distance(p[i 1].x,p[i 1].y,i);
           }
    }
    printf("Closest pair distance = %.4lf",d);
}

I have been trying to finish this homework for a while and I'm not sure on how to fix this.

The output supposed to look like this: enter image description here

This is what I got: enter image description here

CodePudding user response:

First, I think your distance function should get two points, and not two points arrays. you also don't need to pass the index. this way your function will only do what it should do: calculate the Euclidean distance.

double distance(point p1, point p2)
{
    double d = sqrt(pow(p2.x-p1.x,2)   pow(p2.y-p1.y,2));
    return d;
}

Second, in your main function, do you want to check only the distance between consecutive points or between any point? I think you want the second option but decide for yourself:

first option:

for(i=0;i<size-1;i  )
{
    if( d > distance(p[i],p[i 1]))
    {
        d = distance(p[i],p[i 1]);
    }
}

second option:

for(i=0;i<size-1;i  )
{
    for (j=i 1;j<size;j  )
    {
        if( d > distance(p[i],p[j]))
        {
            d = distance(p[i],p[j]);
        }
    }
}

notice I set j=i 1 because first, i don't want to calculate distance(p[i],p[i]) because that will always be 0 and that is the minimal value distance can return. secondly it's sufficient to test only for j>i values because distance(p[i],p[j])==distance(p[j],p[i])

I hope that cover everything

CodePudding user response:

I have done some changes. Read the comments marked with // CHANGE HERE to understand the changes.

#include <stdio.h>
#include <math.h>
#include <float.h>
#include <stdlib.h>

typedef struct
{
    // CHANGE HERE: double instead of int
    double x, y;
} point;

// CHANGE HERE: just accept two points whose distance needs to be calculated
double distance(point p1, point p2)
{
    return sqrt((pow(p1.x -  p2.x, 2))   (pow(p1.y - p2.y, 2)));
}

int main()
{
    int size, i, j;
    double d;
    
    printf("Enter number of points: ");
    scanf("%d", &size);
    
    // CHANGE HERE: use malloc for variable sized arrays
    point* p = malloc(size * sizeof(point)); 
    for (i = 0; i < size; i  )
    {
        // CHANGE HERE: read double instead of int
        printf("Enter point %d: ", i   1);
        scanf("%lf,%lf", &p[i].x, &p[i].y);
    }
    
    // CHANGE HERE: store a high value by default
    d = DBL_MAX;
    for (i = 0; i < size - 1; i  )
    {
        // CHANGE HERE: to get the exact pair of points with closest distance
        // you need to compare the distance of each point with the rest
        for (j = i   1; j < size; j  )
        {
            // CHANGE HERE: pass previous and current point to distance
            double dist = distance(p[i], p[j]);
            if (d > dist)
            {
               d = dist;
            }
        }
    }
    printf("Closest pair distance = %.4lf", d);
    
    // CHANGE HERE: don't forget to free malloc()ed memory
    free(p);
    return 0;
}

As mentioned by @tstanisl, you can also use hypot function present in math.h, like:

double distance(point p1, point p2)
{
    return hypot(p1.x - p2.x, p1.y - p2.y);
}
  •  Tags:  
  • c
  • Related