summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--szilagyi/nomogram.py21
1 files changed, 14 insertions, 7 deletions
diff --git a/szilagyi/nomogram.py b/szilagyi/nomogram.py
index aed8dca..9d76127 100644
--- a/szilagyi/nomogram.py
+++ b/szilagyi/nomogram.py
@@ -5,10 +5,20 @@ import re
from collections import deque
+class Vector(complex):
+ def __getitem__(self, index):
+ if index == 0:
+ return self.real
+ elif index == 1:
+ return self.imag
+ else:
+ raise IndexError
+
+
def load(directory):
def _read(iterable):
for x, y in iterable:
- yield float(x), float(y)
+ yield Vector(float(x), float(y))
def _load(filename):
with open(filename) as fd:
@@ -81,13 +91,10 @@ def find_boundary_curves(swis, x, y):
return segments
-def dist(x1, y1, x2, y2):
- return math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
-
-
def calculate_swi(segments, x, y):
+ vec = Vector(x, y)
low = segments[0]
high = segments[1]
- dist_to_low = min(dist(p[0], p[1], x, y) for p in (low[1][low[2]], low[1][low[2]]))
- dist_to_high = min(dist(p[0], p[1], x, y) for p in (high[1][high[2]], high[1][high[2]]))
+ dist_to_low = min(abs(vec - p) for p in (low[1][low[2]], low[1][low[2]]))
+ dist_to_high = min(abs(vec - p) for p in (high[1][high[2]], high[1][high[2]]))
return dist_to_low / (dist_to_low + dist_to_high) * (high[0] - low[0]) + low[0]