Axione-IPE-Viewer/webapp/coordinates.py

39 lines
1.2 KiB
Python
Raw Normal View History

2023-01-11 15:35:12 +01:00
import math
from ipe_fetcher import AreaCoordinates
2023-02-28 22:31:35 +01:00
def adapt_coordinates_to_max_area(
coordinates: AreaCoordinates, max_area
) -> AreaCoordinates:
swx = coordinates.get("swx")
swy = coordinates.get("swy")
nex = coordinates.get("nex")
ney = coordinates.get("ney")
2023-01-11 15:35:12 +01:00
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