Skip to content

Polyline

a polyline curve, defined by a set of vertices.

value instance-attribute

value: List[float]

length property writable

length: float

is_closed

is_closed(tolerance: float = 1e-06) -> bool

check if the polyline is closed (start point equals end point within tolerance)

Source code in src/specklepy/objects/geometry/polyline.py
def is_closed(self, tolerance: float = 1e-6) -> bool:
    """
    check if the polyline is closed (start point equals end point within tolerance)
    """
    if len(self.value) < 6:  # need at least 2 points to be closed
        return False

    # compare first and last points
    start = Point(
        x=self.value[0], y=self.value[1], z=self.value[2], units=self.units
    )
    end = Point(
        x=self.value[-3], y=self.value[-2], z=self.value[-1], units=self.units
    )
    return start.distance_to(end) <= tolerance

calculate_length

calculate_length() -> float
Source code in src/specklepy/objects/geometry/polyline.py
def calculate_length(self) -> float:
    points = self.get_points()
    total_length = 0.0
    for i in range(len(points) - 1):
        total_length += points[i].distance_to(points[i + 1])
    if self.is_closed() and points:
        total_length += points[-1].distance_to(points[0])
    return total_length

get_points

get_points() -> List[Point]

converts the raw coordinate list into Point objects

Source code in src/specklepy/objects/geometry/polyline.py
def get_points(self) -> List[Point]:
    """
    converts the raw coordinate list into Point objects
    """
    if len(self.value) % 3 != 0:
        raise ValueError(
            "Polyline value list is malformed: expected length to be multiple of 3"
        )

    points = []
    for i in range(0, len(self.value), 3):
        point = Point(
            x=self.value[i],
            y=self.value[i + 1],
            z=self.value[i + 2],
            units=self.units,
        )
        points.append(point)
    return points