# check if r*(AC) + s*(AB) == P def point_in_triangle((x_a, y_a), (x_b, y_b), (x_c, y_c), (x_p, y_p)): t = x_c - x_a u = x_b - x_a v = y_c - y_a w = y_b - y_a # Cramer's rule detA = t*w - u*v detR = x_p*w - u*y_p detS = t*y_p - x_p*v if detA == 0: print "Not solvable" r = detR / float(detA) s = detS / float(detA) return 0 <= r <= 1 and 0 <= s <= 1 and r + s < 1 count = 0 f = open('102.txt', 'r') for line in f: line = line.rstrip('\r\n') coords = [ int(x) for x in line.split(',') ] # move one point to origin, adjust other points (a1, a2) = (coords[0], coords[1]) A = (0, 0) B = (coords[2]-a1, coords[3]-a2) C = (coords[4]-a1, coords[5]-a2) P = (-a1, -a2) if point_in_triangle(A, B, C, P): count += 1 f.close() print count