How to find the intersection point of two lines? [Solved!]

2D geometry question: How do I find in Python the intersection point (if any) of two lines with endpoints (x,y) in 2D:

line1=((x1,y1), (x2, y2))
line2=((X1,Y1), (X2, Y2))

You could try to use http://www.blender.org/documentation/248PythonDoc/Mathutils-module.html#LineIntersect . If returned vectors are the same, you know the lines may intersect between given endpoints. You have to check if this intersection point is between both lines separately.

Actually I wrote this question to a bit wrong area, because my problem is in a Python script outside Blender, (standalone), so I can’t use the functions Blender gives. Til now I have found this

http://www.pygame.org/wiki/IntersectingLineDetection

but now I can’t find out why it always gives only integers as result. (Floating points needed)

go to wikipedia

and search line intersection
you should find the equations giving the XY intersection
of 2 lines

Salutations

Are you passing floating point values as your points? Python casts a value as an integer if there is no decimal precision specified, it casts it as a float if there are values to the right of a decimal.

pt1 = 1 # this will be an integer
pt2 = 1.0 #this will be a float

I’m trying this kind of arbitrary numbers with the script:


p11 = (472.1, 110.34)
p12 = (542.356, 117.646)
p13 = (500.24, 100.45)
p14 = (507.98, 150.77)
# but, ...
print calculateIntersectPoint(p11, p12, p13, p14)

gives still [502, 113]

ahh. i feel dumb. I tested the script and had the same results. It didn’t make sense. I then actually read the code (should do that first!).

Here, comment line 140:
from:
point = [int(pp) for pp in point]
to
#point = [int(pp) for pp in point]

Thank you, it works like a dream!

Hi ,
I am getting error because of the statement in Line 120 to 132
if r1.width < tolerance:

I am getting error because of " ; " in the if statement as invalid syntax.

Please guide me

This is quite old thread, you should refer to the latest API docs:

http://www.blender.org/documentation/blender_python_api_2_70a_release/mathutils.geometry.html#mathutils.geometry.intersect_line_line