본문 바로가기
scikit-spatial (벡터,평면,원 등)

[파이썬] 벡터,평면,선,원,실린더 등을 다루는 패키지 (scikit-spatial)

by 만다린망고 2021. 10. 22.
반응형

scikit-spatial 패키지는 넘파이 기반입니다. 아래와 같은 객체를 만들어줍니다. 

Point
Points
Vector
Line
Plane
Circle
Sphere
Triangle
Cylinder

 

documentation link : https://scikit-spatial.readthedocs.io/en/stable/objects/toc.html


뿐만 아니라 여러가지 계산도 가능합니다. 각각의 클래스를 불러오는 방법은 아래와 같습니다. 아래는 Points 의 예시입니다. 

from skspatial.objects import Points


각 객체를 정의하는 방법을 알아봅시다. 

point = Point([1,2,6])
points = Points([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
vector = Vector([2,3,5])
line1 = Line(point=[0, 0, 3], direction=[1, 3, 0]) #한점과 방향이용
line1 = Line.from_points([1,2,5], [5,5,9]) # 두 점 이용
plane1 = Plane(point=[2, 3, 6], normal=[0, 0, 1]) #한점과 법선벡터 이용
plane2 = Plane.from_points(점1, 점2, 점3) #세 점 이용
circle = Circle([2, 3], 5) #2D만 가능
sphere = Sphere([1, 3, 5], 3)
triangle = Triangle([0, 2, 1], [1,3, 0], [5, 5, 1])
cylinder = Cylinder(point=[1, 2, 3], vector=[0, 0, 5], radius=4)


각 클래스에서 사용 가능한 메소드는 dir 함수를 사용하면 됩니다. 예를들어 Line 클래스에서 사용 가능한 메소드는 아래와 같이 찾습니다 .

>>> dir(Line)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'best_fit', 'contains_point', 'distance_line', 'distance_point', 'from_points', 'from_slope', 'intersect_line', 'is_close', 'is_coplanar', 'plot_2d', 'plot_3d', 'plotter', 'project_point', 'project_vector', 'side_point', 'sum_squares', 'to_point', 'transform_points']


메소드의 사용 법은 아래와 같이 알아봅니다. 

>>> help(Line.plot_2d)
Help on function plot_2d in module skspatial.objects.line:

plot_2d(self, ax_2d: 'Axes', t_1: 'float' = 0, t_2: 'float' = 1, **kwargs) -> 'None'
    Plot a 2D line.
    
    The line is plotted by connecting two 2D points.
    
    Parameters
    ----------
    ax_2d : Axes
        Instance of :class:`~matplotlib.axes.Axes`.
    t_1, t_2 : {int, float}
        Parameters to determine points 1 and 2 along the line.
        These are passed to :meth:`Line.to_point`.
        Defaults are 0 and 1.
    kwargs : dict, optional
        Additional keywords passed to :meth:`~matplotlib.axes.Axes.plot`.
    
    Examples
    --------
    .. plot::
        :include-source:
    
        >>> import matplotlib.pyplot as plt
        >>> from skspatial.objects import Line
    
        >>> _, ax = plt.subplots()
    
        >>> line = Line([1, 2], [3, 4])
    
        >>> line.plot_2d(ax, t_1=-2, t_2=3, c='k')
        >>> line.point.plot_2d(ax, c='r', s=100, zorder=3)
        >>> grid = ax.grid()
반응형

댓글