How to calculate the perpendicular to a straight line?
Two points of a straight line are known, for example, {100, 100} and {200, 200}, and there is a point that does not lie on the straight line, for example, {200, 100}. How to determine at what point relative to the straight line will be perpendicular, if you draw a line? The response in the Java code is desirable.
2 answers
In general, here:
double x1 = 100, y1 = 100, x2 = 200, y2 = 200, x3 = 200, y3 = 100;
double x = (x1 * x1 * x3 - 2 * x1 * x2 * x3 + x2 * x2 * x3 + x2 *
(y1 - y2) * (y1 - y3) - x1 * (y1 - y2) * (y2 - y3)) / ((x1 - x2) *
(x1 - x2) + (y1 - y2) * (y1 - y2));
double y = (x2 * x2 * y1 + x1 * x1 * y2 + x2 * x3 * (y2 - y1) - x1 *
(x3 * (y2 - y1) + x2 * (y1 + y2)) + (y1 - y2) * (y1 - y2) * y3) / ((
x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
Are you satisfied with this answer? Simplify it yourself, okay? ...
Update
Once the desire to know HOW sounded... It's simple.
We are looking for a point (x,y) that lies on a straight line through the points (x1, y1) and (x2, y2), and the straight line through the points (x,y) and (x3,y3) is perpendicular to the straight line through the points (x1,y1) and (x2, y2).
The first condition is
Well, and the second-the product of the slopes should give -1 (Equation straight - y = kx + b, and for perpendicular lines k1*k2 = -1):
And then we just solve this system of equations...
First, you need to find the equation of the line to which you want to draw a perpendicular:
In your case, it is simple: y = 1x + 0
(direct y = ax + b
)
Next, the equation of the perpendicular to the line through the point M(x1; y1)
can be found as follows:
Simplify and get y = -x + 300
Solving a system of equations to find the intersection point of the lines:
y = -x + 300
y = x
x = -x + 300
x = 150
y = 150