from skspatial.objects import Vector
from skspatial.plotting import plot_2d
vector_a = Vector([1, 1])
vector_b = Vector([2, 0])
vector_projected = vector_b.project_vector(vector_a)
# 向量a投影到向量b,返回向量
_, ax = plot_2d(
vector_a.plotter(color='k', head_width=0.1),
vector_b.plotter(color='k', head_width=0.1),
vector_projected.plotter(color='r', head_width=0.1),
)
ax.axis([-0.5, 2.5, -0.5, 1.5]) # 显示的坐标轴坐标
2、line-Plane Projection
from skspatial.objects import Line
from skspatial.objects import Plane
from skspatial.plotting import plot_3d
plane = Plane([0, 1, 0], [0, 1, 0]) # plane(点,法向量)
line = Line([0, -1, 0], [1, -2, 0]) # line(点,方向)
line_projected = plane.project_line(line) # 线投到面上
plot_3d(
plane.plotter(lims_x=(-5, 5), lims_y=(-5, 5), alpha=0.3),
line.plotter(t_1=-2, t_2=2, color='k'),
line_projected.plotter(t_1=-2, t_2=4, color='r'),
)
3、3D Vector-Line Projection
from skspatial.objects import Line
from skspatial.objects import Vector
from skspatial.plotting import plot_3d
line = Line([0, 0, 0], [1, 1, 2])
vector = Vector([1, 1, 0.1])
vector_projected = line.project_vector(vector)
plot_3d(
line.plotter(t_1=-1, c='k', linestyle='--'),
vector.plotter(point=line.point, color='k'),
vector_projected.plotter(point=line.point, color='r', linewidth=2, zorder=3),
)
4、Vector-Plane Projection
from skspatial.objects import Plane
from skspatial.objects import Vector
from skspatial.plotting import plot_3d
plane = Plane([0, 0, 0], [0, 0, 1])
vector = Vector([1, 1, 1])
vector_projected = plane.project_vector(vector)
_, ax = plot_3d(
plane.plotter(lims_x=(-5, 5), lims_y=(-5, 5), alpha=0.3),
vector.plotter(point=plane.point, color='k'),
vector_projected.plotter(point=plane.point, color='r', linewidth=2, zorder=3),
)
ax.set_zlim([-1, 1])
5、2D Point-Line Projection
from skspatial.objects import Line
from skspatial.objects import Point
from skspatial.plotting import plot_2d
line = Line(point=[0, 0], direction=[1, 1])
point = Point([1, 4])
point_projected = line.project_point(point)
line_projection = Line.from_points(point, point_projected)
_, ax = plot_2d(
line.plotter(t_2=5, c='k'),
line_projection.plotter(c='k', linestyle='--'),
point.plotter(s=75, c='k'),
point_projected.plotter(c='r', s=75, zorder=3),
)
ax.axis('equal')
6、Point-Plane Projection
from skspatial.objects import Plane
from skspatial.objects import Point
from skspatial.objects import Vector
from skspatial.plotting import plot_3d
plane = Plane(point=[0, 0, 2], normal=[1, 0, 2])
point = Point([5, 9, 3])
point_projected = plane.project_point(point)
vector_projection = Vector.from_points(point, point_projected)
plot_3d(
plane.plotter(lims_x=(0, 10), lims_y=(0, 15), alpha=0.3),
point.plotter(s=75, c='k'),
point_projected.plotter(c='r', s=75, zorder=3),
vector_projection.plotter(point=point, c='k', linestyle='--'),
)