最接近的四点C程序

时间:2018-12-21 14:06:13

标签: c

我需要找到最接近的一对四点C程序。这段代码分三点。我需要四个点的解决方案。

我尝试过了。此解决方案为三个输入。

输入三个点时,我会得到最接近的点,但是我需要四个点中的最接近点。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct Point
{
    int x, y ;
};
double getDistanceAB(struct Point a, struct Point b)
{
    double distanceAB;
    distanceAB = sqrt((a.x - b.x) * (a.x - b.x) + (a.y-b.y) *(a.y-b.y));
    return distanceAB;
}
double getDistanceBC(struct Point b, struct Point c)
{
    double distanceBC;
    distanceBC = sqrt((b.x - c.x) * (b.x - c.x) + (b.y-c.y) *(b.y-c.y));
    return distanceBC;
}
double getDistanceAC(struct Point a, struct Point c)
{
    double distanceAC;
    distanceAC = sqrt((a.x - c.x) * (a.x - c.x) + (a.y-c.y) *(a.y-c.y));
    return distanceAC;
}
int main()
{
    struct Point a, b, c;
    printf("Enter coordinate of points a: ");
    scanf("%d %d", &a.x, &a.y);
    printf("Enter coordinate of points b: ");
    scanf("%d %d", &b.x, &b.y);
    printf("Enter coordinate of points c: ");
    scanf("%d %d", &c.x, &c.y);
    if((getDistanceAB(a,b))>(getDistanceBC(b,c)) && (getDistanceAB(a,b))>(getDistanceBC(a,c)))
    {
        printf("Point A and B are closest.");
    }
    else if((getDistanceBC(b,c))>(getDistanceAC(a,c)) && (getDistanceBC(b,c))>(getDistanceAC(a,b)))
    {
        printf("Point B and C are closest.");
    }
    else if((getDistanceBC(a,c))>(getDistanceAC(a,b)) && (getDistanceBC(a,c))>(getDistanceAC(b,c)))
    {
        printf("Point A and C are closest.");
    }
    else
    {
        printf("All point are same.");
    }
}

4 个答案:

答案 0 :(得分:3)

首先,更改此内容:

<StackPanel Grid.Column="0" Orientation="Horizontal">
    <ToggleButton DockPanel.Dock="Left" Style="{StaticResource MaterialDesignHamburgerToggleButton}" IsChecked="False" 
                x:Name="MenuToggleButton"/>
    <StackPanel DockPanel.Dock="Left">
        <controls:Autocomplete Width="200" HorizontalAlignment="Center" VerticalContentAlignment="Center" VerticalAlignment="Center" Style="{StaticResource MaterialDesignAutocompleteDense}" 
                               AutocompleteSource="{Binding Path=AutocompleteSource}" Hint="Which OS?"
              Background="#A5D6A7"     
               ItemTemplate="{StaticResource itemTemplateDense}" />                            
    </StackPanel>
</StackPanel>

仅此:

double getDistanceAB(struct Point a, struct Point b)
{
    double distanceAB;
    distanceAB = sqrt((a.x - b.x) * (a.x - b.x) + (a.y-b.y) *(a.y-b.y));
    return distanceAB;
}
double getDistanceBC(struct Point b, struct Point c)
{
    double distanceBC;
    distanceBC = sqrt((b.x - c.x) * (b.x - c.x) + (b.y-c.y) *(b.y-c.y));
    return distanceBC;
}
double getDistanceAC(struct Point a, struct Point c)
{
    double distanceAC;
    distanceAC = sqrt((a.x - c.x) * (a.x - c.x) + (a.y-c.y) *(a.y-c.y));
    return distanceAC;
}

函数的主要要点之一是您不必重复代码。

现在,您要做的就是通过对第四点再进行一次扫描来创建四个点,并将其添加到决策树中。

请记住这一点,以确保您做出的决策树...如果使用与原始帖子中相同的逻辑检查点“ a”是否不是最接近的点,则不必比较点“ a”再次。

答案 1 :(得分:2)

