Find the intersection of two lines: Difference between revisions
Content added Content deleted
(Added Easylang) |
No edit summary |
||
Line 2,280: | Line 2,280: | ||
{{out}} |
{{out}} |
||
<pre>POINT (5 5)</pre> |
<pre>POINT (5 5)</pre> |
||
{{libheader|Pygame}} |
|||
Find the intersection by importing the external [https://www.pygame.org/docs/ PyGame] library. |
|||
<syntaxhighlight lang="python">def segment_intersection(a, b, c, d): |
|||
""" returns a pygame.Vector2 or None if there is no intersection """ |
|||
denom = (a.x - b.x) * (c.y - d.y) - (a.y - b.y) * (c.x - d.x) |
|||
if not denom: |
|||
return |
|||
t = ((a.x - c.x) * (c.y - d.y) - (a.y - c.y) * (c.x - d.x)) / denom |
|||
u = -((a.x - b.x) * (a.y - c.y) - (a.y - b.y) * (a.x - c.x)) / denom |
|||
if 0 <= t <= 1 and 0 <= u <= 1: |
|||
return a.lerp(b, t) |
|||
if __name__ == '__main__': |
|||
a,b = pg.Vector2(4,0), pg.Vector2(6,10) # try (4, 0), (6, 4) |
|||
c,d = pg.Vector2(0,3), pg.Vector2(10,7) # for non intersecting test |
|||
pt = segment_intersection(a, b, c, d) |
|||
print(pt)</syntaxhighlight> |
|||
{{Out}} |
|||
<pre>[5,5]</pre> |
|||
=={{header|Racket}}== |
=={{header|Racket}}== |