Skip to content

Instantly share code, notes, and snippets.

@zv
Last active December 6, 2021 20:08
Show Gist options
  • Save zv/121d192ad8c860c113f743f50e71455d to your computer and use it in GitHub Desktop.
Save zv/121d192ad8c860c113f743f50e71455d to your computer and use it in GitHub Desktop.
Check if point is inside triangle
from numpy.linalg import det
def solve(v, v0, v1, v2):
"See: https://mathworld.wolfram.com/TriangleInterior.html"
x = (det([v, v2]) - det([v0, v2])) / det([v1, v2])
y = -((det([v, v1]) - det([v0, v1])) / det([v1, v2]))
return x, y
for x, y in ([5, 2.5], [6, 2.5], [5, 12], [6, 12]):
a, b = solve(v=[x, y], v0 = [0, 0], v1 = [5, 10], v2 = [10, 0])
print(f'x={x}, y={y}\ta={a:.05}, b={b:0.5}\ta, b > 0 = {a > 0 and b > 0} \ta+b < 1={a+b < 1}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment