POV-Ray : Newsgroups : povray.general : Looking for a formula : Re: Looking for a formula Server Time
29 Jul 2024 12:27:28 EDT (-0400)
  Re: Looking for a formula  
From: Trevor G Quayle
Date: 15 Feb 2013 09:25:01
Message: <web.511e451524d8606e81c811d20@news.povray.org>
"H. Karsten" <h-karsten()web.de> wrote:
> Hi people :)
> I have two lines and like to have there intersection-point.
>
> Like
> <Line_1_Start_X,Line_1_Start_y> to <Line_1_End_X,Line_1_End_Y>
> and
> <Line_2_Start_X,Line_2_Start_y> to <Line_2_End_X,Line_2_End_Y>
>
> And I like to get <Intersection_X,Intersection_Y>
>
> Is there any formula, coming with the PovRay-Inc-Files, like Math.inc, I can
> use?
>
> Or does someone have a quick way to solve this?
>
> Tanx,
> Holger :)

OK, if it is a pair of 2d lines, you just need to solve for the two line
equations, then solve for the intersection:

General 2-d line equation is "y = mx + b"

LINE 1
     P11 = <x11,y11>
     P12 = <x12,y12>

Solve slope (m):
     m1 = (y12-y11)/(x12-x11)

Solve intercept (b):
     b1 = y11 - m1 * x11

LINE 2
     P21 = <x21,y21>
     P22 = <x22,y22>

Solve slope (m):
     m2 = (y22 - y21) / (x22 - x21)

Solve intercept (b):
     b2 = y21 - m2 * x21

Now solve the two line equatins for 'x' and 'y':

xi = (b2 - b1) / (m1 - m2)
yi = m1 * xi + b1

Intersection point: Pi = <xi,yi>


Or to really simplify it, we can combine all of the above and get formulae to
solve directly for x and y from the points:

xi =
[(x21-x22)*(x11*y12-x12*y11)+(x11-x12)*(x22*y21-x21*y22)]/[(x21-x22)*(y12-y11)+(x11-x12)(y21-y22)]

yi = (xi-x11)*(y12-y11)/(x12-x11)+y11


As I have already noted, this only works in 2-space.  In 3-space (x,y,z) it is
not as simple, lines have parametric equations, rather than direct functions
(surfaces like planes have functions), and lines will not always intersect, in
fact it would be very rare that that do: imagine picking 2 sets of random points
in a room and tying a piece of string between each, they are not likely to
intersect.  In 3-space, the best equivalent is the closest points between the
lines.  This can also be solved using algebraic equations (need to solve for
various plane functions, and find the intersection of three planes) which I am
not going to go into at present.  Note: if the closest distance between the two
points is 0, they actually intersect (i.e., they are the same point
coordinates).

-tgq
-tgq


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.