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)
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
topoint2
.>>> 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
topoint2
. This version is faster thandistance_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 apoint
.
- 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 pointline_start
andline_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 inouter
rect.
- gaphas.geometry.rectangle_intersects(recta: tuple[float, float, float, float], rectb: tuple[float, float, float, float]) bool [source]¶
Return True if
recta
andrectb
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
andrectb
. If they do not intersect,None
is returned.>>> rectangle_clip((0, 0, 20, 20), (10, 10, 20, 20)) (10, 10, 10, 10)