So I need to know the Distance from a point to a line (in 2D space), given two coordinates of the line (AB).
Here is what I have so far:
public double pointToLineDistance(Point A, Point B, Point P)
{
double normalLength = Math.sqrt((B.x - A.x) * (B.x - A.x) + (B.y - A.y) * (B.y - A.y));
return Math.abs((P.x - A.x) * (B.y - A.y) - (P.y - A.y) * (B.x - A.x)) / normalLength;
}
But I also need to get the coordinates of the point where the perpendicular line intersects with the AB line (it’s ok if it’s outside this segment).
Any ideas?
The idea is to construct an equation of the line going through the points A and B. When you have constructed that equation, you construct an equation of a line that goes through P and is perpendicular to AB. The equation for the perpendicular has coefficients that are easily derived from the equation for the AB-line. Once you have two equations, solving them will give you the coordinate of the intersection.
Is this for homework?