Skip to content

Commit dcbb1b8

Browse files
committed
Create Polygon2 docstrings
Creates skeleton for polygon2 * docs/Geometry.rst - Use autoclass instead of duplicated documentation * pygorithm/geometry/line2.py - Add spaces around default param values * pygorithm/geometry/polygon2.py - Add skeleton and docstrings
1 parent 3397e93 commit dcbb1b8

File tree

3 files changed

+149
-88
lines changed

3 files changed

+149
-88
lines changed

docs/Geometry.rst

Lines changed: 3 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -80,94 +80,10 @@ Axis-Aligned Line
8080
Concave Polygon
8181
---------------
8282

83-
.. class:: Polygon2
84-
85-
Defines a concave polygon defined by a list of points such that each
86-
adjacent pair of points form a line, the line from the last point to
87-
the first point form a line, and the lines formed from the smaller
88-
index to the larger index will walk clockwise around the polygon.
89-
90-
.. note::
91-
92-
Polygons should be used as if they were completely immutable to
93-
ensure correctness. All attributes of Polygon2 can be reconstructed
94-
from the points array, and thus cannot be changed on their own and
95-
must be recalculated if there were any changes to `points`.
96-
97-
.. note::
98-
99-
To reduce unnecessary recalculations, Polygons notably do not have
100-
an easily modifiable position. However, where relevant, the class
101-
methods will accept offsets to the polygons.
102-
103-
.. note::
104-
105-
Unfortunately, operations on rotated polygons require recalculating
106-
the polygon based on its rotated points. This should be avoided
107-
unless necessary through the use of Axis-Aligned Bounding Boxes
108-
and similar tools.
109-
110-
.. attribute:: Polygon2.points
111-
112-
The ordered list of Vector2s in this polygon.
113-
114-
.. attribute:: Polygon2.lines
115-
116-
The ordered list of Line2s in this polygon.
117-
118-
.. attribute:: Polygon2.normals
119-
120-
The orders list of normal Vector2s corresponding to the Line2s
121-
in lines
122-
123-
.. attribute:: Polygon2.center
124-
125-
The Vector2 center of the polygon. Lazily initialized.
126-
127-
.. attribute:: Polygon2.aabb
128-
129-
The Rect2 bounding box of this polygon. Lazily initialized.
130-
131-
.. method:: Polygon2(self, points)
132-
133-
- **points** - The ordered list of points in this polygon.
134-
135-
.. staticmethod:: Polygon2.contains_point(polygon, offset, vec)
136-
137-
- **polygon** - polygon to check
138-
- **offset** - offset of the polygon
139-
- **vec** - the vector to check if is inside the polyogn at offset
140-
141-
Determines if the polygon contains vector when at the specified offset.
142-
143-
.. staticmethod:: Polygon2.contains_polygon(poly1, poly2, offset1, offset2)
144-
145-
- **poly1** - The polygon that might contain poly2
146-
- **poly2** - The polygon that might be contained in poly1
147-
- **offset1** - The position of the first polygon
148-
- **offset2** - The position of the second polygon
149-
- **Return Value** - `bool` true if poly2 is completely contained in poly1, false otherwise
150-
151-
Determines if the second polygon is completely contained in the first
152-
polygon.
153-
154-
.. staticmethod:: Polygon2.find_intersection(poly1, poly2, offset1, offset2)
83+
.. autoclass:: pygorithm.geometry.polygon2.Polygon2
84+
:members:
85+
:special-members:
15586

156-
- **poly1** - The first polygon
157-
- **poly2** - The second polygon
158-
- **offset1** - The offset of the first polygon
159-
- **offset2** - The offset of the second polygon
160-
- **Return Value** - `bool, None or Vector2` If there is intersection, the MTV for poly1 or None
161-
162-
Determines if the two polygons intersect, and how to prevent
163-
intersection by a move on the first polygon. If the polygons
164-
do not intersect, returns `false, None`. If the polygons
165-
intersect and overlap, returns `true, Vector2`. If the polygons
166-
are touching but do not overlap, returns `true, None`.
167-
168-
.. note::
169-
170-
Ensure that the result `true, None` is handled correctly.
17187

17288

17389

pygorithm/geometry/line2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ def calculate_y_intercept(offset):
275275
pass
276276

