import{_ as l,c as a,j as s,a as n,a4 as t,o as i}from"./chunks/framework.DpC1ZpOZ.js";const qs=JSON.parse('{"title":"mbcp.mp_math.plane","description":"","frontmatter":{"title":"mbcp.mp_math.plane","lastUpdated":false},"headers":[],"relativePath":"zht/api/mp_math/plane.md","filePath":"zht/api/mp_math/plane.md"}'),e={name:"zht/api/mp_math/plane.md"},h=t(`
mbcp.mp_math.plane
本模块定义了三维空间中的平面类
Plane3
__init__(self, a: float, b: float, c: float, d: float)
説明: 平面方程:ax + by + cz + d = 0
變數説明:
- a (
float
): x系数- b (
float
): y系数- c (
float
): z系数- d (
float
): 常数项
def __init__(self, a: float, b: float, c: float, d: float):
"""
平面方程:ax + by + cz + d = 0
Args:
a ([\`float\`](https%3A//docs.python.org/3/library/functions.html#float)): x系数
b (\`float\`): y系数
c (\`float\`): z系数
d (\`float\`): 常数项
"""
self.a = a
self.b = b
self.c = c
self.d = d
approx(self, other: Plane3) -> bool
説明: 判断两个平面是否近似相等。
變數説明:
- other (
Plane3
): 另一个平面
返回: bool
: 是否近似相等
def approx(self, other: 'Plane3') -> bool:
"""
判断两个平面是否近似相等。
Args:
other ([\`Plane3\`](./plane#class-plane3)): 另一个平面
Returns:
[\`bool\`](https://docs.python.org/3/library/functions.html#bool): 是否近似相等
"""
if self.a != 0:
k = other.a / self.a
return approx(other.b, self.b * k) and approx(other.c, self.c * k) and approx(other.d, self.d * k)
elif self.b != 0:
k = other.b / self.b
return approx(other.a, self.a * k) and approx(other.c, self.c * k) and approx(other.d, self.d * k)
elif self.c != 0:
k = other.c / self.c
return approx(other.a, self.a * k) and approx(other.b, self.b * k) and approx(other.d, self.d * k)
else:
return False
cal_angle(self, other: Line3 | Plane3) -> AnyAngle
説明: 计算平面与平面之间的夹角。
`,16),p={class:"tip custom-block"},k=s("p",{class:"custom-block-title"},"TIP",-1),r=s("p",null,"平面间夹角计算公式:",-1),o={tabindex:"0",class:"MathJax",jax:"SVG",display:"true",style:{direction:"ltr",display:"block","text-align":"center",margin:"1em 0",position:"relative"}},d={style:{overflow:"visible","min-height":"1px","min-width":"1px","vertical-align":"-2.17ex"},xmlns:"http://www.w3.org/2000/svg",width:"22.011ex",height:"5.206ex",role:"img",focusable:"false",viewBox:"0 -1342 9729 2301","aria-hidden":"true"},Q=t('變數説明:
返回: AnyAngle
: 夹角
抛出:
TypeError
不支持的类型
def cal_angle(self, other: 'Line3 | Plane3') -> 'AnyAngle':
"""
计算平面与平面之间的夹角。
:::tip
平面间夹角计算公式:
$$\\\\theta = \\\\arccos(\\\\frac{n1 \\\\cdot n2}{|n1| \\\\cdot |n2|})$$
其中 $n1$ 和 $n2$ 分别为两个平面的法向量
:::
:::tip
平面与直线夹角计算公式:
$$\\\\theta = \\\\arccos(\\\\frac{n \\\\cdot d}{|n| \\\\cdot |d|})$$
其中 $n$ 为平面的法向量,$d$ 为直线的方向向量
:::
Args:
other ([\`Line3\`](./line#class-line3) | [\`Plane3\`](./plane#class-plane3)): 另一个平面或直线
Returns:
[\`AnyAngle\`](./angle#class-anyangle): 夹角
Raises:
[\`TypeError\`](https%3A//docs.python.org/3/library/exceptions.html#TypeError): 不支持的类型
"""
if isinstance(other, Line3):
return self.normal.cal_angle(other.direction).complementary
elif isinstance(other, Plane3):
return AnyAngle(math.acos(self.normal @ other.normal / (self.normal.length * other.normal.length)), is_radian=True)
else:
raise TypeError(f'Unsupported type: {type(other)}')
cal_distance(self, other: Plane3 | Point3) -> float
説明: 计算平面与平面或点之间的距离。
變數説明:
返回: float
: 距离
抛出:
TypeError
不支持的类型
def cal_distance(self, other: 'Plane3 | Point3') -> float:
"""
计算平面与平面或点之间的距离。
Args:
other ([\`Plane3\`](./plane#class-plane3) | [\`Point3\`](./point#class-point3)): 另一个平面或点
Returns:
[\`float\`](https%3A//docs.python.org/3/library/functions.html#float): 距离
Raises:
[\`TypeError\`](https%3A//docs.python.org/3/library/exceptions.html#TypeError): 不支持的类型
"""
if isinstance(other, Plane3):
return 0
elif isinstance(other, Point3):
return abs(self.a * other.x + self.b * other.y + self.c * other.z + self.d) / (self.a ** 2 + self.b ** 2 + self.c ** 2) ** 0.5
else:
raise TypeError(f'Unsupported type: {type(other)}')
cal_intersection_line3(self, other: Plane3) -> Line3
説明: 计算两平面的交线。
`,16),G={class:"tip custom-block"},I=s("p",{class:"custom-block-title"},"TIP",-1),J=s("p",null,"计算两平面交线的一般步骤:",-1),O=s("ol",null,[s("li",null,"求两平面的法向量的叉乘得到方向向量")],-1),U={tabindex:"0",class:"MathJax",jax:"SVG",display:"true",style:{direction:"ltr",display:"block","text-align":"center",margin:"1em 0",position:"relative"}},K={style:{overflow:"visible","min-height":"1px","min-width":"1px","vertical-align":"-0.186ex"},xmlns:"http://www.w3.org/2000/svg",width:"11.937ex",height:"1.756ex",role:"img",focusable:"false",viewBox:"0 -694 5276 776","aria-hidden":"true"},W=t('變數説明:
- other (
Plane3
): 另一个平面
返回: Line3
: 交线
抛出:
ValueError
平面平行且无交线
def cal_intersection_line3(self, other: 'Plane3') -> 'Line3':
"""
计算两平面的交线。
:::tip
计算两平面交线的一般步骤:
1. 求两平面的法向量的叉乘得到方向向量
$$ d = n1 \\\\times n2 $$
2. 寻找直线上的一点,依次假设$x=0$, $y=0$, $z=0$,并代入两平面方程求出合适的点
直线最终可用参数方程或点向式表示
$$ \\\\begin{cases} x = x_0 + dt \\\\\\\\ y = y_0 + dt \\\\\\\\ z = z_0 + dt \\\\end{cases} $$
或
$$ \\\\frac{x - x_0}{m} = \\\\frac{y - y_0}{n} = \\\\frac{z - z_0}{p} $$
:::
Args:
other ([\`Plane3\`](./plane#class-plane3)): 另一个平面
Returns:
[\`Line3\`](./line#class-line3): 交线
Raises:
[\`ValueError\`](https%3A//docs.python.org/3/library/exceptions.html#ValueError): 平面平行且无交线
"""
if self.normal.is_parallel(other.normal):
raise ValueError('Planes are parallel and have no intersection.')
direction = self.normal.cross(other.normal)
x, y, z = (0, 0, 0)
if self.a != 0 and other.a != 0:
A = np.array([[self.b, self.c], [other.b, other.c]])
B = np.array([-self.d, -other.d])
y, z = np.linalg.solve(A, B)
elif self.b != 0 and other.b != 0:
A = np.array([[self.a, self.c], [other.a, other.c]])
B = np.array([-self.d, -other.d])
x, z = np.linalg.solve(A, B)
elif self.c != 0 and other.c != 0:
A = np.array([[self.a, self.b], [other.a, other.b]])
B = np.array([-self.d, -other.d])
x, y = np.linalg.solve(A, B)
return Line3(Point3(x, y, z), direction)
cal_intersection_point3(self, other: Line3) -> Point3
説明: 计算平面与直线的交点。
變數説明:
- other (
Line3
): 直线
返回: Point3
: 交点
抛出:
ValueError
平面与直线平行或重合
def cal_intersection_point3(self, other: 'Line3') -> 'Point3':
"""
计算平面与直线的交点。
Args:
other ([\`Line3\`](./line#class-line3)): 直线
Returns:
[\`Point3\`](./point#class-point3): 交点
Raises:
[\`ValueError\`](https%3A//docs.python.org/3/library/exceptions.html#ValueError): 平面与直线平行或重合
"""
if self.normal @ other.direction == 0:
raise ValueError('The plane and the line are parallel or coincident.')
x, y, z = other.get_parametric_equations()
t = -(self.a * other.point.x + self.b * other.point.y + self.c * other.point.z + self.d) / (self.a * other.direction.x + self.b * other.direction.y + self.c * other.direction.z)
return Point3(x(t), y(t), z(t))
cal_parallel_plane3(self, point: Point3) -> Plane3
説明: 计算平行于该平面且过指定点的平面。
變數説明:
- point (
Point3
): 指定点
返回: Plane3
: 平面
def cal_parallel_plane3(self, point: 'Point3') -> 'Plane3':
"""
计算平行于该平面且过指定点的平面。
Args:
point ([\`Point3\`](./point#class-point3)): 指定点
Returns:
[\`Plane3\`](./plane#class-plane3): 平面
"""
return Plane3.from_point_and_normal(point, self.normal)
is_parallel(self, other: Plane3) -> bool
説明: 判断两个平面是否平行。
變數説明:
- other (
Plane3
): 另一个平面
返回: bool
: 是否平行
def is_parallel(self, other: 'Plane3') -> bool:
"""
判断两个平面是否平行。
Args:
other ([\`Plane3\`](./plane#class-plane3)): 另一个平面
Returns:
[\`bool\`](https%3A//docs.python.org/3/library/functions.html#bool): 是否平行
"""
return self.normal.is_parallel(other.normal)
normal(self) -> Vector3
説明: 平面的法向量。
返回: Vector3
: 法向量
@property
def normal(self) -> 'Vector3':
"""
平面的法向量。
Returns:
[\`Vector3\`](./vector#class-vector3): 法向量
"""
return Vector3(self.a, self.b, self.c)
from_point_and_normal(cls, point: Point3, normal: Vector3) -> Plane3
説明: 工厂函数 由点和法向量构造平面(点法式构造)。
變數説明:
返回: Plane3
: 平面
@classmethod
def from_point_and_normal(cls, point: 'Point3', normal: 'Vector3') -> 'Plane3':
"""
工厂函数 由点和法向量构造平面(点法式构造)。
Args:
point ([\`Point3\`](./point#class-point3)): 平面上一点
normal ([\`Vector3\`](./vector#class-vector3)): 法向量
Returns:
[\`Plane3\`](./plane#class-plane3): 平面
"""
a, b, c = (normal.x, normal.y, normal.z)
d = -a * point.x - b * point.y - c * point.z
return cls(a, b, c, d)
from_three_points(cls, p1: Point3, p2: Point3, p3: Point3) -> Plane3
説明: 工厂函数 由三点构造平面。
變數説明:
- p1 (
Point3
): 点1- p2 (
Point3
): 点2- p3 (
Point3
): 点3
返回: 平面
@classmethod
def from_three_points(cls, p1: 'Point3', p2: 'Point3', p3: 'Point3') -> 'Plane3':
"""
工厂函数 由三点构造平面。
Args:
p1 ([\`Point3\`](./point#class-point3)): 点1
p2 (\`Point3\`): 点2
p3 (\`Point3\`): 点3
Returns:
平面
"""
v1 = p2 - p1
v2 = p3 - p1
normal = v1.cross(v2)
return cls.from_point_and_normal(p1, normal)
from_two_lines(cls, l1: Line3, l2: Line3) -> Plane3
説明: 工厂函数 由两直线构造平面。
變數説明:
- l1 (
Line3
): 直线- l2 (
Line3
): 直线
返回: 平面
@classmethod
def from_two_lines(cls, l1: 'Line3', l2: 'Line3') -> 'Plane3':
"""
工厂函数 由两直线构造平面。
Args:
l1 ([\`Line3\`](./line#class-line3)): 直线
l2 (\`Line3\`): 直线
Returns:
平面
"""
v1 = l1.direction
v2 = l2.point - l1.point
if v2 == zero_vector3:
v2 = l2.get_point(1) - l1.point
return cls.from_point_and_normal(l1.point, v1.cross(v2))
from_point_and_line(cls, point: Point3, line: Line3) -> Plane3
説明: 工厂函数 由点和直线构造平面。
變數説明:
返回: 平面
@classmethod
def from_point_and_line(cls, point: 'Point3', line: 'Line3') -> 'Plane3':
"""
工厂函数 由点和直线构造平面。
Args:
point ([\`Point3\`](./point#class-point3)): 平面上一点
line ([\`Line3\`](./line#class-line3)): 直线
Returns:
平面
"""
return cls.from_point_and_normal(point, line.direction)
@overload
__and__(self, other: Line3) -> Point3 | None
@overload
def __and__(self, other: 'Line3') -> 'Point3 | None':
...
@overload
__and__(self, other: Plane3) -> Line3 | None
@overload
def __and__(self, other: 'Plane3') -> 'Line3 | None':
...
__and__(self, other)
説明: 取两平面的交集(人话:交线)
變數説明:
抛出:
TypeError
不支持的类型
def __and__(self, other):
"""
取两平面的交集(人话:交线)
Args:
other ([\`Line3\`](./line#class-line3) | [\`Plane3\`](./plane#class-plane3)): 另一个平面或直线
Returns:
[\`Line3\`](./line#class-line3) | [\`Point3\`](./point#class-point3) | [\`None\`](https%3A//docs.python.org/3/library/constants.html#None): 交集
Raises:
[\`TypeError\`](https%3A//docs.python.org/3/library/exceptions.html#TypeError): 不支持的类型
"""
if isinstance(other, Plane3):
if self.normal.is_parallel(other.normal):
return None
return self.cal_intersection_line3(other)
elif isinstance(other, Line3):
if self.normal @ other.direction == 0:
return None
return self.cal_intersection_point3(other)
else:
raise TypeError(f"unsupported operand type(s) for &: 'Plane3' and '{type(other)}'")
__eq__(self, other) -> bool
説明: 判断两个平面是否等价。
變數説明:
- other (
Plane3
): 另一个平面
返回: bool
: 是否等价
def __eq__(self, other) -> bool:
"""
判断两个平面是否等价。
Args:
other ([\`Plane3\`](./plane#class-plane3)): 另一个平面
Returns:
[\`bool\`](https%3A//docs.python.org/3/library/functions.html#bool): 是否等价
"""
return self.approx(other)
__rand__(self, other: Line3) -> Point3
def __rand__(self, other: 'Line3') -> 'Point3':
return self.cal_intersection_point3(other)