Find the intersection of two lines: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 2,285:
<syntaxhighlight lang="python">def segment_intersection(a, b, c, d):
""" returns a pygame.Vector2 or None if there is no intersection """
denomab, cd, ac = (a.x - b.x), * (c.y - d.y), - (a.y - b.y) * (c.x - d.x)
if not (denom:= ab.x * cd.y - ab.y * cd.x):
return
 
t = ((aac.x - c.x) * (ccd.y - dac.y) - (a.y - c.y) * (c.x - dcd.x)) / denom
u = -((aab.x - b.x) * (aac.y - cab.y) - (a.y - b.y) * (a.x - cac.x)) / denom
if 0 <= t <= 1 and 0 <= u <= 1:
return a.lerp(b, t)
3

edits