Skip to content

Commit 331e35a

Browse files
committed
Finish rect2
More tests for rect-rect intersection would be good. Found a bug with polygon from_regular. Added regression test and fixed it. * pygorithm/geometry/polygon2.py - Fix a bug (math error, see doc changes) * pygorithm/geometry/rect2.py - Implement all the things * tests/test_geometry.py - test all the things for rect2
1 parent 5ccf2a2 commit 331e35a

File tree

3 files changed

+454
-65
lines changed

3 files changed

+454
-65
lines changed

pygorithm/geometry/polygon2.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,9 @@ def from_regular(cls, sides, length, start_rads = None, start_degs = None, cente
205205
206206
Finally, each vertex is found using ``<radius * cos(angle), radius * sin(angle)>``
207207
208-
If the center is not specified, the bounding box of the polygon is calculated while the vertices
209-
are being found, and the center of the bounding box is set to the center of the circle. This
210-
is never greater than (circumradius, circumradius).
208+
If the center is not specified, the minimum of the bounding box of the
209+
polygon is calculated while the vertices are being found, and the inverse
210+
of that value is offset to the rest of the points in the polygon.
211211
212212
:param sides: the number of sides in the polygon
213213
:type sides: :class:`numbers.Number`
@@ -251,8 +251,6 @@ def from_regular(cls, sides, length, start_rads = None, start_degs = None, cente
251251
pts = []
252252
_minx = 0
253253
_miny = 0
254-
_maxx = 0
255-
_maxy = 0
256254
for i in range(sides):
257255
x = center.x + math.cos(angle) * radius
258256
y = center.y + math.sin(angle) * radius
@@ -262,13 +260,11 @@ def from_regular(cls, sides, length, start_rads = None, start_degs = None, cente
262260
if _recenter:
263261
_minx = min(_minx, x)
264262
_miny = min(_miny, y)
265-
_maxx = max(_maxx, x)
266-
_maxy = max(_maxy, y)
267263

268264
if _recenter:
269-
_newcenter = vector2.Vector2((_maxx - _minx) / 2, (_maxy - _miny) / 2)
265+
_offset = vector2.Vector2(-_minx, -_miny)
270266
for i in range(sides):
271-
pts[i] += _newcenter
267+
pts[i] += _offset
272268

273269
return cls(pts, suppress_errors = True)
274270

@@ -520,7 +516,7 @@ def _create_link(pts):
520516
:type pts: list of :class:`pygorithm.geometry.vector2.Vector2`
521517
"""
522518

523-
param0 = "+".join(('%28{}%2C+{}%29'.format(v.x, v.y)) for v in pts)
519+
param0 = "+".join(('%28{}%2C+{}%29'.format(round(v.x, 3), round(v.y, 3))) for v in pts)
524520
xmin = pts[0].x
525521
xmax = xmin
526522
ymin = pts[1].y

0 commit comments

Comments
 (0)