scikit-spatial学习笔记——Fitting&Triangle

一、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')],
)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值