Rectangle

class gaphas.geometry.Rectangle(x: float = 0, y: float = 0, width: float | None = None, height: float | None = None, x1: float = 0, y1: float = 0)[source]

Python Rectangle implementation. Rectangles can be added (union), substituted (intersection) and points and rectangles can be tested to be in the rectangle.

>>> r1= Rectangle(1,1,5,5)
>>> r2 = Rectangle(3,3,6,7)

Test if two rectangles intersect:

>>> if r1 - r2: 'yes'
'yes'
>>> r1, r2 = Rectangle(1,2,3,4), Rectangle(1,2,3,4)
>>> r1 == r2
True
>>> r = Rectangle(-5, 3, 10, 8)
>>> r.width = 2
>>> r
Rectangle(-5, 3, 2, 8)
>>> r = Rectangle(-5, 3, 10, 8)
>>> r.height = 2
>>> r
Rectangle(-5, 3, 10, 2)
expand(delta: float) None[source]
>>> r = Rectangle(-5, 3, 10, 8)
>>> r.expand(5)
>>> r
Rectangle(-10, -2, 20, 18)
tuple() tuple[float, float, float, float][source]

A type safe version of tuple(rectangle).

Geometry functions

gaphas.geometry.distance_point_point(point1: Tuple[float, float], point2: Tuple[float, float] = (0.0, 0.0)) float[source]

Return the distance from point point1 to point2.

>>> f"{distance_point_point((0,0), (1,1)):.3f}"
'1.414'
gaphas.geometry.distance_point_point_fast(point1: Tuple[float, float], point2: Tuple[float, float] = (0.0, 0.0)) float[source]

Return the distance from point point1 to point2. This version is faster than distance_point_point(), but less precise.

>>> distance_point_point_fast((0,0), (1,1))
2
gaphas.geometry.distance_rectangle_point(rect: Tuple[float, float, float, float] | Rectangle, point: Tuple[float, float]) float[source]

Return the distance (fast) from a rectangle (x, y, width,height) to a point.

gaphas.geometry.point_on_rectangle(rect: Tuple[float, float, float, float] | Rectangle, point: Tuple[float, float], border: bool = False) Tuple[float, float][source]

Return the point on which point can be projecten on the rectangle.

border = True will make sure the point is bound to the border of the reactangle. Otherwise, if the point is in the rectangle, it’s okay.

gaphas.geometry.distance_line_point(line_start: Tuple[float, float], line_end: Tuple[float, float], point: Tuple[float, float]) tuple[float, Tuple[float, float]][source]

Calculate the distance of a point from a line. The line is marked by begin and end point line_start and line_end.

A tuple is returned containing the distance and point on the line.

gaphas.geometry.intersect_line_line(line1_start: Tuple[float, float], line1_end: Tuple[float, float], line2_start: Tuple[float, float], line2_end: Tuple[float, float]) Tuple[float, float] | None[source]

Find the point where the lines (segments) defined by (line1_start, line1_end) and (line2_start, line2_end) intersect. If no intersection occurs, None is returned.

>>> intersect_line_line((3, 0), (8, 10), (0, 0), (10, 10))
(6, 6)
>>> intersect_line_line((0, 0), (10, 10), (3, 0), (8, 10))
(6, 6)
>>> intersect_line_line((0, 0), (10, 10), (8, 10), (3, 0))
(6, 6)
>>> intersect_line_line((8, 10), (3, 0), (0, 0), (10, 10))
(6, 6)
>>> intersect_line_line((0, 0), (0, 10), (3, 0), (8, 10))
>>> intersect_line_line((0, 0), (0, 10), (3, 0), (3, 10))

Ticket #168: >>> intersect_line_line((478.0, 117.0), (478.0, 166.0), (527.5, 141.5), (336.5, 139.5)) (478.5, 141.48167539267016) >>> intersect_line_line((527.5, 141.5), (336.5, 139.5), (478.0, 117.0), (478.0, 166.0)) (478.5, 141.48167539267016)

This is a Python translation of the lines_intersect, C Code from Graphics Gems II, Academic Press, Inc. The original routine was written by Mukesh Prasad.

EULA: The Graphics Gems code is copyright-protected. In other words, you cannot claim the text of the code as your own and resell it. Using the code is permitted in any program, product, or library, non-commercial or commercial. Giving credit is not required, though is a nice gesture. The code comes as-is, and if there are any flaws or problems with any Gems code, nobody involved with Gems - authors, editors, publishers, or webmasters - are to be held responsible. Basically, don’t be a jerk, and remember that anything free comes with no guarantee.

gaphas.geometry.rectangle_contains(inner: Tuple[float, float, float, float], outer: Tuple[float, float, float, float]) bool[source]

Returns True if inner rect is contained in outer rect.

gaphas.geometry.rectangle_intersects(recta: Tuple[float, float, float, float], rectb: Tuple[float, float, float, float]) bool[source]

Return True if recta and rectb intersect.

>>> rectangle_intersects((5,5,20, 20), (10, 10, 1, 1))
True
>>> rectangle_intersects((40, 30, 10, 1), (1, 1, 1, 1))
False
gaphas.geometry.rectangle_clip(recta: Tuple[float, float, float, float], rectb: Tuple[float, float, float, float]) Tuple[float, float, float, float] | None[source]

Return the clipped rectangle of recta and rectb. If they do not intersect, None is returned.

>>> rectangle_clip((0, 0, 20, 20), (10, 10, 20, 20))
(10, 10, 10, 10)