Problem 102

completed January 14, 2012

Comments

I found the one point that was alone on either side of the y-axis. Using this point, I found the y-intercepts of the two lines that are made through this one. If (0,0) was between these two y-intercepts, I knew the origin was inside the triangle.

Code

count = 0
for p in tri:
	x = sorted([(p[0], 0), (p[2], 2), (p[4], 4)])
	if max(x)[0] < 0 or min(x)[0] > 0: continue
	if x[1][0] < 0:
		test = max(x)
	elif x[1][0] > 0:
		test = min(x)
	if test[1] == 0:
		a = (p[0], p[1])
		b = (p[2], p[3])
		c = (p[4], p[5])
	elif test[1] == 2:
		a = (p[2], p[3])
		b = (p[0], p[1])
		c = (p[4], p[5])
	elif test[1] == 4:
		a = (p[4], p[5])
		b = (p[2], p[3])
		c = (p[0], p[1])
	gap = ((a[0]*b[1] - b[0]*a[1])/(a[0] - b[0]), (a[0]*c[1] - c[0]*a[1])/(a[0] - c[0]))
	if (gap[0] > 0 and gap[1] < 0) or (gap[0] < 0 and gap[1] > 0):
		count += 1

print count