diff options
author | Aki <please@ignore.pl> | 2022-08-31 00:56:58 +0200 |
---|---|---|
committer | Aki <please@ignore.pl> | 2022-08-31 00:56:58 +0200 |
commit | d40b1d853872d6d96359abd7e7867fbe915ecf98 (patch) | |
tree | 42c6dd9819dbbdf87444e7b3531280e229fe00de | |
parent | 836bfd0e2fe965a1abebf6e573cdc9abacf94686 (diff) | |
download | szilagyi-d40b1d853872d6d96359abd7e7867fbe915ecf98.zip szilagyi-d40b1d853872d6d96359abd7e7867fbe915ecf98.tar.gz szilagyi-d40b1d853872d6d96359abd7e7867fbe915ecf98.tar.bz2 |
Wrapped points into a class
The getitem implementation is here to ease the transition and divide it into
clear small steps. Later, it will be removed in favour of x and y properties
or simply real and imag accesses.
-rw-r--r-- | szilagyi/nomogram.py | 21 |
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] |