Skip to content

Instantly share code, notes, and snippets.

@rightson
Last active December 14, 2015 13:59
Show Gist options
  • Save rightson/5097696 to your computer and use it in GitHub Desktop.
Save rightson/5097696 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
def cross_product(p1, p2, p3):
return ((p1[0] - p3[0]) * (p2[1] - p3[1])) - ((p2[0] - p3[0]) * (p1[1] - p3[1]))
def point_in_triangle(s, p1, p2, p3):
b1 = cross_product(s, p1, p2) < 0
b2 = cross_product(s, p2, p3) < 0
b3 = cross_product(s, p3, p1) < 0
return (b1 == b2 and b2 == b3)
if __name__ == "__main__":
print point_in_triangle([10, 10], [0, 0], [3, 0], [0, 4])
print point_in_triangle([1, 1], [0, 0], [3, 0], [0, 4])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment