Fix rotation around anchor points for shapes.Polygon.
Minor code simplification/refactoring.
@@ -1090,9 +1090,6 @@ class Polygon(_ShapeBase):
# len(self._coordinates) = the number of vertices and sides in the shape.
self._coordinates = list(coordinates)
- # x = self._coordinates[0][0]
- # y = self._coordinates[0][1]
-
self._rotation = 0
self._rgb = color
@@ -1107,48 +1104,40 @@ class Polygon(_ShapeBase):
if not self._visible:
self._vertex_list.vertices = tuple([0] * ((len(self._coordinates) - 2) * 6))
elif self._rotation:
- x = float(self.x) - self._anchor_x
- y = float(self.y) - self._anchor_y
+ # Adjust all coordinates by the anchor.
+ anchor_x = self._anchor_x
+ anchor_y = self._anchor_y
+ coords = [[x - anchor_x, y - anchor_y] for x, y in self._coordinates]
+ # Rotate the polygon around its first vertex.
+ x, y = self._coordinates[0]
r = -math.radians(self._rotation)
cr = math.cos(r)
sr = math.sin(r)
- anchor_x = self._anchor_x
- anchor_y = self._anchor_y
-
- # Rotate the polygon around its first vertex.
- coords = list(self._coordinates)
for i, c in enumerate(coords):
- # Set to new object to avoid modification to self._coordinates.
c = [c[0] - x, c[1] - y]
c = [c[0] * cr - c[1] * sr + x, c[0] * sr + c[1] * cr + y]
coords[i] = c
- # Adjust all coordinates by the anchor.
- adjusted_coordinates = list(map(lambda c: (c[0] - anchor_x, c[1] - anchor_y),
- coords))
-
# Triangulate the convex polygon.
triangles = []
- for n in range(len(adjusted_coordinates) - 2):
- triangles += [adjusted_coordinates[0], adjusted_coordinates[n+1], adjusted_coordinates[n+2]]
+ for n in range(len(coords) - 2):
+ triangles += [coords[0], coords[n + 1], coords[n + 2]]
# Flattening the list before setting vertices to it.
self._vertex_list.vertices = tuple(value for coordinate in triangles for value in coordinate)
else:
+ # Adjust all coordinates by the anchor.
anchor_x = self._anchor_x
anchor_y = self._anchor_y
-
- # Adjust all coordinates by the anchor.
- adjusted_coordinates = list(map(lambda c: (c[0] - anchor_x, c[1] - anchor_y),
- self._coordinates))
+ coords = [[x - anchor_x, y - anchor_y] for x, y in self._coordinates]
# Triangulate the convex polygon.
triangles = []
- for n in range(len(adjusted_coordinates) - 2):
- triangles += [adjusted_coordinates[0], adjusted_coordinates[n+1], adjusted_coordinates[n+2]]
+ for n in range(len(coords) - 2):
+ triangles += [coords[0], coords[n + 1], coords[n + 2]]
# Flattening the list before setting vertices to it.
self._vertex_list.vertices = tuple(value for coordinate in triangles for value in coordinate)
@@ -1216,5 +1205,4 @@ class Polygon(_ShapeBase):
self._update_position()
-
__all__ = ('Arc', 'Circle', 'Line', 'Rectangle', 'BorderedRectangle', 'Triangle', 'Star', 'Polygon')
