点到由两点形成的直线的距离

huangapple go评论99阅读模式
英文:

Distance Between a point to a line formed by two points

问题

我试图找到从点3(newPoint)到由点1(prevPoint)和点2(curPoint)形成的直线的垂直距离在C++中。

我之前使用了这个公式。但现在在一些交叉检查后,我对我的程序的正确性产生了疑问。

编辑:
所有点(prevPoint、curPoint和newPoint)都是Point类型的。

  1. struct Point {
  2. string name;
  3. int x;
  4. int y;
  5. bool visited;
  6. };
  7. double d = distance(prevPoint, curPoint);
  8. double dx = (curPoint.x - prevPoint.x) / d;
  9. double dy = (curPoint.y - prevPoint.y) / d;
  10. double distToLine = abs((newPoint.x - prevPoint.x) * dy - (newPoint.y - prevPoint.y) * dx);

distance 函数:

  1. double distance(Point a, Point b) {
  2. double dx = a.x - b.x;
  3. double dy = a.y - b.y;
  4. return sqrt(dx * dx + dy * dy);
  5. }

我的代码正确吗?如果不正确,我需要一些正确的公式。谢谢!

英文:

I am trying to find the perpendicular distance from point 3 (newPoint) to a line formed by point 1 (prevPoint) and point 2 (curPoint) in C++.

I was using this formula previously. But now I am questioning the correctness of my program upon some cross-checking.

EDIT:
all the points(prevPoint, curPoint and newPoint) is of Point type

  1. struct Point {
  2. string name;
  3. int x;
  4. int y;
  5. bool visited;
  6. };
  7. double d = distance(prevPoint, curPoint);
  8. double dx = (curPoint.x - prevPoint.x) / d;
  9. double dy = (curPoint.y - prevPoint.y) / d;
  10. double distToLine = abs((newPoint.x - prevPoint.x) * dy - (newPoint.y - prevPoint.y) * dx);

The distance function:

  1. double distance(Point a, Point b) {
  2. double dx = a.x - b.x;
  3. double dy = a.y - b.y;
  4. return sqrt(dx * dx + dy * dy);}

Is my code correct? I need some correct formula if its not. Thanks!

答案1

得分: 3

以下是您要翻译的内容:

  1. double d = distance(prevPoint, curPoint);
  2. double area = abs((curPoint.x - prevPoint.x) * (newPoint.y - prevPoint.y) - (curPoint.y - prevPoint.y) * (newPoint.x - prevPoint.x));
  3. double distToLine = area / d;
英文:

The formula would be as follows in your case:

  1. double d = distance(prevPoint, curPoint);
  2. double area = abs((curPoint.x - prevPoint.x) * (newPoint.y - prevPoint.y) - (curPoint.y - prevPoint.y) * (newPoint.x - prevPoint.x));
  3. double distToLine = area / d;

答案2

得分: 0

