summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorReiner Herrmann <reiner@reiner-h.de>2011-11-30 23:29:14 +0100
committerReiner Herrmann <reiner@reiner-h.de>2014-08-31 20:35:09 +0200
commit83ca7628fbba1b99cfd60bf6e40b5e76d7a0d5f1 (patch)
tree98d6916413ec413a7efb9ceb3ea42a73fef041eb /src
parent28af93e8007f055c2e3d9d8d69f04afa32c0d044 (diff)
solve 102 via Cramer's rule to get rid of workaround
Diffstat (limited to 'src')
-rw-r--r--src/projecteuler/102.py16
1 files changed, 11 insertions, 5 deletions
diff --git a/src/projecteuler/102.py b/src/projecteuler/102.py
index e506516..a2108f6 100644
--- a/src/projecteuler/102.py
+++ b/src/projecteuler/102.py
@@ -1,16 +1,22 @@
+# 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
- # workaround for horizontal lines
- if t == 0:
- t = 0.00000000000000001
+ # 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"
- s = (y_p - (x_p*v/float(t))) / (float(w) - u*v/float(t))
- r = (x_p - u*s) / float(t)
+ r = detR / float(detA)
+ s = detS / float(detA)
return 0 <= r <= 1 and 0 <= s <= 1 and r + s < 1