Python Line Intersection
John Nagle
nagle at animats.com
Fri Apr 9 14:43:52 EDT 2010
Chris Rebert wrote:
> On Fri, Apr 9, 2010 at 8:04 AM, Peyman Askari <peter_peyman_puk at yahoo.ca> 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?
>
>
> Just solve the equations ahead of time by using generic ones.
>
> Given:
> y = mx + b
> y = nx + c
>
> We set them equal and solve for x:
> mx + b = nx + c
> mx - nx = c - b
> (m-n)x = c - b
> x = (c - b) / (m-n)
Actually, you don't want to do it that way, because it fails for vertical
lines, when m and n go to infinity.
See Wikipedia for the usual solution, given points on both lines:
http://en.wikipedia.org/wiki/Line-line_intersection
This is done all the time in computer graphics work.
John Nagle
More information about the Python-list
mailing list