Axione-IPE-Viewer/webapp/coordinates.py
2023-01-11 15:35:12 +01:00

35 lines
1.2 KiB
Python

import math
from ipe_fetcher import AreaCoordinates
def check_coordinates_area(coordinates: AreaCoordinates, max_area) -> AreaCoordinates:
swx = coordinates.get('swx')
swy = coordinates.get('swy')
nex = coordinates.get('nex')
ney = coordinates.get('ney')
x_interval = abs(nex - swx)
y_interval = abs(ney - swy)
area = x_interval * y_interval
if area <= max_area:
# We are within max area, use original coordinates
return coordinates
else:
# Decrease area size to max area while keeping x y ratio
new_x_interval = x_interval * math.sqrt(max_area / (x_interval * y_interval))
new_y_interval = y_interval * math.sqrt(max_area / (x_interval * y_interval))
return AreaCoordinates(
swx=(swx + nex - new_x_interval) / 2,
swy=(swy + ney - new_y_interval) / 2,
nex=(swx + nex + new_x_interval) / 2,
ney=(swy + ney + new_y_interval) / 2,
)
def check_coordinates_args(args):
processed_args = {}
for k in ["swx", "swy", "nex", "ney"]:
if k not in args:
raise ValueError(f"{k} not in args")
processed_args[k] = float(args[k])
return processed_args