Reputation: 149
I am trying to find the point of intersection between a line the altitude of a point to that line. So given the information, two points that create a line and one point to draw the altitude from, I want to find the point on the line that is closest to the point, (so a point on the line that forms a perpendicular line with the given point.)
(hope that made sense without an illistration)
given a line and a point, how would I find this new Point?
public static Point2D findPointOnTwoLines(Line2D line, Point2D point) {
double slope = line.getSlope();
double oppRec = line.getOppRecip(); // returns the opposite reciprocal of the slope
double x = ((slope * line.getPoint1().getX()) - (line.getPoint1().getY()))
/ ((slope) - (oppRec));
double y = (slope * ((oppRec * point.getX()) - point.getY())) - (oppRec * ((slope * point.getX()) - point.getY()))
/ ((slope) - (oppRec));
return new Point2D(x, y);
}
this is my attempt at solving the equation using determinents, and it failed to give the right coordinate when I passed:
Line2D line = new Line2D(2, 3, 1, 1); // line from (2, 3) to (1, 1)
Point2D point = new Point2D(0, 2);
if you know how to find the right point using this method, (or any other method for that matter) it would be greatly appreciated!!
ASKII art of what I mean, if you can make a real image of this and send it to me I would be happy to use that instead.
1
\ 3
\ /
\ / the number points are thrown in as arguments, and the "X" point is returned
\ /
X <---- this is a 90 degree angle
\
\ the "X" is suppose to represent the point on the line closest to the point "3"
\
2
Upvotes: 0
Views: 326
Reputation: 37645
double x = (point.getY() - oppRec * point.getX() - line.getPoint1().getY() + slope * line.getPoint1().getX()) / (slope - oppRec);
double y = oppRec * x + point.getY() - oppRec * point.getX();
Upvotes: 1