我将功能数量减少到double getDistance(struct Point p, struct Point o) 并将您的要点保存在列表中,这样您就可以让程序动态运行这些要点,而不用对每个条件进行编程。

一旦在列表中列出了点,就可以运行循环,以检查列表中每对的距离,并根据当前最短的距离进行检查;如果检查的对的距离更近,则将当前最短的距离更改为检查的对,以及哪对点具有该距离。

这样,您可以扩展它以适用于任意数量的点。

我不习惯C的语法,但是对于检查列表中的点,您需要使用double for循环,其中第一个遍历列表中的每个点,第二个检查距离从/到第一个点到列表中所有以后的点。

for i = 0, i++, length(listOfPoints) {
    for j = i+1, j++, length(listOfPoints) {
        getDistance(listOfPoints[i], listOfPoints[j]
    }
}

希望这对您有所帮助。

答案 2 :(得分:1)

这就是我要解决的方法,

#include <stdio.h>

typedef struct
{
    int x;
    int y;
} Point;

int square(int x) { return x * x; }

int distanceSq(Point *a, Point *b)
{
    return square(a->x - b->x) + square(a->y - b->y);
}

int main(int argc, char const *argv[])
{
    int n = 4;
    Point a[4];
    for (int i = 0; i < n; i++)
    {
        printf("Enter Point %d <as x y>: ", i + 1);
        scanf("%d %d", &a[i].x, &a[i].y);
    }

    int distance = __INT_MAX__;
    int p1 = -1, p2 = -1;

    for (int i = 0; i < n - 1; i++)
        for (int j = i + 1; j < n; j++)
        {
            int current = distanceSq(&a[i], &a[j]);
            if (current < distance)
            {
                p1 = i;
                p2 = j;
                distance = current;
            }
        }

    printf("The closest points are [%d %d] and [%d %d]", a[p1].x, a[p1].y, a[p2].x, a[p2].y);

    return 0;
}

注意:

  1. 这可以扩展n个点
  2. 给我们第一对最接近的点
  3. 我们不需要取平方根,因为如果平方大,则平方根会成比例地变大(在有n个点的情况下,这可能会节省计算时间)

答案 3 :(得分:0)

在这里,您可以解决任意数量的问题。

只需将MAX_POINTS更改为您可能需要的任何内容。

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

#define MAX_POINTS (4U)

struct Point
{
    int x;
    int y;
};

struct PointPair
{
    struct Point a;
    struct Point b;
};

double getDistance(const struct PointPair pair)
{
    return sqrt((pair.a.x - pair.b.x) * (pair.a.x - pair.b.x) +
                (pair.a.y - pair.b.y) * (pair.a.y - pair.b.y));
}

void readPoints(struct Point points[const])
{
    for (unsigned i = 0; i < MAX_POINTS; i++)
    {
        printf("Enter coordinate of point %u: ", i);
        scanf("%d %d", &(points[i].x), &(points[i].y));
    }
}

bool checkForShorterDistance(const struct PointPair pair, double *const p_minDistance)
{
    double tempDistance = getDistance(pair);

    if (tempDistance < *p_minDistance)
    {
        *p_minDistance = tempDistance;
        return true;
    }

    return false;
}

struct PointPair getClosestPair(const struct Point points[const])
{
    struct PointPair result =
    {
        .a = points[0],
        .b = points[1]
    };
    double minDistance = getDistance(result);

    struct PointPair tempPair;

    unsigned i, j;

    for (i = 0; i < MAX_POINTS; i++)
    {
        tempPair.a = points[i];

        for (j = 0; j < MAX_POINTS; j++)
        {
            if (i == j)
            {
                continue;
            }

            tempPair.b = points[j];

            if (checkForShorterDistance(tempPair, &minDistance))
            {
                result = tempPair;
            }
        }
    }

    return result;
}

int main(void)
{
    struct Point points[MAX_POINTS];

    readPoints(points);

    struct PointPair pair = getClosestPair(points);

    printf("Closest pair is (%d, %d) and (%d, %d)\n",
           pair.a.x,
           pair.a.y,
           pair.b.x,
           pair.b.y);

    return 0;
}