英文:
Distance Between a point to a line formed by two points
问题
我试图找到从点3(newPoint)到由点1(prevPoint)和点2(curPoint)形成的直线的垂直距离在C++中。
我之前使用了这个公式。但现在在一些交叉检查后,我对我的程序的正确性产生了疑问。
编辑:
所有点(prevPoint、curPoint和newPoint)都是Point类型的。
struct Point {
string name;
int x;
int y;
bool visited;
};
double d = distance(prevPoint, curPoint);
double dx = (curPoint.x - prevPoint.x) / d;
double dy = (curPoint.y - prevPoint.y) / d;
double distToLine = abs((newPoint.x - prevPoint.x) * dy - (newPoint.y - prevPoint.y) * dx);
distance
函数:
double distance(Point a, Point b) {
double dx = a.x - b.x;
double dy = a.y - b.y;
return sqrt(dx * dx + dy * dy);
}
我的代码正确吗?如果不正确,我需要一些正确的公式。谢谢!
英文:
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
struct Point {
string name;
int x;
int y;
bool visited;
};
double d = distance(prevPoint, curPoint);
double dx = (curPoint.x - prevPoint.x) / d;
double dy = (curPoint.y - prevPoint.y) / d;
double distToLine = abs((newPoint.x - prevPoint.x) * dy - (newPoint.y - prevPoint.y) * dx);
The distance
function:
double distance(Point a, Point b) {
double dx = a.x - b.x;
double dy = a.y - b.y;
return sqrt(dx * dx + dy * dy);}
Is my code correct? I need some correct formula if its not. Thanks!
答案1
得分: 3
以下是您要翻译的内容:
double d = distance(prevPoint, curPoint);
double area = abs((curPoint.x - prevPoint.x) * (newPoint.y - prevPoint.y) - (curPoint.y - prevPoint.y) * (newPoint.x - prevPoint.x));
double distToLine = area / d;
英文:
The formula would be as follows in your case:
double d = distance(prevPoint, curPoint);
double area = abs((curPoint.x - prevPoint.x) * (newPoint.y - prevPoint.y) - (curPoint.y - prevPoint.y) * (newPoint.x - prevPoint.x));
double distToLine = area / d;
答案2
得分: 0
在以下测试代码中,你的代码与我的代码产生了相同的结果。
struct Point
{
int x, y;
Point(int x = 0, int y = 0) : x(x), y(y) {}
};
double MyMethod(Point prevPoint, Point curPoint, Point newPoint)
{
// 为简单起见,解决问题时使用以prevPoint为原点的坐标系。
Point B(curPoint.x - prevPoint.x, curPoint.y - prevPoint.y);
Point C(newPoint.x - prevPoint.x, newPoint.y - prevPoint.y);
// 将矢量P表示为
// P = t * B - C
// 其中,t是标量。
//
// 我们需要做的是找到最小化此P长度的t值。
// 目标函数是:
// Err(t) = (P点积 P) = (t * Bx - Cx)^2 + (t * By - Cy)^2
// 然后
// dErr(t)/dt = 2 * (t * Bx - Cx) * Bx + 2 * (t * By - Cy) * By = 0
// 因此
// t = (CxBx + CyBy) / (BxBx + ByBy)
double t = (C.x * B.x + C.y * B.y) / double(B.x * B.x + B.y * B.y);
// 现在我们要获得的距离是P的长度(使用计算出的t值)。
double Px = t * B.x - C.x;
double Py = t * B.y - C.y;
return sqrt(Px * Px + Py * Py);
}
namespace YourCode
{
double distance(Point a, Point b)
{
double dx = a.x - b.x;
double dy = a.y - b.y;
return sqrt(dx * dx + dy * dy);
}
double YourMethod(Point prevPoint, Point curPoint, Point newPoint)
{
double d = distance(prevPoint, curPoint);
double dx = (curPoint.x - prevPoint.x) / d;
double dy = (curPoint.y - prevPoint.y) / d;
double distToLine = abs((newPoint.x - prevPoint.x) * dy - (newPoint.y - prevPoint.y) * dx);
return distToLine;
}
}
int main(int argc, char *argv[])
{
Point A(3, 10);
Point B(-5, 22);
Point C(1, 4);
std::cout << YourCode::YourMethod(A, B, C) << std::endl;
std::cout << MyMethod(A, B, C) << std::endl;
return 0;
}
英文:
In the following test code, your code had the same results as mine.
struct Point
{
int x,y;
Point( int x=0, int y=0 ) : x(x),y(y) {}
};
double MyMethod( Point prevPoint, Point curPoint, Point newPoint )
{
//For simplicity, solve the problem in a coordinate system with the origin at prevPoint.
Point B( curPoint.x-prevPoint.x, curPoint.y-prevPoint.y );
Point C( newPoint.x-prevPoint.x, newPoint.y-prevPoint.y );
// Considering the Vector P as
// P = t*B - C
// where, t is scalar.
//
// What we need to do is find the value of t that minimizes the length of this P.
// Objective Function is :
// Err(t) = (P dot P) = (t*Bx - Cx)^2 + (t*By - Cy)^2
// And Then
// dErr(t)/dt = 2*(t*Bx - Cx)Bx + 2*(t*By - Cy)By = 0
// So
// t = (CxBx + CyBy) / (BxBx + ByBy)
double t = ( C.x*B.x + C.y*B.y ) / double(B.x*B.x + B.y*B.y);
// Now the distance we want to obtain is length of P (with the calculated t value).
double Px = t*B.x - C.x;
double Py = t*B.y - C.y;
return sqrt( Px*Px + Py*Py );
}
namespace YourCode
{
double distance(Point a, Point b)
{
double dx = a.x - b.x;
double dy = a.y - b.y;
return sqrt(dx * dx + dy * dy);
}
double YourMethod( Point prevPoint, Point curPoint, Point newPoint )
{
double d = distance(prevPoint, curPoint);
double dx = (curPoint.x - prevPoint.x) / d;
double dy = (curPoint.y - prevPoint.y) / d;
double distToLine = abs((newPoint.x - prevPoint.x) * dy - (newPoint.y - prevPoint.y) * dx);
return distToLine;
}
}
int main(int argc, char *argv[])
{
Point A( 3,10 );
Point B( -5, 22 );
Point C( 1,4 );
std::cout << YourCode::YourMethod(A,B,C) << std::endl;
std::cout << MyMethod(A,B,C) << std::endl;
return 0;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论