twod
twod
¶
two-dimensional geometric objects for humans™
Classes¶
CPoint(x: float | int = 0, y: float | int = 0)
¶
Functions¶
ccw(b: Any, c: Any) -> float
¶
Returns a float indicating the winding direction of the points [self, b, c].
If result > 0, clock-wise winding. If result < 0, counter clock-wise winding (ccw). If result = 0, the three points are colinear.
Note: ccw is also 2*area of the triangle [self, b, c]
Line(start: Point = None, end: Point = None)
dataclass
¶
A line segment defined by two points.
The Line class represents a geometric line segment with start and end points and provides many helpful properties and methods for geometric operations.
line = Line(Point(0, 0), Point(3, 4)) line.length 5.0 line.angle_degrees 53.13010235415598
Attributes¶
length: float
property
¶
The length of this line segment.
length_squared: float
property
¶
The squared length of this line segment (faster than length).
vector: Point
property
¶
The vector from start to end point.
direction: Point
property
¶
The unit vector in the direction from start to end.
angle_radians: float
property
¶
The angle of this line in radians, measured counter-clockwise from positive x-axis.
angle_degrees: float
property
¶
The angle of this line in degrees, measured counter-clockwise from positive x-axis.
slope: float
property
¶
The slope of this line (rise over run).
midpoint: Point
property
¶
The midpoint of this line segment.
is_point: bool
property
¶
True if this line has zero length (start == end).
is_horizontal: bool
property
¶
True if this line is horizontal (same y coordinates).
is_vertical: bool
property
¶
True if this line is vertical (same x coordinates).
Functions¶
reverse() -> Line
¶
Return a new line with start and end points swapped.
extend(distance: float, from_end: bool = True) -> Line
¶
Extend the line by a given distance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
distance
|
float
|
Distance to extend (positive extends outward, negative inward) |
required |
from_end
|
bool
|
If True, extend from end point; if False, extend from start point |
True
|
Returns:
| Type | Description |
|---|---|
Line
|
New Line extended by the specified distance |
point_at_parameter(t: float) -> Point
¶
Get point on line at parameter t.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t
|
float
|
Parameter where 0 = start point, 1 = end point |
required |
Returns:
| Type | Description |
|---|---|
Point
|
Point on the line at parameter t |
distance_to_point(point: Point) -> float
¶
Calculate the shortest distance from a point to this line segment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
point
|
Point
|
Point to calculate distance to |
required |
Returns:
| Type | Description |
|---|---|
float
|
Shortest distance from point to line segment |
closest_point_on_line(point: Point) -> Point
¶
intersects_line(other: Line) -> bool
¶
Check if this line segment intersects with another line segment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Line
|
Other line segment to check intersection with |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the line segments intersect |
intersection_point(other: Line) -> Optional[Point]
¶
parallel_to(other: Line) -> bool
¶
Check if this line is parallel to another line.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Line
|
Other line to check parallelism with |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if lines are parallel |
perpendicular_to(other: Line) -> bool
¶
Check if this line is perpendicular to another line.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Line
|
Other line to check perpendicularity with |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if lines are perpendicular |
angle_between(other: Line) -> float
¶
Calculate the angle between this line and another line in radians.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Line
|
Other line to calculate angle with |
required |
Returns:
| Type | Description |
|---|---|
float
|
Angle between lines in radians (0 to π) |
contains_point(point: Point, tolerance: float = 1e-10) -> bool
¶
Check if a point lies on this line segment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
point
|
Point
|
Point to check |
required |
tolerance
|
float
|
Tolerance for floating point comparison |
1e-10
|
Returns:
| Type | Description |
|---|---|
bool
|
True if point lies on the line segment |
Point(x: float = 0.0, y: float = 0.0)
dataclass
¶
Attributes¶
y: float = 0.0
class-attribute
instance-attribute
¶
A two dimensional geometric point.
The Point class represents a geometric point with 'x' and 'y' attributes and has many helpful properties and methods.
p = Point() p.is_origin True
is_origin: bool
property
¶
True if and only if x == 0 and y == 0.
quadrant: Quadrant
property
¶
The quadrant in the cartesian plane this point is located in.
Possible values are: - Quadrant.FIRST - Quadrant.SECOND - Quadrant.THIRD - Quadrant.FOURTH - Quadrant.ORIGIN - Quadrant.X_AXIS - Quadrant.Y_AXIS
radius: float
property
writable
¶
The distance from this point to the origin.
radians: float
property
writable
¶
The angle in radians measured counter-clockwise from 3 o'clock.
degrees: float
property
writable
¶
The angle in degrees measured counter-clockwise from 3 o'clock.
polar: tuple[float, float]
property
writable
¶
Polar coordinates tuple: (R, ϴ).
R is the distance from the origin to this point. ϴ is the angle (radians) measured counter-clockwise from 3 o'clock.
polar_deg: tuple[float, float]
property
writable
¶
Polar coordinates tuple: (R, ϴ).
R is the distance from the origin to this point. ϴ is the angle (degrees) measured counter-clockwise from 3 o'clock.
xy: tuple[float, float]
property
writable
¶
A tuple of this point's x and y coordinates.
Functions¶
from_polar(radius: float, theta: float, is_radians: bool = True, translate: Optional[Point] = None) -> Point
classmethod
¶
Returns a Point with polar coordinates (R, ϴ).
The point is genrated relative to the origin.
:param float radius: :param float theta: :param bool is_radians: :return: Point
distance(other: Optional[Point] = None) -> float
¶
Return the floating point distance between self and other.
If other is not given, the distance from self to the origin is returned.
:param other: Point :return: float
distance_squared(other: Optional[Point] = None) -> float
¶
Return the floating point squared distance between self and other.
If other is not given, the squared distance from self to the origin is returned.
:param other: Point :return: float
dot(other: Point) -> float
¶
Return a scalar dot product of self with other.
:param other: Point :return: float
cross(other: Point) -> float
¶
Return a scalar cross product of self with other.
:param other: Point :return: float
ccw(b: Point, c: Point) -> float
¶
Return a floating point value indicating the winding direction of the points [self, b, c].
If ccw < 0, clock-wise winding If ccw > 0, counter clock-wise winding If ccw == 0, the three points are colinear
Note: ccw is also 2*area of the triangle [self, b, c].
:param b: Point :param c: Point :return: float
is_ccw(b: Point, c: Point) -> bool
¶
Return True if the angle [self, b, c] has counter clock-wise winding, else False.
Raises the exception ColinearPoints if the points compose a line.
:param b: Point :param c: Point :return: bool
is_colinear(b: Point, c: Point) -> bool
¶
True if the angle [self, b, c ] is a line, else False.
:param b: Point :param c: Point :return: bool
midpoint(other: Optional[Point] = None) -> Point
¶
Return a new Point midway between self and other..
If other is not given, the midpoint between self and the origin is returned.
:param other: Point :return: Point
between(p: Point, q: Point) -> bool
¶
True if self is bounded by the points [p, q], else False
The bounds are checked by less than or equal to (<=) so self is considered between if it resides on any of the lines constructed using [p,q].
:param p: Point :param q: Point :return: bool
inside(p: Point, q: Point) -> bool
¶
True if point is bounded by the points (p, q), else False
The bounds are checked by less than (<) so point is considered inside if it does not reside on any of the lines constructed using (p,q).
:param p: Point :param q: Point :return: bool
Rect(x: float | int = 0, y: float | int = 0, w: float | int = 0, h: float | int = 0)
¶
A rectangle specified by an origin at (x,y) and dimensions (w,h).
Attributes¶
A: Point
property
¶
Point at (x,y).
B: Point
property
¶
Point at (x+w, y).
C: Point
property
¶
Point at (x+w, y+h).
D: Point
property
¶
Point at (x, y+h).
vertices: list[Point]
property
¶
The points A, B, C, and D in a list.
sides: list[float]
property
¶
The lengths of each side: AB, BC, CD, and DA.
center: Point
property
writable
¶
Point at the center of the rectangle (midpoint of AC).
perimeter: float
property
¶
The distance around this rectangle.
area: float
property
¶
The area of this rectangle.