Point Within Triangle

Published by Colin Brash in

Create a function that takes a triangle and a test point. Return true if the test point lies within the triangle, false if it doesn't.

Examples

withinTriangle(triangle: [(1, 4), (5, 6), (6, 1)], testPoint: (4, 5)) ➞ true

withinTriangle(triangle: [(1, 4), (5, 6), (6, 1)], testPoint: (3, 2)) ➞ false

withinTriangle(triangle: [(-6, 2), (-2, -2), (8, 4)], testPoint: (4, 2)) ➞ true

Notes

  • Points are given as tuples containing two integer values.
  • The triangle is given as an array of three Points.
Watch a quick demo on how Edabit works.