1
- import unittest
2
1
import math
3
-
2
+ import unittest
4
3
from math import sqrt
5
- from pygame import Vector2 , Vector3 , Rect , FRect
6
4
5
+ from pygame import Vector2 , Vector3 , Rect , FRect
7
6
from pygame .geometry import Circle
8
7
9
8
@@ -308,7 +307,7 @@ def test_area_update(self):
308
307
self .assertEqual (c .area , math .pi * 4 )
309
308
310
309
c .r_sqr = 100
311
- self .assertEqual (c .area , math .pi * (10 ** 2 ))
310
+ self .assertEqual (c .area , math .pi * (10 ** 2 ))
312
311
313
312
def test_area_invalid_value (self ):
314
313
"""Ensures the area handles invalid values correctly."""
@@ -1176,8 +1175,11 @@ def test_contains_argnum(self):
1176
1175
def test_contains_return_type (self ):
1177
1176
"""Tests if the function returns the correct type"""
1178
1177
c = Circle (10 , 10 , 4 )
1178
+ items = [Circle (3 , 4 , 15 ), (0 , 0 ), Vector2 (0 , 0 ), Rect (0 , 0 , 10 , 10 ),
1179
+ FRect (0 , 0 , 10 , 10 )]
1179
1180
1180
- self .assertIsInstance (c .contains (Circle (10 , 10 , 4 )), bool )
1181
+ for item in items :
1182
+ self .assertIsInstance (c .contains (item ), bool )
1181
1183
1182
1184
def test_contains_circle (self ):
1183
1185
"""Ensures that the contains method correctly determines if a circle is
@@ -1190,6 +1192,10 @@ def test_contains_circle(self):
1190
1192
# self
1191
1193
self .assertTrue (c .contains (c ))
1192
1194
1195
+ # self-like
1196
+ c_s = Circle (c )
1197
+ self .assertTrue (c .contains (c_s ))
1198
+
1193
1199
# contained circle
1194
1200
self .assertTrue (c .contains (c2 ))
1195
1201
@@ -1210,10 +1216,12 @@ def test_contains_point(self):
1210
1216
p1 = (10 , 10 )
1211
1217
p2 = (10 , 15 )
1212
1218
p3 = (100 , 100 )
1219
+ p4 = (c .x + math .sin (math .pi / 4 ) * c .r , c .y + math .cos (math .pi / 4 ) * c .r )
1213
1220
1214
- p1v = Vector2 (10 , 10 )
1215
- p2v = Vector2 (10 , 15 )
1216
- p3v = Vector2 (100 , 100 )
1221
+ p1v = Vector2 (p1 )
1222
+ p2v = Vector2 (p2 )
1223
+ p3v = Vector2 (p3 )
1224
+ p4v = Vector2 (p4 )
1217
1225
1218
1226
# contained point
1219
1227
self .assertTrue (c .contains (p1 ))
@@ -1222,13 +1230,19 @@ def test_contains_point(self):
1222
1230
self .assertFalse (c .contains (p2 ))
1223
1231
self .assertFalse (c .contains (p3 ))
1224
1232
1233
+ # on the edge
1234
+ self .assertTrue (c .contains (p4 ))
1235
+
1225
1236
# contained point
1226
1237
self .assertTrue (c .contains (p1v ))
1227
1238
1228
1239
# not contained point
1229
1240
self .assertFalse (c .contains (p2v ))
1230
1241
self .assertFalse (c .contains (p3v ))
1231
1242
1243
+ # on the edge
1244
+ self .assertTrue (c .contains (p4v ))
1245
+
1232
1246
def test_contains_rect_frect (self ):
1233
1247
"""Ensures that the contains method correctly determines if a rect is
1234
1248
contained within the circle"""
@@ -1237,9 +1251,17 @@ def test_contains_rect_frect(self):
1237
1251
r2 = Rect (10 , 10 , 10 , 10 )
1238
1252
r3 = Rect (10 , 10 , 5 , 5 )
1239
1253
1254
+ angle = math .pi / 4
1255
+ x = c .x - math .sin (angle ) * c .r
1256
+ y = c .y - math .cos (angle ) * c .r
1257
+ rx = c .x + math .sin (angle ) * c .r
1258
+ ry = c .y + math .cos (angle ) * c .r
1259
+ r_edge = Rect (x , y , rx - x , ry - y )
1260
+
1240
1261
fr1 = FRect (0 , 0 , 3 , 3 )
1241
1262
fr2 = FRect (10 , 10 , 10 , 10 )
1242
1263
fr3 = FRect (10 , 10 , 5 , 5 )
1264
+ fr_edge = FRect (x , y , rx - x , ry - y )
1243
1265
1244
1266
# contained rect
1245
1267
self .assertTrue (c .contains (r1 ))
@@ -1248,13 +1270,19 @@ def test_contains_rect_frect(self):
1248
1270
self .assertFalse (c .contains (r2 ))
1249
1271
self .assertFalse (c .contains (r3 ))
1250
1272
1273
+ # on the edge
1274
+ self .assertTrue (c .contains (r_edge ))
1275
+
1251
1276
# contained rect
1252
1277
self .assertTrue (c .contains (fr1 ))
1253
1278
1254
1279
# not contained rect
1255
1280
self .assertFalse (c .contains (fr2 ))
1256
1281
self .assertFalse (c .contains (fr3 ))
1257
1282
1283
+ # on the edge
1284
+ self .assertTrue (c .contains (fr_edge ))
1285
+
1258
1286
1259
1287
if __name__ == "__main__" :
1260
1288
unittest .main ()
0 commit comments