【问题标题】:Declare and define the function distance() to find the Euclidean distance between the two points (x[0], y[0]) and (x[1], y[1]) in c声明并定义函数distance()求c中两点(x[0], y[0])和(x[1], y[1])之间的欧式距离
【发布时间】:2014-10-31 01:49:49
【问题描述】:

声明并定义函数 distance() 以求欧几里得距离: √((x1 - x2)² + (y1 - y2)²) 在 (x[0], y[0]) 和 (x[1], y[1]) 两点之间 这个函数应该只计算并返回答案。

C程序-----

双倍距离(双倍 x[],双倍 y[]);

我还应该放什么。我是在这个函数中包含欧几里德距离还是创建一个新的?

【问题讨论】:

  • double distance(double x[], double y[]){return hypot(x[1]-x[0],y[1]-y[0]);}

标签: c euclidean-distance


【解决方案1】:
double distance(double x[], double y[]);

是函数声明。

double distance(double x[], double y[]) {
    //Write code here that returns a double
}

是函数定义。

看起来问题要你同时做。

【讨论】:

  • 如何包含欧式距离公式 distance = sqrt(pow(x[0] + x[1], 2) + pow(y[0]-y[1]2);
  • 首先您需要尝试自己编写代码。之后,如果您遇到任何问题,请发布一个新问题,其中包括您编写的代码、有问题的行以及具体问题是什么。请勿将其粘贴到此帖子上,因为我们希望每个帖子保留一个问题。
  • 否 -- 不要发布新问题。更好的是,只需编辑此问题并将新信息添加为原始信息的附录。编写代码后,编译它。您需要在您的gcc 命令中包含数学库-lm。 (即gcc -Wall -Wextra -o myprogram myprogram.c -lm
【解决方案2】:

这是最简单的distance()函数定义:

`double distance(double x, double y){
    return 1/sqrt(((x[0]-x[1])*(x[0]-x[1])+(y[0]-y[1])*(y[0]-y[1])));
}`

请注意,如果您使用它来比较距离,则使用距离的平方最快。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-01
    • 2018-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-09
    • 2021-06-19
    相关资源
    最近更新 更多