-2

I 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.

Shamim Hafiz - MSFT
  • 21,454
  • 43
  • 116
  • 176
devan
  • 1,643
  • 8
  • 37
  • 62

1 Answers1

5

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.

btilly
  • 43,296
  • 3
  • 59
  • 88
  • There are an infinite amount of points that are h away from B (assuming a realistic h > AB) and that a line can be drawn perpendicular to AB through A. In fact the set of all those points describes a circle centered around A. – Lance Roberts Jul 10 '11 at 21:34
  • @Lance Roberts In 3 dimensions the set of such points will describe a circle. In 4 dimensions it will form a sphere. But the code given was for 2 dimensions, and then you'll get 2 points. The technique I gave finds those points. – btilly Jul 11 '11 at 01:14
  • Ok, in 2D sure, +1. (though we may never know what the OP really wants). – Lance Roberts Jul 11 '11 at 01:18
  • @btilly Please could you clarify the meaning of `**` in this answer? e.g. `h**2` – Samuel Harmer Jun 11 '12 at 07:47
  • 2
    @Styne666 It means exponentiation. In many C-derived programming languages (eg C, C++, Java, Perl, Ruby, ...) ^ means xor, so ** got used instead for exponentiation. – btilly Jun 11 '12 at 14:23
  • @btilly Ah, but C++ [doesn't seem to have `**`](http://stackoverflow.com/q/3775274/594137). That would be why I've never seen it! Thank you. – Samuel Harmer Jun 11 '12 at 17:24
  • @Styne666 you're right. I just assumed that the math operators in Perl were lifted from C along with the precedence table, but I was wrong. However Java, Perl and Ruby all use ** for exponents. – btilly Jun 11 '12 at 18:13