Skip to content

perependicular_line

cellseg_gsontools.lines.perpendicular_line(line, seg_length)

Create a perpendicular line from a line segment.

Note

Returns an empty line if perpendicular line is not possible from the input.

Parameters:

Name Type Description Default
line LineString

Line segment to create a perpendicular line from.

required
seg_length float

Length of the perpendicular line.

required

Returns:

Type Description
LineString

shapely.LineString: Perpendicular line to the input line of length seg_length.

Source code in cellseg_gsontools/lines.py
def perpendicular_line(
    line: shapely.LineString, seg_length: float
) -> shapely.LineString:
    """Create a perpendicular line from a line segment.

    Note:
        Returns an empty line if perpendicular line is not possible from the input.

    Parameters:
        line (shapely.LineString):
            Line segment to create a perpendicular line from.
        seg_length (float):
            Length of the perpendicular line.

    Returns:
        shapely.LineString:
            Perpendicular line to the input line of length `seg_length`.
    """
    left = line.parallel_offset(seg_length / 2, "left").centroid
    right = line.parallel_offset(seg_length / 2, "right").centroid

    if left.is_empty or right.is_empty:
        return shapely.LineString()

    return shapely.LineString([left, right])