Python Line Intersection

Gary Herron gherron at islandtraining.com
Fri Apr 9 11:37:30 EDT 2010


Peyman Askari wrote:
> Hello
>
> This is partly Python related, although it might end up being more 
> math related.
>
> I am using PyGTK (GUI builder for Python) and I need to find the 
> intersection point for two lines. It is easy to do, even if you only 
> have the four points describing line segments 
> (http://www.maths.abdn.ac.uk/~igc/tch/eg1006/notes/node23.html). 
> However, it requires that you solve for two equations. How can I do 
> this in Python, either solve equations, or calculating intersection 
> points some other way?
>
> Cheers
>
>
> Peyman Askari
>

It is purely a math question, having nothing to do with Python.  But 
I'll answer it anyway:

Your problem is equivalent to solving a system of two equations in two 
unknowns.  If you can put those equations in the following form

  a*x + b*y = c
  d*x + e*y = f

then the solution is

  x = (c*e - b*f) / (a*e - b*d) 
  y = (a*f - c*d) / (a*e - b*d)

If the denominator is zero then the lines are parallel, and there is no 
(unique) solution.
(There are other was of solving the system, but they will all amount to 
the same arithmetic, and will, of course, produce the same result.)

Gary Herron



> ------------------------------------------------------------------------



More information about the Python-list mailing list