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], point: Tuple[float, float]) float[source]

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

>>> distance_rectangle_point(Rectangle(0, 0, 10, 10), (11, -1))
2
>>> distance_rectangle_point((0, 0, 10, 10), (11, -1))
2
>>> distance_rectangle_point((0, 0, 10, 10), (-1, 11))
2
gaphas.geometry.point_on_rectangle(rect: Tuple[float, float, float, float], 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.

>>> point_on_rectangle(Rectangle(0, 0, 10, 10), (11, -1))
(10, 0)
>>> point_on_rectangle((0, 0, 10, 10), (5, 12))
(5, 10)
>>> point_on_rectangle(Rectangle(0, 0, 10, 10), (12, 5))
(10, 5)
>>> point_on_rectangle(Rectangle(1, 1, 10, 10), (3, 4))
(3, 4)
>>> point_on_rectangle(Rectangle(1, 1, 10, 10), (0, 3))
(1, 3)
>>> point_on_rectangle(Rectangle(1, 1, 10, 10), (4, 3))
(4, 3)
>>> point_on_rectangle(Rectangle(1, 1, 10, 10), (4, 9), border=True)
(4, 11)
>>> point_on_rectangle((1, 1, 10, 10), (4, 6), border=True)
(1, 6)
>>> point_on_rectangle(Rectangle(1, 1, 10, 10), (5, 3), border=True)
(5, 1)
>>> point_on_rectangle(Rectangle(1, 1, 10, 10), (8, 4), border=True)
(11, 4)
>>> point_on_rectangle((1, 1, 10, 100), (5, 8), border=True)
(1, 8)
>>> point_on_rectangle((1, 1, 10, 100), (5, 98), border=True)
(5, 101)
gaphas.geometry.distance_line_point(line_start: Tuple[float, float], line_end: Tuple[float, float], point: Tuple[float, float]) tuple[float, typing.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.

>>> distance_line_point((0., 0.), (2., 4.), point=(3., 4.))
(1.0, (2.0, 4.0))
>>> distance_line_point((0., 0.), (2., 4.), point=(-1., 0.))
(1.0, (0.0, 0.0))
>>> distance_line_point((0., 0.), (2., 4.), point=(1., 2.))
(0.0, (1.0, 2.0))
>>> d, p = distance_line_point((0., 0.), (2., 4.), point=(2., 2.))
>>> f"{d:.3f}"
'0.894'
>>> f"({p[0]:.3f}, {p[1]:.3f})"
'(1.200, 2.400)'
gaphas.geometry.intersect_line_line(line1_start: Point, line1_end: Point, line2_start: Point, line2_end: Point) Point | 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: Rect, rectb: Rect) Rect | 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)