From d40b1d853872d6d96359abd7e7867fbe915ecf98 Mon Sep 17 00:00:00 2001 From: Aki Date: Wed, 31 Aug 2022 00:56:58 +0200 Subject: 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. --- szilagyi/nomogram.py | 21 ++++++++++++++------- 1 file 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] -- cgit v1.1