Geometry
Module: terminaltexteffects.utils.geometry
This module provides utility functions for geometric calculations and operations.
The purpose of these functions is to find terminal coordinates that fall within certain regions or along certain paths. These functions are used by effects to enable more complex animations and movement paths.
Functions:
Name | Description |
---|---|
find_coords_on_circle |
Finds points on a circle given the origin, radius, and number of points. |
find_coords_in_circle |
Finds coordinates within an ellipse given the center and major axis length. |
find_coords_in_rect |
Finds coordinates within a rectangle given the origin and distance. |
find_coord_at_distance |
Finds the coordinate at a given distance along a line defined by two coordinates. |
find_coord_on_bezier_curve |
Finds points on a quadratic or cubic bezier curve. |
find_coord_on_line |
Finds points on a line. |
find_length_of_bezier_curve |
Finds the length of a quadratic or cubic bezier curve. |
find_length_of_line |
Finds the length of a line intersecting two coordinates. |
find_normalized_distance_from_center |
Returns the normalized distance from the center of the Canvas. |
Coord
dataclass
A coordinate with row and column values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
column |
int
|
column value |
required |
row |
int
|
row value |
required |
Source code in terminaltexteffects/utils/geometry.py
find_coord_at_distance(origin, target, distance)
Finds the coordinate at the given distance along the line defined by the origin and target coordinates.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
origin |
Coord
|
origin coordinate (a) |
required |
target |
Coord
|
target coordinate (b) |
required |
distance |
float
|
distance from the target coordinate (b), away from the origin coordinate (a) |
required |
Returns:
Name | Type | Description |
---|---|---|
Coord |
Coord
|
Coordinate at the given distance (c). |
Source code in terminaltexteffects/utils/geometry.py
find_coord_on_bezier_curve(start, control, end, t)
Finds points on a quadratic or cubic bezier curve.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
start |
Coord
|
The starting coordinate of the curve. |
required |
control |
tuple[Coord, ...] | Coord
|
The control point(s) of the curve. For a quadratic bezier curve, a single control point is expected. For a cubic bezier curve, two control points are expected. |
required |
end |
Coord
|
The ending coordinate of the curve. |
required |
t |
float
|
The parameter value between 0 and 1 that determines the position on the curve. |
required |
Returns:
Name | Type | Description |
---|---|---|
Coord |
Coord
|
The coordinate on the bezier curve corresponding to the given parameter value. |
Source code in terminaltexteffects/utils/geometry.py
find_coord_on_line(start, end, t)
Finds points on a line.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
start |
Coord
|
The starting coordinate of the line. |
required |
end |
Coord
|
The ending coordinate of the line. |
required |
t |
float
|
The parameter value between 0 and 1 representing the position on the line. |
required |
Returns:
Name | Type | Description |
---|---|---|
Coord |
Coord
|
The coordinate on the line corresponding to the given parameter value. |
Source code in terminaltexteffects/utils/geometry.py
find_coords_in_circle(center, diameter)
Find the coordinates within an circle given the center and diameter. The actual shape calculated is an ellipse with a major axis of length diameter, however the terminal cell height/width ratio creates a circle visually.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
center |
Coord
|
The center coordinate of the circle. |
required |
diameter |
int
|
The length of the major axis of the circle. |
required |
Returns:
Type | Description |
---|---|
list[Coord]
|
list[Coord]: A list of coordinates within the circle. |
Source code in terminaltexteffects/utils/geometry.py
find_coords_in_rect(origin, distance)
Find coords that fall within a rectangle with the given origin and distance from the origin. Distance specifies the number of units in each direction from the origin. Final width = 2 * distance + 1, final height = 2 * distance + 1.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
origin |
Coord
|
center of the rectangle |
required |
distance |
int
|
distance from the origin |
required |
Returns:
Type | Description |
---|---|
list[Coord]
|
list[Coord]: list of Coord points in the rectangle |
Source code in terminaltexteffects/utils/geometry.py
find_coords_on_circle(origin, radius, coords_limit=0, unique=True)
Finds points on a circle.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
origin |
Coord
|
origin of the circle |
required |
radius |
int
|
radius of the circle |
required |
coords_limit |
int
|
limit the number of coords returned, if 0, the number of points is calculated based on the circumference of the circle |
0
|
unique |
bool
|
whether to remove duplicate points. Defaults to True. |
True
|
Returns:
Name | Type | Description |
---|---|---|
list |
Coord
|
list of Coord points on the circle |
Source code in terminaltexteffects/utils/geometry.py
find_length_of_bezier_curve(start, control, end)
Finds the length of a quadratic or cubic bezier curve.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
start |
Coord
|
The starting coordinate of the curve. |
required |
control |
tuple[Coord, ...] | Coord
|
The control point(s) of the curve. |
required |
end |
Coord
|
The ending coordinate of the curve. |
required |
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
The length of the bezier curve. |
Source code in terminaltexteffects/utils/geometry.py
find_length_of_line(coord1, coord2, double_row_diff=False)
Returns the length of the line intersecting coord1 and coord2. If double_row_diff is True, the distance is doubled to account for the terminal character height/width ratio.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
coord1 |
Coord
|
first coordinate. |
required |
coord2 |
Coord
|
second coordinate. |
required |
double_row_diff |
bool
|
whether to double the row difference to account for terminal character height/width ratio. Defaults to False. |
False
|
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
length of the line |
Source code in terminaltexteffects/utils/geometry.py
find_normalized_distance_from_center(max_row, max_column, other_coord)
Returns the normalized distance from the center of the Canvas as a float between 0 and 1.
The distance is calculated using the Pythagorean theorem and accounts for the aspect ratio of the terminal.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
max_row |
int
|
Maximum row value of the Canvas. |
required |
max_column |
int
|
Maximum column value of the Canvas. |
required |
other_coord |
Coord
|
Other coordinate from which to calculate the distance. |
required |
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
Normalized distance from the center of the Canvas, float between 0 and 1. |