277277
@staticmethod
278-
def find_intersection(line1, line2, offset1=None, offset2=None, find_mtv=True):
278+
def find_intersection(line1, line2, offset1 = None, offset2 = None, find_mtv = True):
279279
"""
280280
Find the intersection between the two lines.
281281

pygorithm/geometry/polygon2.py

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
"""
2+
polygon2
3+
4+
Author: Timothy Moore
5+
6+
Defines a class for simple 2-d convex polygons. Contains
7+
SAT-intersection.
8+
"""
9+
10+
class Polygon2(object):
11+
"""
12+
Define a concave polygon defined by a list of points such that each
13+
adjacent pair of points form a line, the line from the last point to
14+
the first point form a line, and the lines formed from the smaller
15+
index to the larger index will walk clockwise around the polygon.
16+
17+
.. note::
18+
19+
Polygons should be used as if they were completely immutable to
20+
ensure correctness. All attributes of Polygon2 can be reconstructed
21+
from the points array, and thus cannot be changed on their own and
22+
must be recalculated if there were any changes to `points`.
23+
24+
.. note::
25+
26+
To reduce unnecessary recalculations, Polygons notably do not have
27+
an easily modifiable position. However, where relevant, the class
28+
methods will accept offsets to the polygons. In all of these cases
29+
the offset may be None for a minor performance improvement.
30+
31+
.. note::
32+
33+
Unfortunately, operations on rotated polygons require recalculating
34+
the polygon based on its rotated points. This should be avoided
35+
unless necessary through the use of Axis-Aligned Bounding Boxes
36+
and similar tools.
37+
38+
:ivar points: the ordered set of points on this polygon
39+
:vartype points: list of :class:`pygorithm.geometry.vector2.Vector2`
40+
41+
:ivar lines: the ordered set of lines on this polygon
42+
:vartype lines: list of :class:`pygorithm.geometry.line2.Line2`
43+
44+
:ivar normals: the ordered set of normals on this polygon
45+
:vartype normals: list of :class:`pygorithm.geometry.vector2.Vector2`
46+
47+
:ivar center: the center of this polygon when unshifted.
48+
:vartype center: :class:`pygorithm.geometry.vector2.Vector2`
49+
"""
50+
51+
def __init__(self, points, suppress_errors = False):
52+
"""
53+
Create a new polygon from the set of points
54+
55+
.. caution::
56+
57+
A significant amount of calculation is performed when creating
58+
a polygon. These should be reused whenever possible. This cost
59+
can be alleviated somewhat by suppressing certain expensive
60+
sanity checks, but the polygon can behave very unexpectedly
61+
(and potentially without explicit errors) if the errors are
62+
suppressed.
63+
64+
:param points: the ordered set of points on this polygon
65+
:type points: list of :class:`pygorithm.geometry.vector2.Vector2`
66+
67+
:param suppress_errors: True to not do somewhat expensive sanity checks
68+
:type suppress_errors: bool
69+
70+
:raises ValueError: if there are any repeated points (not suppressable)
71+
:raises ValueError: if there are less than 3 points (not suppressable)
72+
:raises ValueError: if the polygon is not convex (suppressable)
73+
:raises ValueError: if the points are not clockwise oriented (suppressable)
74+
"""
75+
pass
76+
77+
@property
78+
def area(self):
79+
"""
80+
Get the area of this polygon. Lazily initialized.
81+
82+
:returns: area of this polygon
83+
:rtype: :class:`numbers.Number`
84+
"""
85+
pass
86+
87+
@staticmethod
88+
def contains_point(polygon, offset, point):
89+
"""
90+
Determine if the polygon at offset contains point.
91+
92+
Distinguish between points that are on the edge of the polygon and
93+
points that are completely contained by the polygon.
94+
95+
.. tip::
96+
97+
This can never return True, True
98+
99+
:param polygon: the polygon
100+
:type polygon: :class:`pygorithm.geometry.polygon2.Polygon2`
101+
:param offset: the offset of the polygon
102+
:type offset: :class:`pygorithm.geometry.vector2.Vector2` or None
103+
:param point: the point to check
104+
:type point: :class:`pygorithm.geometry.vector2.Vector2`
105+
:returns: (on edge, contained)
106+
:rtype: (bool, bool)
107+
"""
108+
pass
109+
110+
@staticmethod
111+
def find_intersection(poly1, poly2, offset1, offset2, find_mtv = True):
112+
"""
113+
Find if the polygons are intersecting and how to resolve it.
114+
115+
Distinguish between polygons that are sharing 1 point or a single line
116+
(touching) as opposed to polygons that are sharing a 2-dimensional
117+
amount of space.
118+
119+
The resulting MTV should be applied to the first polygon (or its offset),
120+
or its negation can be applied to the second polyogn (or its offset).
121+
122+
The MTV will be non-null if overlapping is True and find_mtv is True.
123+
124+
.. note::
125+
126+
There is only a minor performance improvement from setting find_mtv to
127+
False. It is rarely an improvement to first check without finding
128+
mtv and then to find the mtv.
129+
130+
131+
:param poly1: the first polygon
132+
:type poly1: :class:`pygorithm.geometry.polygon2.Polygon2`
133+
:param poly2: the second polygon
134+
:type poly2: :class:`pygorithm.geometry.polygon2.Polygon2`
135+
:param offset1: the offset of the first polygon
136+
:type offset1: :class:`pygorithm.geometry.vector2.Vector2` or None
137+
:param offset2: the offset of the second polygon
138+
:type offset2: :class:`pygorithm.geometry.vector2.Vector2` or None
139+
:param find_mtv: if False, the mtv is always None and there is a small \
140+
performance improvement
141+
:type find_mtv: bool
142+
:returns: (touching, overlapping, (mtv distance, mtv axis))
143+
:rtype: (bool, bool, (:class:`numbers.Number`, :class:`pygorithm.geometry.vector2.Vector2`) or None)
144+
"""
145+
pass

0 commit comments

Comments
 (0)