This repository has been archived by the owner on Oct 26, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
centered_figure.py
executable file
·201 lines (164 loc) · 5.76 KB
/
centered_figure.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
"""
Centered polygon figure, needs shapely and pygame.
Create figure with an (x,y) center, set color, thickness and surface to plot
(optional).
Usage:
triangle = CenteredFigure([(0, 0), (0, 1), (0, 3)], [10, 10])
square = CenteredFigure([(0, 0), (0, 1), (1, 1), (1, 0)], [10, 10])
triangle.collide(square) -> true
triangle.rotate(15) -> Rotate 15 grads Counterclockwise
triangle.scale(10) -> Multiply 10 to all vertices position
Copyright (C) 2017 Pablo Pizarro @ppizarror
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
"""
# Imports
from math import cos, sin, pi
from shapely.geometry import Polygon
import pygame
class CenteredFigure(object):
def __init__(self, points, center, color=(255, 255, 255), width=0,
pygame_surface=None):
"""
Create centered figure from point list with center (x,y).
:param points: Clockwise vertices tuple list (ex. [(10,10),(10,5), ...])
:type points: list
:param center: Center list (ex [10, 10])
:type center: list
:param color: Tuple color
:type color: tuple
:param width: Border width (px)
:param pygame_surface: Pygame surface object
:type pygame_surface: pygame.display
"""
assert self._assert_center(center), 'Center must be a list'
assert isinstance(color, tuple), 'Color must be a tuple'
assert self._assert_vertices(
points), 'Vertices must be a list of tuples'
self._points = points
self._center = center
self._color = color
self._width = width
self._surface = pygame_surface
@staticmethod
def _assert_center(center):
"""
Check if center variable is correct.
:param center: Center list
:return: Boolean
"""
if isinstance(center, list):
if len(center) == 2:
return True
return False
@staticmethod
def _assert_vertices(vertices):
"""
Check if vertices list only contain tuples
:param vertices: Vertices list
:return:
"""
if isinstance(vertices, list):
for v in vertices:
if not isinstance(v, tuple):
return False
return True
return False
def collide(self, figure):
"""
Check if figure collides with another figure.
:param figure: Figure to check collision
:type figure: CenteredFigure
:return:
"""
assert isinstance(figure, CenteredFigure), \
'Figure must be CenteredFigure'
p1 = Polygon(self.get_vertices())
p2 = Polygon(figure.get_vertices())
return p1.intersects(p2)
def draw(self):
"""
Draw polygon figure to pygame surface.
:return: None
"""
if self._surface is not None:
pygame.draw.polygon(self._surface, self._color, self.get_vertices(),
self._width)
def get_center(self):
"""
Return center list (reference).
:return: Center list
:rtype: list
"""
return self._center
def get_vertices(self):
"""
Return tuple list of centered points.
:return: List of tuples
:rtype: list
"""
# Get center
cx = self._center[0]
cy = self._center[1]
# Return vertex list
return [(u + cx, v + cy) for u, v in self._points]
def rotate(self, angle):
"""
Rotate vertices list (around center, which is mathematically [0,0])
:param angle: Rotation angle, Counterclockwise (grad)
:return:
"""
angle = - angle * pi / 180.0
rotated_vertices = []
for u, v in self._points:
nu = u * cos(angle) - v * sin(angle)
nv = v * cos(angle) + u * sin(angle)
rotated_vertices.append((nu, nv))
self._points = rotated_vertices
def scale(self, factor):
"""
Scale the figure.
:param factor:
:return:
"""
self._points = [(x * factor, y * factor) for x, y in self._points]
def set_center(self, center):
"""
Set figure center.
:param center: Center list
:type center: list
:return:
"""
self._center = center
def set_color(self, color):
"""
Set figure color.
:param color: Pygame color
:return:
"""
assert isinstance(color, tuple), 'Color must be a tuple'
self._color = color
def set_surface(self, surface):
"""
Set pygame surface object.
:param surface: Pygame display surface.
:type surface: pygame.display
:return:
"""
self._surface = surface
def set_vertices_points(self, points):
"""
Set vertices points of figure.
:param points: Clockwise vertices tuple list (ex. [(10,10),(10,5), ...])
:type points: list
:return:
"""
assert self._assert_vertices(
points), 'Vertices must be a list of tuples'
self._points = points