mirror of
https://github.com/enzet/map-machine.git
synced 2025-08-02 00:00:29 +02:00
Refactor constructor; change code style.
This commit is contained in:
parent
8509e3c0e2
commit
21be74a8a2
18 changed files with 759 additions and 683 deletions
|
@ -1,98 +1,46 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
"""
|
||||
Author: Sergey Vartanov (me@enzet.ru)
|
||||
"""
|
||||
|
||||
import math
|
||||
import numpy as np
|
||||
from typing import Optional
|
||||
|
||||
|
||||
def get_ratio(maximum, minimum, ratio: float = 1):
|
||||
return (maximum[0] - minimum[0]) * ratio / (maximum[1] - minimum[1])
|
||||
|
||||
|
||||
class Flinger:
|
||||
def map_(
|
||||
value: float, current_min: float, current_max: float, target_min: float,
|
||||
target_max: float):
|
||||
"""
|
||||
Flinger. Coordinates repositioning.
|
||||
Map current value in bounds of current_min and current_max to bounds of
|
||||
target_min and target_max.
|
||||
"""
|
||||
def __init__(
|
||||
self, minimum, maximum, target_minimum=None, target_maximum=None,
|
||||
ratio=None):
|
||||
|
||||
self.minimum = minimum
|
||||
self.maximum = maximum
|
||||
|
||||
if not target_minimum:
|
||||
target_minimum = [0, 0]
|
||||
if not target_maximum:
|
||||
target_maximum = maximum - minimum
|
||||
|
||||
space = [0, 0]
|
||||
|
||||
if ratio:
|
||||
if ratio == "geo":
|
||||
ratio = math.sin(
|
||||
(90.0 - ((self.maximum[1] + self.minimum[1]) / 2.0))
|
||||
/ 180.0 * math.pi)
|
||||
|
||||
current_ratio = get_ratio(self.maximum, self.minimum, ratio)
|
||||
target_ratio = get_ratio(target_maximum, target_minimum)
|
||||
|
||||
if current_ratio >= target_ratio:
|
||||
n = (target_maximum[0] - target_minimum[0]) / \
|
||||
(maximum[0] - minimum[0]) / ratio
|
||||
space[1] = \
|
||||
((target_maximum[1] - target_minimum[1]) -
|
||||
(maximum[1] - minimum[1]) * n) / 2.0
|
||||
space[0] = 0
|
||||
else:
|
||||
n = (target_maximum[1] - target_minimum[1]) / \
|
||||
(maximum[1] - minimum[1])
|
||||
space[0] = \
|
||||
((target_maximum[0] - target_minimum[0]) -
|
||||
(maximum[0] - minimum[0]) * n) / 2.0
|
||||
space[1] = 0
|
||||
|
||||
target_minimum[0] += space
|
||||
target_maximum[0] += space
|
||||
|
||||
self.target_minimum = target_minimum
|
||||
self.target_maximum = target_maximum
|
||||
|
||||
def fling(self, current):
|
||||
"""
|
||||
Fling current point to the surface.
|
||||
|
||||
:param current: vector to fling
|
||||
"""
|
||||
x = map_(
|
||||
current[0], self.minimum[0], self.maximum[0],
|
||||
self.target_minimum[0], self.target_maximum[0])
|
||||
y = map_(
|
||||
current[1], self.minimum[1], self.maximum[1],
|
||||
self.target_minimum[1], self.target_maximum[1])
|
||||
return [x, y]
|
||||
return \
|
||||
target_min + (value - current_min) / (current_max - current_min) * \
|
||||
(target_max - target_min)
|
||||
|
||||
|
||||
class Geo:
|
||||
def __init__(self, lat, lon):
|
||||
self.lat = lat
|
||||
self.lon = lon
|
||||
def __init__(self, lat: float, lon: float):
|
||||
self.lat: float = lat
|
||||
self.lon: float = lon
|
||||
|
||||
def __getitem__(self, item):
|
||||
def __getitem__(self, item) -> Optional[float]:
|
||||
if item == 0:
|
||||
return self.lon
|
||||
if item == 1:
|
||||
return self.lat
|
||||
return None
|
||||
|
||||
def __add__(self, other):
|
||||
def __add__(self, other: "Geo") -> "Geo":
|
||||
return Geo(self.lat + other.lat, self.lon + other.lon)
|
||||
|
||||
def __sub__(self, other):
|
||||
def __sub__(self, other: "Geo") -> "Geo":
|
||||
return Geo(self.lat - other.lat, self.lon - other.lon)
|
||||
|
||||
def __repr__(self):
|
||||
def __repr__(self) -> str:
|
||||
return f"{self.lat}, {self.lon}"
|
||||
|
||||
|
||||
|
@ -157,15 +105,3 @@ class GeoFlinger:
|
|||
self.minimum.lat, self.maximum.lat,
|
||||
self.target_minimum[1], self.target_maximum[1])
|
||||
return [x, y]
|
||||
|
||||
|
||||
def map_(
|
||||
value: float, current_min: float, current_max: float, target_min: float,
|
||||
target_max: float):
|
||||
"""
|
||||
Map current value in bounds of current_min and current_max to bounds of
|
||||
target_min and target_max.
|
||||
"""
|
||||
return \
|
||||
target_min + (value - current_min) / (current_max - current_min) * \
|
||||
(target_max - target_min)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue