First express your lines like a1*(x-x0)+b1*(y-y0)=0
and a2*(x-x0)+b2*(y-y0)=0
and their intersection x0
,y0
is accounted for already in the equations.
updated signs
The transformation from x
,y
to z
,w
is
z = -sqrt(a1^2+b1^2)*(a2*(x-x0)+b2*(y-y0))/(a2*b1-a1*b2) w = sqrt(a2^2+b2^2)*(a1*(x-x0)+b1*(y-y0))/(a1*b2-a2*b1)
and the inverse
x = x0 - b1*z/sqrt(a1^2+b1^2) + b2*w/sqrt(a2^2+b2^2) y = y0 + a1*z/sqrt(a1^2+b1^2) - a2*w/sqrt(a2^2+b2^2)
It would be helpful to scale the coefficients such that sqrt(a1^2+b1^2)=1
and sqrt(a2^2+b2^2)=1
.
Note that this works for non-orthogonal lines also. As long as they are not parallel and a2*b1-a1*b2!=0
it is going to work.
Example
The z
line (-2)*(x-3) + (1)*(y-1) = 0
and w
line (-1)*(x-3) + (-4)*(y-1) = 0
meet at (3,1)
. The coefficients are thus a1=-2
, b1=1
, a2=-1
, b2=-4
.
The coordinates (x,y)=(2,1)
transform to
z = -sqrt((-2)^2+1^2) ((-1) (x-3)+(-4) (y-1))/((-1) 1-(-2) (-4)) = 0.2484
w = sqrt((-1)^2+(-4)^2) ((-2) (x-3)+1 (y-1))/((-2) (-4)-(-1) 1) = 0.9162
With the inverse
x = -1 z/sqrt((-2)^2+1^2)+(-4) w/sqrt((-1)^2+(-4)^2)+3 = 2
y = (-2) z/sqrt((-2)^2+1^2)-(-1) w/sqrt((-1)^2+(-4)^2)+1 = 1
Development
For a line a1*(x-x0)+b1*(y-y0)=0
the direction vector along the line is e1 = [e1x,e1y]= [-b1/sqrt(a1^2+b1^2),a1/sqrt(a1^2+b1^2)]
. Similarly for the other line.
The screen coordinates of a local point [z,w]
is found by starting at the origin x0
, y0
and moving by z
along the first line and then by w
along the second line. So
x = x0 + e1x*z + e2x*w = x0 -b1/sqrt(a1^2+b1^2)*z - b2/sqrt(a2^2+b2^2)*w
y = y0 + e1y*z + e2y*w = y0 +a1/sqrt(a1^2+b1^2)*z + a2/sqrt(a2^2+b2^2)*w
Now I need to flip the direction of the second line to make it work per the original posting visualization, by reversing the sign of w
.
To find z
, w
from x
, y
invert the above two equations.