Skip to content

模組 mbcp.mp_math.line

本模块定义了三维空间中的直线类

class Line3

method __init__(self, point: Point3, direction: Vector3)

説明: 三维空间中的直线。由一个点和一个方向向量确定。

變數説明:

  • point (Point3): 直线上的一点
  • direction (Vector3): 方向向量
源碼於GitHub上查看
python
def __init__(self, point: 'Point3', direction: 'Vector3'):
    self.point = point
    self.direction = direction

method approx(self, other: Line3, epsilon: float = APPROX) -> bool

説明: 判断两条直线是否近似相等。

變數説明:

  • other (Line3): 另一条直线
  • epsilon (float): 误差

返回: bool: 是否近似相等

源碼於GitHub上查看
python
def approx(self, other: 'Line3', epsilon: float=APPROX) -> bool:
    return self.is_approx_parallel(other, epsilon) and (self.point - other.point).is_approx_parallel(self.direction, epsilon)

method cal_angle(self, other: Line3) -> AnyAngle

説明: 计算直线和直线之间的夹角。

變數説明:

  • other (Line3): 另一条直线

返回: AnyAngle: 夹角

源碼於GitHub上查看
python
def cal_angle(self, other: 'Line3') -> 'AnyAngle':
    return self.direction.cal_angle(other.direction)

method cal_distance(self, other: Line3 | Point3) -> float

説明: 计算直线和直线或点之间的距离。

變數説明:

返回: float: 距离

抛出:

源碼於GitHub上查看
python
def cal_distance(self, other: 'Line3 | Point3') -> float:
    if isinstance(other, Line3):
        if self == other:
            return 0
        elif self.is_parallel(other):
            return (other.point - self.point).cross(self.direction).length / self.direction.length
        elif not self.is_coplanar(other):
            return abs(self.direction.cross(other.direction) @ (self.point - other.point) / self.direction.cross(other.direction).length)
        else:
            return 0
    elif isinstance(other, Point3):
        return (other - self.point).cross(self.direction).length / self.direction.length
    else:
        raise TypeError('Unsupported type.')

method cal_intersection(self, other: Line3) -> Point3

説明: 计算两条直线的交点。

變數説明:

  • other (Line3): 另一条直线

返回: Point3: 交点

抛出:

源碼於GitHub上查看
python
def cal_intersection(self, other: 'Line3') -> 'Point3':
    if self.is_parallel(other):
        raise ValueError('Lines are parallel and do not intersect.')
    if not self.is_coplanar(other):
        raise ValueError('Lines are not coplanar and do not intersect.')
    return self.point + self.direction.cross(other.direction) @ other.direction.cross(self.point - other.point) / self.direction.cross(other.direction).length ** 2 * self.direction

method cal_perpendicular(self, point: Point3) -> Line3

説明: 计算直线经过指定点p的垂线。

變數説明:

返回: Line3: 垂线

源碼於GitHub上查看
python
def cal_perpendicular(self, point: 'Point3') -> 'Line3':
    return Line3(point, self.direction.cross(point - self.point))

method get_point(self, t: RealNumber) -> Point3

説明: 获取直线上的点。同一条直线,但起始点和方向向量不同,则同一个t对应的点不同。

變數説明:

返回: Point3: 点

源碼於GitHub上查看
python
def get_point(self, t: RealNumber) -> 'Point3':
    return self.point + t * self.direction

method get_parametric_equations(self) -> tuple[OneSingleVarFunc, OneSingleVarFunc, OneSingleVarFunc]

説明: 获取直线的参数方程。

返回: tuple[OneSingleVarFunc, OneSingleVarFunc, OneSingleVarFunc]: 参数方程

源碼於GitHub上查看
python
def get_parametric_equations(self) -> tuple[OneSingleVarFunc, OneSingleVarFunc, OneSingleVarFunc]:
    return (lambda t: self.point.x + self.direction.x * t, lambda t: self.point.y + self.direction.y * t, lambda t: self.point.z + self.direction.z * t)

method is_approx_parallel(self, other: Line3, epsilon: float = 1e-06) -> bool

説明: 判断两条直线是否近似平行。

變數説明:

  • other (Line3): 另一条直线
  • epsilon (float): 误差

返回: bool: 是否近似平行

源碼於GitHub上查看
python
def is_approx_parallel(self, other: 'Line3', epsilon: float=1e-06) -> bool:
    return self.direction.is_approx_parallel(other.direction, epsilon)

method is_parallel(self, other: Line3) -> bool

説明: 判断两条直线是否平行。

變數説明:

返回: bool: 是否平行

源碼於GitHub上查看
python
def is_parallel(self, other: 'Line3') -> bool:
    return self.direction.is_parallel(other.direction)

method is_collinear(self, other: Line3) -> bool

説明: 判断两条直线是否共线。

變數説明:

返回: bool: 是否共线

源碼於GitHub上查看
python
def is_collinear(self, other: 'Line3') -> bool:
    return self.is_parallel(other) and (self.point - other.point).is_parallel(self.direction)

method is_point_on(self, point: Point3) -> bool

説明: 判断点是否在直线上。

變數説明:

返回: bool: 是否在直线上

源碼於GitHub上查看
python
def is_point_on(self, point: 'Point3') -> bool:
    return (point - self.point).is_parallel(self.direction)

method is_coplanar(self, other: Line3) -> bool

説明: 判断两条直线是否共面。 充要条件:两直线方向向量的叉乘与两直线上任意一点的向量的点积为0。

變數説明:

返回: bool: 是否共面

源碼於GitHub上查看
python
def is_coplanar(self, other: 'Line3') -> bool:
    return self.direction.cross(other.direction) @ (self.point - other.point) == 0

method simplify(self)

説明: 简化直线方程,等价相等。 自体简化,不返回值。

按照可行性一次对x y z 化 0 处理,并对向量单位化

源碼於GitHub上查看
python
def simplify(self):
    self.direction.normalize()
    if self.direction.x == 0:
        self.point.x = 0
    if self.direction.y == 0:
        self.point.y = 0
    if self.direction.z == 0:
        self.point.z = 0

@classmethod

method from_two_points(cls, p1: Point3, p2: Point3) -> Line3

説明: 工厂函数 由两点构造直线。

變數説明:

返回: Line3: 直线

源碼於GitHub上查看
python
@classmethod
def from_two_points(cls, p1: 'Point3', p2: 'Point3') -> 'Line3':
    direction = p2 - p1
    return cls(p1, direction)

method __and__(self, other: Line3) -> Line3 | Point3 | None

説明: 计算两条直线点集合的交集。重合线返回自身,平行线返回None,交线返回交点。

變數説明:

  • other (Line3): 另一条直线

返回: Line3 | Point3 | None: 交集

源碼於GitHub上查看
python
def __and__(self, other: 'Line3') -> 'Line3 | Point3 | None':
    if self.is_collinear(other):
        return self
    elif self.is_parallel(other) or not self.is_coplanar(other):
        return None
    else:
        return self.cal_intersection(other)

method __eq__(self, other) -> bool

説明: 判断两条直线是否等价。

v1 // v2 ∧ (p1 - p2) // v1

變數説明:

  • other (Line3): 另一条直线

返回: bool: 是否等价

源碼於GitHub上查看
python
def __eq__(self, other) -> bool:
    return self.direction.is_parallel(other.direction) and (self.point - other.point).is_parallel(self.direction)

文檔由 VitePress 構建 | API引用由 litedoc 生成