Files
mbcp/mbcp/mp_math/angle.py
2024-08-27 09:08:27 +08:00

95 lines
2.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding: utf-8 -*-
"""
Copyright (C) 2020-2024 LiteyukiStudio. All Rights Reserved
@Time : 2024/8/26 上午6:29
@Author : snowykami
@Email : snowykami@outlook.com
@File : angle.py
@Software: PyCharm
"""
from typing import overload
from .const import PI
class AnyAngle:
def __init__(self, value: float, is_radian: bool = False):
"""
任意角度。
Args:
value: 角度或弧度值
is_radian: 是否为弧度,默认为否
"""
if is_radian:
self.radian = value
else:
self.radian = value * PI / 180
@property
def complementary(self) -> "AnyAngle":
"""
余角两角的和为90°。
Returns:
余角
"""
return AnyAngle(PI / 2 - self.minimum_positive.radian, is_radian=True)
@property
def supplementary(self) -> "AnyAngle":
"""
补角两角的和为180°。
Returns:
补角
"""
return AnyAngle(PI - self.minimum_positive.radian, is_radian=True)
@property
def degree(self) -> float:
"""
角度。
Returns:
弧度
"""
return self.radian * 180 / PI
@property
def minimum_positive(self) -> "AnyAngle":
"""
最小正角。
Returns:
最小正角度
"""
return AnyAngle(self.radian % (2 * PI))
@property
def maximum_negative(self) -> "AnyAngle":
"""
最大负角。
Returns:
最大负角度
"""
return AnyAngle(-self.radian % (2 * PI), is_radian=True)
def __add__(self, other: "AnyAngle") -> "AnyAngle":
return AnyAngle(self.radian + other.radian, is_radian=True)
def __sub__(self, other: "AnyAngle") -> "AnyAngle":
return AnyAngle(self.radian - other.radian, is_radian=True)
def __mul__(self, other: float) -> "AnyAngle":
return AnyAngle(self.radian * other, is_radian=True)
@overload
def __truediv__(self, other: float) -> "AnyAngle":
...
@overload
def __truediv__(self, other: "AnyAngle") -> float:
...
def __truediv__(self, other):
if isinstance(other, AnyAngle):
return self.radian / other.radian
return AnyAngle(self.radian / other, is_radian=True)