mower-ng/mower/utils/vector.py

38 lines
1 KiB
Python

from mower.utils import typealias as tp
def va(a: tp.Coordinate, b: tp.Coordinate) -> tp.Coordinate:
"""向量加法,vector add"""
return [a[0] + b[0], a[1] + b[1]]
def vs(a: tp.Coordinate, b: tp.Coordinate) -> tp.Coordinate:
"""向量减法,vector subtract"""
return [a[0] - b[0], a[1] - b[1]]
def sa(scope: tp.Scope, vector: tp.Coordinate) -> tp.Scope:
"""区域偏移,scope add"""
return [va(scope[0], vector), va(scope[1], vector)]
def sm(a: float, v: tp.Coordinate) -> tp.Coordinate:
"""数乘向量,scalar multiply"""
return round(a * v[0]), round(a * v[1])
def ss(x_rate: float, y_rate: float, scope: tp.Scope) -> tp.Scope:
"""区域放缩,scope scaling"""
return (
int(scope[0][0] * x_rate),
int(scope[0][1] * y_rate),
), (
int(scope[1][0] * x_rate),
int(scope[1][1] * y_rate),
)
def in_scope(scope: tp.Scope, vector: tp.Coordinate) -> bool:
"""在区域内"""
x, y = vector
return scope[0][0] < x < scope[1][0] and scope[0][1] < y < scope[1][1]