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!