【问题标题】:Point on a straight line specific distance away in C点在 C 中特定距离的直线上
【发布时间】:2012-04-11 22:03:48
【问题描述】:

如何找到与给定点相距特定距离的直线上的点。我正在用 C 编写这段代码,但我没有得到正确的答案。谁能指导我做错了什么。

我得到了 x1,y1,x2,y2 的值​​和剩下的距离。使用这些我可以找到斜率 m 和 y 截距也很好。 现在,我需要在连接这两个点的直线上找到距离点 x1,y1 10 个单位的点。我似乎在这里出错了。这是我写的代码。

int x1 = node[n].currentCoordinates.xCoordinate;
int y1 = node[n].currentCoordinates.yCoordinate;
int x2 = node[n].destinationLocationCoordinates.xCoordinate;
int y2 = node[n].destinationLocationCoordinates.yCoordinate;

int distanceleft = (y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1);
distanceleft = sqrt(distanceleft);
printf("Distance left to cover is %d\n",distanceleft);
int m = (y2 - y1)/(x2 - x1); // slope.
int b = y1 - m * x1; //y-intercept


//find point on the line that is 10 units away from
//current coordinates on equation y = mx + b.
if(x2 > x1)
{
     printf("x2 is greater than x1\n");
     int tempx = 0;
     int tempy = 0;
     for(tempx = x1; tempx <= x2; tempx++)
     {
          tempy = y1 + (y2 - y1) * (tempx - x1)/(x2 - x1);
          printf("tempx = %d, tempy = %d\n",tempx,tempy);
          int distanceofthispoint = (tempy - y1) * (tempy - y1) + (tempx - x1) * (tempx - x1);
          distanceofthispoint = sqrt((int)distanceofthispoint);
          if(distanceofthispoint >= 10)
          {
               //found new points.
               node[n].currentCoordinates.xCoordinate = tempx;
               node[n].currentCoordinates.yCoordinate = tempy;
               node[n].TimeAtCurrentCoordinate = clock;
               printf("Found the point at the matching distance\n");
               break;
          }
     }
}
else
{
     printf("x2 is lesser than x1\n");
     int tempx = 0;
     int tempy = 0;
     for(tempx = x1; tempx >= x2; tempx--)
     {
          tempy = y1 + (y2 - y1) * (tempx - x1)/(x2 - x1);
          printf("tempx = %d, tempy = %d\n",tempx,tempy);
          int distanceofthispoint = (tempy - y1) * (tempy - y1) + (tempx - x1) * (tempx - x1);
          distanceofthispoint = sqrt((int)distanceofthispoint);
          if(distanceofthispoint >= 10)
          {
               //found new points.
               node[n].currentCoordinates.xCoordinate = tempx;
               node[n].currentCoordinates.yCoordinate = tempy;
               node[n].TimeAtCurrentCoordinate = clock;
               printf("Found the point at the matching distance\n");
               break;
          }
     }
}
printf("at time %f, (%d,%d) are the coordinates of node %d\n",clock,node[n].currentCoordinates.xCoordinate,node[n].currentCoordinates.yCoordinate,n);

【问题讨论】:

    标签: c 2d euclidean-distance


    【解决方案1】:

    数学是这样的,我没有时间用 C 写东西。

    你有一个点 (x1,y1) 和另一个点 (x2,y2),当链接时它会给你一个段。

    因此你有一个方向向量v=(xv, yv),其中xv=x2-x1yv=y2-y1

    现在,您需要将该向量除以其范数,得到一个新向量:vector = v / sqrt(xv2 + yv2) .

    现在,您只需将向量乘以您想要的点的距离添加到您的原点:

    位置=(x原点,y原点)+距离×向量

    我希望这会有所帮助!

    【讨论】:

    • 同意。我不认为你真的需要一个循环。这是代数,不是算法,真的。
    • 向量方程中的最后一个v 应该在sqrt 之下、分数之下还是分数之外?你忘了关闭括号:)
    • 哦,好吧,没关系。我明白。它在外面,你只是将 v 乘以 1/.. 而不是 v/...
    【解决方案2】:

    或者更简单,

    从斜坡上找出角度

    θ = arctan(y2-y1/x2-x1)
    

    您可能希望根据斜率的分子和分母来修改 θ 的象限。然后你可以在距离(x1,y1)的距离为d的线上找到任何点

    x_new = x1 + d×cos(θ)
    y_new = y1 + d×sin(θ)
    

    在这种情况下,你有 d=10

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-03
      • 1970-01-01
      • 2019-10-31
      • 1970-01-01
      相关资源
      最近更新 更多