From de5bfa3843ba95068453ccfd6143e2412bc0c723 Mon Sep 17 00:00:00 2001 From: snowy Date: Tue, 6 Aug 2024 18:02:51 +0800 Subject: [PATCH] :zap: add some mathematical object --- .gitignore | 2 ++ main.py | 10 ++++++ mcpe/__init__.py | 10 ++++++ mcpe/mp_math/__init__.py | 10 ++++++ mcpe/mp_math/line.py | 66 ++++++++++++++++++++++++++++++++++++++++ mcpe/mp_math/plane.py | 10 ++++++ mcpe/mp_math/point.py | 23 ++++++++++++++ mcpe/mp_math/vector.py | 63 ++++++++++++++++++++++++++++++++++++++ requirements.txt | 1 + tests/test_vector.py | 22 ++++++++++++++ 10 files changed, 217 insertions(+) create mode 100644 .gitignore create mode 100644 main.py create mode 100644 mcpe/__init__.py create mode 100644 mcpe/mp_math/__init__.py create mode 100644 mcpe/mp_math/line.py create mode 100644 mcpe/mp_math/plane.py create mode 100644 mcpe/mp_math/point.py create mode 100644 mcpe/mp_math/vector.py create mode 100644 requirements.txt create mode 100644 tests/test_vector.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3d1b3b5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*script* +.idea* \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..7622630 --- /dev/null +++ b/main.py @@ -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 +""" \ No newline at end of file diff --git a/mcpe/__init__.py b/mcpe/__init__.py new file mode 100644 index 0000000..b89c326 --- /dev/null +++ b/mcpe/__init__.py @@ -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 +""" diff --git a/mcpe/mp_math/__init__.py b/mcpe/mp_math/__init__.py new file mode 100644 index 0000000..a7e0f3e --- /dev/null +++ b/mcpe/mp_math/__init__.py @@ -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 +""" diff --git a/mcpe/mp_math/line.py b/mcpe/mp_math/line.py new file mode 100644 index 0000000..4005fd1 --- /dev/null +++ b/mcpe/mp_math/line.py @@ -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 ...: + ... + + + + + diff --git a/mcpe/mp_math/plane.py b/mcpe/mp_math/plane.py new file mode 100644 index 0000000..d8fc4e6 --- /dev/null +++ b/mcpe/mp_math/plane.py @@ -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 +""" \ No newline at end of file diff --git a/mcpe/mp_math/point.py b/mcpe/mp_math/point.py new file mode 100644 index 0000000..edfbf87 --- /dev/null +++ b/mcpe/mp_math/point.py @@ -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) diff --git a/mcpe/mp_math/vector.py b/mcpe/mp_math/vector.py new file mode 100644 index 0000000..5e8f520 --- /dev/null +++ b/mcpe/mp_math/vector.py @@ -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'") diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..df16fec --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +pytest~=8.3.2 \ No newline at end of file diff --git a/tests/test_vector.py b/tests/test_vector.py new file mode 100644 index 0000000..4a4b890 --- /dev/null +++ b/tests/test_vector.py @@ -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")