simcats_datasets.support_functions.clip_line_to_rectangle

Helper functions for clipping lines into rectangles (into the CSD space).

Used to clip single transition lines into the CSD space to generate transition specific labels with the function get_lead_transition_labels from simcats_datasets.support_functions.get_lead_transition_labels.

@author: f.hader

Module Contents

Functions

clip_slope_line_to_rectangle

Clips a line segment with a given slope to a rectangle, extending it to either positive or negative infinity from the provided point.

clip_infinite_slope_line_to_rectangle

Clips a line segment with a given slope to a rectangle, extending it to positive and negative infinity from the provided point.

clip_point_line_to_rectangle

Clips a line segment defined by its start and end points to a rectangle.

is_point_inside_rectangle

Checks if a point is inside a rectangle defined by its corner points.

line_intersection

Calculates the intersection point between two line segments defined by their respective endpoints.

create_rectangle_corners

Creates rectangle corner points that form a rectangle around the specified x and y value ranges.

Module Implementation Details

simcats_datasets.support_functions.clip_line_to_rectangle.clip_slope_line_to_rectangle(slope, point, rect_corners, is_start=True)

Clips a line segment with a given slope to a rectangle, extending it to either positive or negative infinity from the provided point.

Parameters:
  • slope (float) – The slope of the line.

  • point (Tuple[float, float]) – A tuple (x, y) representing the starting or ending point of the line segment.

  • rect_corners (List[Tuple[float, float]]) – A list of four tuples, each representing the corner points of the rectangle.

  • is_start (bool) – Specifies whether the line extends to positive infinity (True) or negative infinity (False) from the provided point. Default is True (positive infinity).

Returns:

A tuple (start, end) representing the clipped line segment. Returns None if the line is entirely outside the rectangle.

Return type:

Union[Tuple[Tuple[float, float], Tuple[float, float]], None]

Notes

  • The function handles lines defined by a slope and a single point.

  • The ‘is_start’ parameter determines whether the line extends to positive or negative infinity from the provided point.

simcats_datasets.support_functions.clip_line_to_rectangle.clip_infinite_slope_line_to_rectangle(slope, point, rect_corners)

Clips a line segment with a given slope to a rectangle, extending it to positive and negative infinity from the provided point.

Parameters:
  • slope (float) – The slope of the line.

  • point (Tuple[float, float]) – A tuple (x, y) representing the starting or ending point of the line segment.

  • rect_corners (List[Tuple[float, float]]) – A list of four tuples, each representing the corner points of the rectangle.

Returns:

A tuple (start, end) representing the clipped line segment. Returns None if the line is entirely outside the rectangle.

Return type:

Union[Tuple[Tuple[float, float], Tuple[float, float]], None]

Notes

  • The function handles lines defined by a slope and a single point.

simcats_datasets.support_functions.clip_line_to_rectangle.clip_point_line_to_rectangle(start, end, rect_corners)

Clips a line segment defined by its start and end points to a rectangle.

Parameters:
  • start (Tuple[float, float]) – A tuple (x, y) representing the start point of the line.

  • end (Tuple[float, float]) – A tuple (x, y) representing the end point of the line.

  • rect_corners (List[Tuple[float, float]]) – A list of four tuples, each representing the corner points of the rectangle.

Returns:

A tuple representing the clipped line segment (start, end) if any part of the line is inside the rectangle. Returns None if the line is entirely outside the rectangle.

Return type:

Union[Tuple[Tuple[float, float], Tuple[float, float]], None]

Notes

  • The function handles lines defined by two points.

  • The function handles the case when the line is entirely inside the rectangle.

simcats_datasets.support_functions.clip_line_to_rectangle.is_point_inside_rectangle(point, rect_corners)

Checks if a point is inside a rectangle defined by its corner points.

Parameters:
  • point (Tuple[float, float]) – A tuple (x, y) representing the point to be checked.

  • rect_corners (List[Tuple[float, float]]) – A list of four tuples, each representing the corner points of the rectangle. it is assumed that these are sorted so that they from a course around the rectangle. Thus, the first and third (or alternatively the second and fourth) define the rectangle.

Returns:

True if the point is inside the rectangle, False otherwise.

Return type:

bool

simcats_datasets.support_functions.clip_line_to_rectangle.line_intersection(p1, p2, q1, q2)

Calculates the intersection point between two line segments defined by their respective endpoints.

Parameters:
  • p1 (Tuple[float, float]) – A tuple (x, y) representing the first endpoint of the first line.

  • p2 (Tuple[float, float]) – A tuple (x, y) representing the second endpoint of the first line.

  • q1 (Tuple[float, float]) – A tuple (x, y) representing the first endpoint of the second line.

  • q2 (Tuple[float, float]) – A tuple (x, y) representing the second endpoint of the second line.

Returns:

A tuple (x, y) representing the intersection point if the lines intersect. Returns None if the lines are parallel or do not intersect.

Return type:

Union[Tuple[float, float], None]

simcats_datasets.support_functions.clip_line_to_rectangle.create_rectangle_corners(x_range, y_range)

Creates rectangle corner points that form a rectangle around the specified x and y value ranges.

Parameters:
  • x_range (Tuple[float, float]) – A tuple (x_min, x_max) representing the minimum and maximum x values.

  • y_range (Tuple[float, float]) – A tuple (y_min, y_max) representing the minimum and maximum y values.

Returns:

bottom-left, bottom-right, top-right, top-left.

Return type:

A list of four tuples, each representing the corner points of the rectangle in the following order