Geometry 4: Line Segments Intersection

Published by Mateusz Mędrowski in

Write a function that takes coordinates of four points (A, B, C, D) on a two-dimensional plane and returns true if the segment AB intersects segment CD.

Examples

intersection(arr[0]={2, 4}, arr[1]={5, 1}, arr[2]={4, 1}, arr[3]={4, 4}) ➞ true

intersection(arr[0]={1, 2}, arr[1]={3, 4}, arr[2]={5, 6}, arr[3]={7, 8}) ➞ false

intersection(arr[0]={1, 1}, arr[1]={1, 7}, arr[2]={-3, 3}, arr[3]={6, 3}) ➞ true

Notes

  • The given points always create two line segments.
  • The order of the given numbers is X, Y.
  • The numbers in the argument array can be positive or negative.
  • This challenge isn't easy, but don't give up!

Check my profile for other challenges in the series.

Watch a quick demo on how Edabit works.