在以下测试代码中,你的代码与我的代码产生了相同的结果。

  1. struct Point
  2. {
  3. int x, y;
  4. Point(int x = 0, int y = 0) : x(x), y(y) {}
  5. };
  6. double MyMethod(Point prevPoint, Point curPoint, Point newPoint)
  7. {
  8. // 为简单起见,解决问题时使用以prevPoint为原点的坐标系。
  9. Point B(curPoint.x - prevPoint.x, curPoint.y - prevPoint.y);
  10. Point C(newPoint.x - prevPoint.x, newPoint.y - prevPoint.y);
  11. // 将矢量P表示为
  12. // P = t * B - C
  13. // 其中,t是标量。
  14. //
  15. // 我们需要做的是找到最小化此P长度的t值。
  16. // 目标函数是:
  17. // Err(t) = (P点积 P) = (t * Bx - Cx)^2 + (t * By - Cy)^2
  18. // 然后
  19. // dErr(t)/dt = 2 * (t * Bx - Cx) * Bx + 2 * (t * By - Cy) * By = 0
  20. // 因此
  21. // t = (CxBx + CyBy) / (BxBx + ByBy)
  22. double t = (C.x * B.x + C.y * B.y) / double(B.x * B.x + B.y * B.y);
  23. // 现在我们要获得的距离是P的长度(使用计算出的t值)。
  24. double Px = t * B.x - C.x;
  25. double Py = t * B.y - C.y;
  26. return sqrt(Px * Px + Py * Py);
  27. }
  28. namespace YourCode
  29. {
  30. double distance(Point a, Point b)
  31. {
  32. double dx = a.x - b.x;
  33. double dy = a.y - b.y;
  34. return sqrt(dx * dx + dy * dy);
  35. }
  36. double YourMethod(Point prevPoint, Point curPoint, Point newPoint)
  37. {
  38. double d = distance(prevPoint, curPoint);
  39. double dx = (curPoint.x - prevPoint.x) / d;
  40. double dy = (curPoint.y - prevPoint.y) / d;
  41. double distToLine = abs((newPoint.x - prevPoint.x) * dy - (newPoint.y - prevPoint.y) * dx);
  42. return distToLine;
  43. }
  44. }
  45. int main(int argc, char *argv[])
  46. {
  47. Point A(3, 10);
  48. Point B(-5, 22);
  49. Point C(1, 4);
  50. std::cout << YourCode::YourMethod(A, B, C) << std::endl;
  51. std::cout << MyMethod(A, B, C) << std::endl;
  52. return 0;
  53. }
英文:

In the following test code, your code had the same results as mine.

  1. struct Point
  2. {
  3. int x,y;
  4. Point( int x=0, int y=0 ) : x(x),y(y) {}
  5. };
  6. double MyMethod( Point prevPoint, Point curPoint, Point newPoint )
  7. {
  8. //For simplicity, solve the problem in a coordinate system with the origin at prevPoint.
  9. Point B( curPoint.x-prevPoint.x, curPoint.y-prevPoint.y );
  10. Point C( newPoint.x-prevPoint.x, newPoint.y-prevPoint.y );
  11. // Considering the Vector P as
  12. // P = t*B - C
  13. // where, t is scalar.
  14. //
  15. // What we need to do is find the value of t that minimizes the length of this P.
  16. // Objective Function is :
  17. // Err(t) = (P dot P) = (t*Bx - Cx)^2 + (t*By - Cy)^2
  18. // And Then
  19. // dErr(t)/dt = 2*(t*Bx - Cx)Bx + 2*(t*By - Cy)By = 0
  20. // So
  21. // t = (CxBx + CyBy) / (BxBx + ByBy)
  22. double t = ( C.x*B.x + C.y*B.y ) / double(B.x*B.x + B.y*B.y);
  23. // Now the distance we want to obtain is length of P (with the calculated t value).
  24. double Px = t*B.x - C.x;
  25. double Py = t*B.y - C.y;
  26. return sqrt( Px*Px + Py*Py );
  27. }
  28. namespace YourCode
  29. {
  30. double distance(Point a, Point b)
  31. {
  32. double dx = a.x - b.x;
  33. double dy = a.y - b.y;
  34. return sqrt(dx * dx + dy * dy);
  35. }
  36. double YourMethod( Point prevPoint, Point curPoint, Point newPoint )
  37. {
  38. double d = distance(prevPoint, curPoint);
  39. double dx = (curPoint.x - prevPoint.x) / d;
  40. double dy = (curPoint.y - prevPoint.y) / d;
  41. double distToLine = abs((newPoint.x - prevPoint.x) * dy - (newPoint.y - prevPoint.y) * dx);
  42. return distToLine;
  43. }
  44. }
  45. int main(int argc, char *argv[])
  46. {
  47. Point A( 3,10 );
  48. Point B( -5, 22 );
  49. Point C( 1,4 );
  50. std::cout &lt;&lt; YourCode::YourMethod(A,B,C) &lt;&lt; std::endl;
  51. std::cout &lt;&lt; MyMethod(A,B,C) &lt;&lt; std::endl;
  52. return 0;
  53. }

huangapple
  • 本文由 发表于 2023年2月27日 15:19:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75577663.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定