Skip to content

Instantly share code, notes, and snippets.

@arnar
Created November 23, 2011 19:07
Show Gist options
  • Save arnar/1389581 to your computer and use it in GitHub Desktop.
Save arnar/1389581 to your computer and use it in GitHub Desktop.
# Project Euler problem 102
# Which side of the line AB is the point P?
# (The dot product of AP and the perpendicular to AB)
def side((ax,ay), (bx,by), (px,py)):
return (bx - ax) * (py - ay) - (by - ay) * (px - ax)
def same_side(a, b, p, q):
sp = side(a, b, p)
sq = side(a, b, q)
return (sp >= 0 and sq >= 0) or (sp <= 0 and sq <= 0)
def contains_origin(a, b, c):
sa = same_side(b, c, a, (0,0))
sb = same_side(c, a, b, (0,0))
sc = same_side(a, b, c, (0,0))
return (sa and sb and sc)
if __name__ == '__main__':
count = 0
with open('triangles.txt') as f:
for line in f:
ax,ay, bx,by, cx,cy = map(int, line.split(','))
if contains_origin((ax,ay), (bx,by), (cx,cy)):
count += 1
print count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment