Spatial objects and computations based on NumPy arrays for 2D, 3D, and higher-dimensional geometric calculations.
—
Standalone functions for computing geometric properties and triangle analysis capabilities including areas, volumes, triangle classification, and spatial relationships.
Standalone measurement functions for computing areas and volumes from point coordinates without creating spatial object instances.
def area_triangle(point_a, point_b, point_c) -> np.float64:
"""
Calculate area of triangle from three vertices.
The points are the vertices of the triangle and must be 3D or less.
Parameters:
- point_a: array-like, first vertex
- point_b: array-like, second vertex
- point_c: array-like, third vertex
Returns:
Area of the triangle using cross product formula
References:
http://mathworld.wolfram.com/TriangleArea.html
"""
def volume_tetrahedron(point_a, point_b, point_c, point_d) -> np.float64:
"""
Calculate volume of tetrahedron from four vertices.
The points are the vertices of the tetrahedron and must be 3D or less.
Parameters:
- point_a: array-like, first vertex
- point_b: array-like, second vertex
- point_c: array-like, third vertex
- point_d: array-like, fourth vertex
Returns:
Volume of the tetrahedron using scalar triple product
References:
http://mathworld.wolfram.com/Tetrahedron.html
"""
def area_signed(points) -> float:
"""
Calculate signed area of 2D polygon using shoelace formula.
A positive area is returned for counter-clockwise vertex sequences.
Parameters:
- points: array-like, 2D coordinates of polygon vertices
Returns:
Signed area of the polygon
Raises:
ValueError if points are not 2D or fewer than 3 points provided
References:
https://en.wikipedia.org/wiki/Shoelace_formula
"""Usage Example:
from skspatial.measurement import area_triangle, volume_tetrahedron, area_signed
# Calculate triangle area
area = area_triangle([0, 0], [0, 1], [1, 0]) # 0.5
# Calculate triangle area in 3D
area_3d = area_triangle([3, -5, 1], [5, 2, 1], [9, 4, 2]) # 12.54
# Calculate tetrahedron volume
volume = volume_tetrahedron([0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]) # 0.167
# Calculate signed area of polygon
# Counter-clockwise gives positive area
area_ccw = area_signed([[0, 0], [1, 0], [0, 1]]) # 0.5
# Clockwise gives negative area
area_cw = area_signed([[0, 0], [0, 1], [1, 0]]) # -0.5Comprehensive triangle analysis including geometric properties, classification, and special point calculations.
class Triangle:
"""
A triangle defined by three vertices.
Parameters:
- point_a: array-like, first vertex
- point_b: array-like, second vertex
- point_c: array-like, third vertex
"""
def __init__(self, point_a, point_b, point_c): ...
@property
def point_a(self) -> Point:
"""First vertex of the triangle."""
@property
def point_b(self) -> Point:
"""Second vertex of the triangle."""
@property
def point_c(self) -> Point:
"""Third vertex of the triangle."""
@property
def dimension(self) -> int:
"""Dimension of the triangle."""
def multiple(self, name_method, inputs) -> tuple:
"""
Apply method to multiple inputs.
Parameters:
- name_method: str, name of method to apply
- inputs: sequence, inputs to apply method to
Returns:
Tuple of results from applying method to each input
"""
def normal(self) -> Vector:
"""
Calculate normal vector to triangle plane.
Returns:
Normal vector using cross product of edge vectors
"""
def area(self) -> np.float64:
"""
Calculate area of the triangle.
Returns:
Triangle area using cross product formula
"""
def perimeter(self) -> np.float64:
"""
Calculate perimeter of the triangle.
Returns:
Sum of all three side lengths
"""
def point(self, vertex) -> Point:
"""
Get vertex by letter designation.
Parameters:
- vertex: str, vertex letter ('A', 'B', or 'C')
Returns:
Corresponding vertex point
"""
def line(self, side) -> Line:
"""
Get line along specified side.
Parameters:
- side: str, side letter ('a', 'b', or 'c')
'a' is opposite vertex A (BC edge)
'b' is opposite vertex B (AC edge)
'c' is opposite vertex C (AB edge)
Returns:
Line along the specified side
"""
def length(self, side) -> np.float64:
"""
Get length of specified side.
Parameters:
- side: str, side letter ('a', 'b', or 'c')
Returns:
Length of the specified side
"""
def angle(self, vertex) -> float:
"""
Calculate interior angle at specified vertex.
Parameters:
- vertex: str, vertex letter ('A', 'B', or 'C')
Returns:
Interior angle in radians at the vertex
"""
def centroid(self) -> Point:
"""
Calculate centroid (center of mass) of triangle.
Returns:
Centroid point (average of vertices)
"""
def altitude(self, vertex) -> Line:
"""
Get altitude line from specified vertex.
Parameters:
- vertex: str, vertex letter ('A', 'B', or 'C')
Returns:
Altitude line from vertex perpendicular to opposite side
"""
def orthocenter(self, **kwargs) -> Point:
"""
Calculate orthocenter (intersection of altitudes).
Parameters:
- **kwargs: additional keywords passed to line intersection
Returns:
Orthocenter point where altitudes meet
"""
def classify(self, **kwargs) -> str:
"""
Classify triangle by side lengths.
Parameters:
- **kwargs: additional keywords passed to numpy.allclose
Returns:
Classification string: 'equilateral', 'isosceles', or 'scalene'
"""
def is_right(self, **kwargs) -> bool:
"""
Check if triangle is a right triangle.
Parameters:
- **kwargs: additional keywords passed to math.isclose
Returns:
True if triangle has a right angle (90 degrees)
"""
def plot_2d(self, ax_2d, part='points', **kwargs):
"""
Plot triangle in 2D.
Parameters:
- ax_2d: matplotlib Axes, 2D plotting axes
- part: str, what to plot ('points', 'edges', or 'all')
- **kwargs: additional keywords passed to plotting functions
"""
def plot_3d(self, ax_3d, part='points', **kwargs):
"""
Plot triangle in 3D.
Parameters:
- ax_3d: matplotlib Axes3D, 3D plotting axes
- part: str, what to plot ('points', 'edges', or 'all')
- **kwargs: additional keywords passed to plotting functions
"""Usage Example:
from skspatial.objects import Triangle
import numpy as np
# Create triangle from three vertices
triangle = Triangle([0, 0], [3, 0], [1.5, 2.6])
# Calculate basic properties
area = triangle.area() # 3.9
perimeter = triangle.perimeter() # ~9.2
centroid = triangle.centroid() # Point([1.5, 0.87])
# Get vertices and sides
vertex_a = triangle.point('A') # Point([0, 0])
side_a = triangle.line('a') # Line from B to C
length_a = triangle.length('a') # Length of BC
# Calculate angles
angle_a = triangle.angle('A') # Angle at vertex A in radians
angle_degrees = np.degrees(angle_a) # Convert to degrees
# Classify triangle
classification = triangle.classify() # 'scalene'
is_right = triangle.is_right() # False
# Special points
orthocenter = triangle.orthocenter()
altitude_a = triangle.altitude('A') # Altitude from A to BC
# Right triangle example
right_triangle = Triangle([0, 0], [3, 0], [0, 4])
is_right = right_triangle.is_right() # True
classification = right_triangle.classify() # 'scalene' (3-4-5 triangle)
# Equilateral triangle example
import math
height = math.sqrt(3) / 2
equilateral = Triangle([0, 0], [1, 0], [0.5, height])
classification = equilateral.classify() # 'equilateral'
angle_60 = equilateral.angle('A') # π/3 radians (60 degrees)Install with Tessl CLI
npx tessl i tessl/pypi-scikit-spatial