find a point which perpendicular to given line [closed]
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question开发者_如何学CI want to find a point z(x3,y3) which perpendicular to given line. In my example I am given 2 coordinates A(x1 , y1) and B(x2 , y2). I want to find the point z which is perpendicular(AZ) to AB line and distance (h) from point B. ABZ angle is 90. here is my c++ code.
double AB_slope = m; // know it
//find z point which perpendicular to AB line
double AZ_slope = - 1/m;
double x3 = x2 + prescribed_distance * dx;
double y3 = y2 + prescribed_distance * dy;
But I don't know to find dx , dy and prescribed_distance. please help me.
Let me rephrase your question to be what I think it is, then answer it.
You're given points A = (x1, y1)
and B = (x2, y2)
. You want to find a point Z = (x3, y3)
such that AZ
is perpendicular to AB
, and BZ
has length h
.
The vector from A
to B
is v = (x2 - x1, y2 - y1)
. An easy to calculate perpendicular vector to that one is w = (y2 - y1, x1 - x2)
. The line crossing through A
which is perpendicular to AB
is represented by F(s) = A + s*w = (x1 + s*(y2 - y1), y1 + s*(x1 - x2))
as s
ranges over the real numbers. So we need to pick a value s
such that F(s)
is h
away from B
.
From the Pythagorean theorem, the square of the length from F(s)
to B
is always going to be the square of the distance from F(s)
to A
, plus the square of the distance from A
to B
. From which we get the messy expression that we want:
h**2 = s**2 * ((y2 - y1)**2 + (x1-x2)**2) + ((x1 - x2)**2 + (y1 - y2)**2))
= s**2 * ((x1 - x2)**2 + (y1 - y2)**2)) + ((x1 - x2)**2 + (y1 - y2)**2))
= (s**2 + 1) * ((x1 - x2)**2 + (y1 - y2)**2))
(s**2 + 1) = h**2 / ((x1 - x2)**2 + (y1 - y2)**2))
s**2 = h**2 / ((x1 - x2)**2 + (y1 - y2)**2)) - 1
s = sqrt(h**2 / ((x1 - x2)**2 + (y1 - y2)**2)) - 1)
Now plug that expression for s
back into F(s) = (x1 + s*(y2 - y1), y1 + s*(x1 - x2))
and you have your point Z
. And the other possible answer is the same distance on the other side.
精彩评论