Given :
Point A (a1,b1)
Point B (a2,b2)
Distance between A and B
Distance between A and C
Angle between AB and AC = 90deg
Have to find :
C(a3,b3)
I have the co-ordinates of 2 points A(x,y) and B(p,q) but want to find the co-ordinates of a third point C(m,n).
I know the distance between A and B, A and C, and the angle between A and C which is 90deg.
I know this is simple Pythagoras theorem. But how do I implement it in php and what would be the formula?
Let $x,$y
and $p,$q
be the given coordinates of A and B, furthermore call $d
the known distance between A and C and $d0
the known distance between A and B. By doing a little math you get the following formulae (here I'm directly implementing it in PHP):
$m = $x + ($q - $y) * $d / $d0;
$n = $y - ($p - $x) * $d / $d0;
There is also a second solution:
$m = $x - ($q - $y) * $d / $d0;
$n = $y + ($p - $x) * $d / $d0;
EDIT: Here is how I got the equations: I rotated the vector AB, which has the coordinates ($p - $x, $q - $y)
, by 90 degrees to obtain ($q - $y, -($p - $x))
and (-($q - $y), $p - $x)
(depending whether clockwise or counter-clockwise) and then got the vector AC by scaling it with $d / $d0
(the ratio of their lengths). Now I just translated the vector by ($x, $y)
to get ($m, $n)
.
Maybe this can be implemented more elegantly by using a vector class in PHP or even a whole library, but I think for this simple calculation it is much easier to implement it "by hand".