一、Fitting
1、3D Plane of Best Fit(由点得面)
from skspatial.objects import Plane
from skspatial.objects import Points
from skspatial.plotting import plot_3d
points = Points([[0, 0, 0], [1, 3, 5], [-5, 6, 3], [3, 6, 7], [-2, 6, 7]])
plane = Plane.best_fit(points)
plot_3d(
points.plotter(c='k', s=50, depthshade=False),
plane.plotter(alpha=0.2, lims_x=(-5, 5), lims_y=(-5, 5)),
)
2、2D Line of Best Fit(点生线)
from skspatial.objects import Line
from skspatial.objects import Points
from skspatial.plotting import plot_2d
points = Points(
[
[0, 0],
[0, 1],
[1, 2],
[3, 3],
[4, 3],
[6, 5],
[5, 6],
[7, 8],
],
)
line_fit = Line.best_fit(points)
plot_2d(
line_fit.plotter(t_1=-7, t_2=7, c='k'),
points.plotter(c='k'),
)
3、3D Line of Best Fit
from skspatial.objects import Line
from skspatial.objects import Points
from skspatial.plotting import plot_3d
points = Points(
[
[0, 0, 0],
[1, 1, 0],
[2, 3, 2],
[3, 2, 3],
[4, 5, 4],
[6, 5, 5],
[6, 6, 5],
[7, 6, 7],
],
)
line_fit = Line.best_fit(points)
plot_3d(
line_fit.plotter(t_1=-7, t_2=7, c='k'),
points.plotter(c='b', depthshade=False),
)
二、Triangle
1、Triangle with Normal Vector(法向量)
from skspatial.objects import Triangle
from skspatial.plotting import plot_3d
triangle = Triangle([0, 0, 1], [1, 1, 0], [0, 2, 1])
centroid = triangle.centroid() # 求重心点
plot_3d(
triangle.plotter(c='k', zorder=3),
centroid.plotter(c='r'),
triangle.normal().plotter(point=centroid, scalar=0.2, c='r'),
*[x.plotter(c='k', zorder=3) for x in triangle.multiple('line', 'abc')],
)
2、Triangle with Altitudes and Orthocenter
from skspatial.objects import Triangle
from skspatial.plotting import plot_2d
triangle = Triangle([0, 0], [2, 0], [1, 2])
plot_2d(
triangle.plotter(c='k', zorder=3),
triangle.orthocenter().plotter(c='r', edgecolor='k', s=100, zorder=3),
*[x.plotter(c='k', zorder=3) for x in triangle.multiple('line', 'abc')],
*[x.plotter() for x in triangle.multiple('altitude', 'ABC')],
)