add some mathematical object

This commit is contained in:
远野千束 2024-08-06 18:02:51 +08:00
parent 044040dd74
commit de5bfa3843
10 changed files with 217 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*script*
.idea*

10
main.py Normal file
View File

@ -0,0 +1,10 @@
# -*- coding: utf-8 -*-
"""
Copyright (C) 2020-2024 LiteyukiStudio. All Rights Reserved
@Time : 2024/8/6 下午1:30
@Author : snowykami
@Email : snowykami@outlook.com
@File : main.py
@Software: PyCharm
"""

10
mcpe/__init__.py Normal file
View File

@ -0,0 +1,10 @@
# -*- coding: utf-8 -*-
"""
Copyright (C) 2020-2024 LiteyukiStudio. All Rights Reserved
@Time : 2024/8/6 下午12:54
@Author : snowykami
@Email : snowykami@outlook.com
@File : __init__.py
@Software: PyCharm
"""

10
mcpe/mp_math/__init__.py Normal file
View File

@ -0,0 +1,10 @@
# -*- coding: utf-8 -*-
"""
Copyright (C) 2020-2024 LiteyukiStudio. All Rights Reserved
@Time : 2024/8/6 下午12:57
@Author : snowykami
@Email : snowykami@outlook.com
@File : __init__.py.py
@Software: PyCharm
"""

66
mcpe/mp_math/line.py Normal file
View File

@ -0,0 +1,66 @@
# -*- coding: utf-8 -*-
"""
Copyright (C) 2020-2024 LiteyukiStudio. All Rights Reserved
@Time : 2024/8/6 下午12:57
@Author : snowykami
@Email : snowykami@outlook.com
@File : line.py
@Software: PyCharm
"""
from typing import overload, TYPE_CHECKING
if TYPE_CHECKING:
from mcpe.mp_math.point import Point3
from mcpe.mp_math.vector import Vector3
class Line3:
def __init__(self, a: float, b: float, c: float, d:float):
"""
三维空间中的直线
:param a:
:param b:
:param c:
:param d:
"""
self.a = a
self.b = b
self.c = c
self.d = d
def __str__(self):
return f"Line3({self.a}, {self.b}, {self.c}, {self.d})"
def get_perpendicular(self, p: "Point3") -> "Line3":
"""
获取直线经过指定点p的垂线
:param p: 指定点p直线外的点
:return: 垂直于self且过点p的直线
"""
a = -self.b
b = self.a
c = 0
d = -(a * p.x + b * p.y + self.c * p.z)
return Line3(a, b, c, d)
def get_intersection(self, l: "Line3") -> "Point3":
"""
获取两空间直线的交点
:param l: 另一条直线不平行于self不共线且共面
:return: 交点
"""
# 平行检测
if ...:
...
# 共线检测
if ...:
...
# 不共线检测
if ...:
...

10
mcpe/mp_math/plane.py Normal file
View File

@ -0,0 +1,10 @@
# -*- coding: utf-8 -*-
"""
Copyright (C) 2020-2024 LiteyukiStudio. All Rights Reserved
@Time : 2024/8/6 下午1:45
@Author : snowykami
@Email : snowykami@outlook.com
@File : plane.py
@Software: PyCharm
"""

23
mcpe/mp_math/point.py Normal file
View File

@ -0,0 +1,23 @@
from typing import overload, TYPE_CHECKING
if TYPE_CHECKING:
from mcpe.mp_math.vector import Vector3
class Point3:
def __init__(self, x, y, z):
"""
笛卡尔坐标系中的点
:param x:
:param y:
:param z:
"""
self.x = x
self.y = y
self.z = z
def __str__(self):
return f"Point3({self.x}, {self.y}, {self.z})"
def __add__(self, other: "Vector3") -> "Point3":
return Point3(self.x + other.x, self.y + other.y, self.z + other.z)

63
mcpe/mp_math/vector.py Normal file
View File

@ -0,0 +1,63 @@
from typing import overload, TYPE_CHECKING
if TYPE_CHECKING:
from mcpe.mp_math.point import Point3
class Vector3:
def __init__(self, x, y, z):
"""
笛卡尔坐标系中的向量
:param x:
:param y:
:param z:
"""
self.x = x
self.y = y
self.z = z
def __str__(self):
return f"Vector3({self.x}, {self.y}, {self.z})"
@overload
def __add__(self, other: 'Vector3') -> 'Vector3':
...
@overload
def __add__(self, other: 'Point3') -> 'Point3':
...
def __add__(self, other):
if isinstance(other, Vector3):
return Vector3(self.x + other.x, self.y + other.y, self.z + other.z)
elif isinstance(other, Point3):
return Point3(self.x + other.x, self.y + other.y, self.z + other.z)
else:
raise TypeError(f"unsupported operand type(s) for +: 'Vector3' and '{type(other)}'")
def __radd__(self, other: 'Point3') -> 'Point3':
return Point3(self.x + other.x, self.y + other.y, self.z + other.z)
@overload
def __sub__(self, other: 'Vector3') -> 'Vector3':
...
@overload
def __sub__(self, other: 'Point3') -> 'Point3':
...
def __sub__(self, other):
if isinstance(other, Vector3):
return Vector3(self.x - other.x, self.y - other.y, self.z - other.z)
elif isinstance(other, Point3):
return Point3(self.x - other.x, self.y - other.y, self.z - other.z)
else:
raise TypeError(f"unsupported operand type(s) for -: 'Vector3' and '{type(other)}'")
def __rsub__(self, other: Point3):
if isinstance(other, Point3):
return Point3(other.x - self.x, other.y - self.y, other.z - self.z)
elif isinstance(other, Vector3):
return Vector3(other.x - self.x, other.y - self.y, other.z - self.z)
else:
raise TypeError(f"unsupported operand type(s) for -: '{type(other)}' and 'Vector3'")

1
requirements.txt Normal file
View File

@ -0,0 +1 @@
pytest~=8.3.2

22
tests/test_vector.py Normal file
View File

@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
"""
Copyright (C) 2020-2024 LiteyukiStudio. All Rights Reserved
@Time : 2024/8/6 下午1:05
@Author : snowykami
@Email : snowykami@outlook.com
@File : test_vector.py
@Software: PyCharm
"""
from mcpe.mp_math.vector import Vector3
from mcpe.mp_math.point import Point3
def test_v():
v1 = Vector3(1, 2, 3)
v2 = Vector3(4, 5, 6)
v3 = v1 + v2
assert v3.x == 5
assert v3.y == 7
assert v3.z == 9
print("test_v 1111passed")