Skip to content

Commit c3e68b6

Browse files
committed
Merge branch 'dev' of https://github.com/OmkarPathak/pygorithms into dev
'Pulled Requests'
2 parents b09a4bf + 990bed0 commit c3e68b6

File tree

5 files changed

+1095
-16
lines changed

5 files changed

+1095
-16
lines changed

docs/Geometry.rst

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,11 @@ Features
5050

5151
* Shapes available:
5252
- Concave Polygons (polygon2)
53+
- Rectangles (rect2)
5354

5455
* Algorithms available:
5556
- Separating Axis Theorem (polygon2)
57+
- Broad-phase (rect2)
5658

5759
Vector2
5860
-------
@@ -82,9 +84,13 @@ Concave Polygon
8284
:members:
8385
:special-members:
8486

85-
86-
87+
Axis-Aligned Rectangle
88+
----------------------
8789

90+
.. autoclass:: pygorithm.geometry.rect2.Rect2
91+
:members:
92+
:special-members:
93+
:private-members:
8894

8995

9096

pygorithm/data_structures/quadtree.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
Author: Timothy Moore
3+
Created On: 31th August 2017
4+
5+
Defines a two-dimensional quadtree of arbitrary
6+
depth and bucket size.
7+
"""
8+
import inspect
9+
10+
from pygorithm.geometry import (vector2, polygon2, rect2)
11+
12+
class QuadTree(object):
13+
"""
14+
A quadtree is a sorting tool for two-dimensional space, most
15+
commonly used to reduce the number of required collision
16+
calculations in a two-dimensional scene. In this context,
17+
the scene is stepped without collision detection, then a
18+
quadtree is constructed from all of the boundaries
19+
"""
20+
@staticmethod
21+
def get_code():
22+
"""
23+
Get the code for the QuadTree class
24+
25+
:returns: code for QuadTree
26+
:rtype: string
27+
"""
28+
return inspect.getsource(QuadTree)

pygorithm/geometry/polygon2.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Polygon2(object):
1919
index to the larger index will walk clockwise around the polygon.
2020
2121
.. note::
22-
22+
2323
Polygons should be used as if they were completely immutable to
2424
ensure correctness. All attributes of Polygon2 can be reconstructed
2525
from the points array, and thus cannot be changed on their own and
@@ -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

@@ -407,8 +403,8 @@ def contains_point(polygon, offset, point):
407403
:type offset: :class:`pygorithm.geometry.vector2.Vector2` or None
408404
:param point: the point to check
409405
:type point: :class:`pygorithm.geometry.vector2.Vector2`
410-
:returns: (on edge, contained)
411-
:rtype: (bool, bool)
406+
:returns: on edge, contained
407+
:rtype: bool, bool
412408
"""
413409

414410
_previous = polygon.points[0]
@@ -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)