Skip to content

Commit 0913ad6

Browse files
committed
addressed reviews: add more tests and some refactors
1 parent ca9305d commit 0913ad6

File tree

2 files changed

+46
-20
lines changed

2 files changed

+46
-20
lines changed

src_c/circle.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -390,29 +390,27 @@ pg_circle_contains(pgCircleObject *self, PyObject *arg)
390390
double x, y;
391391

392392
if (pgCircle_Check(arg)) {
393-
pgCircleBase *temp = &pgCircle_AsCircle(arg);
393+
pgCircleBase *circle = &pgCircle_AsCircle(arg);
394394
/*a circle is always contained within itself*/
395-
if (temp == scirc)
395+
if (circle == scirc)
396396
Py_RETURN_TRUE;
397397
/* a bigger circle can't be contained within a smaller circle */
398-
if (temp->r > scirc->r)
398+
if (circle->r > scirc->r)
399399
Py_RETURN_FALSE;
400400

401-
const double dx = temp->x - scirc->x;
402-
const double dy = temp->y - scirc->y;
403-
const double dr = temp->r - scirc->r;
401+
const double dx = circle->x - scirc->x;
402+
const double dy = circle->y - scirc->y;
403+
const double dr = circle->r - scirc->r;
404404

405405
result = (dx * dx + dy * dy) <= (dr * dr);
406406
}
407407
else if (pgRect_Check(arg)) {
408-
SDL_Rect *temp = &pgRect_AsRect(arg);
409-
410-
RECT_CIRCLE_CONTAINS(temp, scirc, result);
408+
SDL_Rect *rect = &pgRect_AsRect(arg);
409+
RECT_CIRCLE_CONTAINS(rect, scirc, result);
411410
}
412411
else if (pgFRect_Check(arg)) {
413-
SDL_FRect *temp = &pgFRect_AsRect(arg);
414-
415-
RECT_CIRCLE_CONTAINS(temp, scirc, result);
412+
SDL_FRect *frect = &pgFRect_AsRect(arg);
413+
RECT_CIRCLE_CONTAINS(frect, scirc, result);
416414
}
417415
else if (pg_TwoDoublesFromObj(arg, &x, &y)) {
418416
result = pgCollision_CirclePoint(scirc, x, y);

test/geometry_test.py

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import unittest
21
import math
3-
2+
import unittest
43
from math import sqrt
5-
from pygame import Vector2, Vector3, Rect, FRect
64

5+
from pygame import Vector2, Vector3, Rect, FRect
76
from pygame.geometry import Circle
87

98

@@ -308,7 +307,7 @@ def test_area_update(self):
308307
self.assertEqual(c.area, math.pi * 4)
309308

310309
c.r_sqr = 100
311-
self.assertEqual(c.area, math.pi * (10**2))
310+
self.assertEqual(c.area, math.pi * (10 ** 2))
312311

313312
def test_area_invalid_value(self):
314313
"""Ensures the area handles invalid values correctly."""
@@ -1176,8 +1175,11 @@ def test_contains_argnum(self):
11761175
def test_contains_return_type(self):
11771176
"""Tests if the function returns the correct type"""
11781177
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)]
11791180

1180-
self.assertIsInstance(c.contains(Circle(10, 10, 4)), bool)
1181+
for item in items:
1182+
self.assertIsInstance(c.contains(item), bool)
11811183

11821184
def test_contains_circle(self):
11831185
"""Ensures that the contains method correctly determines if a circle is
@@ -1190,6 +1192,10 @@ def test_contains_circle(self):
11901192
# self
11911193
self.assertTrue(c.contains(c))
11921194

1195+
# self-like
1196+
c_s = Circle(c)
1197+
self.assertTrue(c.contains(c_s))
1198+
11931199
# contained circle
11941200
self.assertTrue(c.contains(c2))
11951201

@@ -1210,10 +1216,12 @@ def test_contains_point(self):
12101216
p1 = (10, 10)
12111217
p2 = (10, 15)
12121218
p3 = (100, 100)
1219+
p4 = (c.x + math.sin(math.pi / 4) * c.r, c.y + math.cos(math.pi / 4) * c.r)
12131220

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)
12171225

12181226
# contained point
12191227
self.assertTrue(c.contains(p1))
@@ -1222,13 +1230,19 @@ def test_contains_point(self):
12221230
self.assertFalse(c.contains(p2))
12231231
self.assertFalse(c.contains(p3))
12241232

1233+
# on the edge
1234+
self.assertTrue(c.contains(p4))
1235+
12251236
# contained point
12261237
self.assertTrue(c.contains(p1v))
12271238

12281239
# not contained point
12291240
self.assertFalse(c.contains(p2v))
12301241
self.assertFalse(c.contains(p3v))
12311242

1243+
# on the edge
1244+
self.assertTrue(c.contains(p4v))
1245+
12321246
def test_contains_rect_frect(self):
12331247
"""Ensures that the contains method correctly determines if a rect is
12341248
contained within the circle"""
@@ -1237,9 +1251,17 @@ def test_contains_rect_frect(self):
12371251
r2 = Rect(10, 10, 10, 10)
12381252
r3 = Rect(10, 10, 5, 5)
12391253

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+
12401261
fr1 = FRect(0, 0, 3, 3)
12411262
fr2 = FRect(10, 10, 10, 10)
12421263
fr3 = FRect(10, 10, 5, 5)
1264+
fr_edge = FRect(x, y, rx - x, ry - y)
12431265

12441266
# contained rect
12451267
self.assertTrue(c.contains(r1))
@@ -1248,13 +1270,19 @@ def test_contains_rect_frect(self):
12481270
self.assertFalse(c.contains(r2))
12491271
self.assertFalse(c.contains(r3))
12501272

1273+
# on the edge
1274+
self.assertTrue(c.contains(r_edge))
1275+
12511276
# contained rect
12521277
self.assertTrue(c.contains(fr1))
12531278

12541279
# not contained rect
12551280
self.assertFalse(c.contains(fr2))
12561281
self.assertFalse(c.contains(fr3))
12571282

1283+
# on the edge
1284+
self.assertTrue(c.contains(fr_edge))
1285+
12581286

12591287
if __name__ == "__main__":
12601288
unittest.main()

0 commit comments

Comments
 (0)