Skip to content

Commit

Permalink
addressed reviews: add more tests and some refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
itzpr3d4t0r committed May 23, 2024
1 parent ca9305d commit 0913ad6
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 20 deletions.
22 changes: 10 additions & 12 deletions src_c/circle.c
Original file line number Diff line number Diff line change
Expand Up @@ -390,29 +390,27 @@ pg_circle_contains(pgCircleObject *self, PyObject *arg)
double x, y;

if (pgCircle_Check(arg)) {
pgCircleBase *temp = &pgCircle_AsCircle(arg);
pgCircleBase *circle = &pgCircle_AsCircle(arg);
/*a circle is always contained within itself*/
if (temp == scirc)
if (circle == scirc)
Py_RETURN_TRUE;
/* a bigger circle can't be contained within a smaller circle */
if (temp->r > scirc->r)
if (circle->r > scirc->r)
Py_RETURN_FALSE;

const double dx = temp->x - scirc->x;
const double dy = temp->y - scirc->y;
const double dr = temp->r - scirc->r;
const double dx = circle->x - scirc->x;
const double dy = circle->y - scirc->y;
const double dr = circle->r - scirc->r;

result = (dx * dx + dy * dy) <= (dr * dr);
}
else if (pgRect_Check(arg)) {
SDL_Rect *temp = &pgRect_AsRect(arg);

RECT_CIRCLE_CONTAINS(temp, scirc, result);
SDL_Rect *rect = &pgRect_AsRect(arg);
RECT_CIRCLE_CONTAINS(rect, scirc, result);
}
else if (pgFRect_Check(arg)) {
SDL_FRect *temp = &pgFRect_AsRect(arg);

RECT_CIRCLE_CONTAINS(temp, scirc, result);
SDL_FRect *frect = &pgFRect_AsRect(arg);
RECT_CIRCLE_CONTAINS(frect, scirc, result);
}
else if (pg_TwoDoublesFromObj(arg, &x, &y)) {
result = pgCollision_CirclePoint(scirc, x, y);
Expand Down
44 changes: 36 additions & 8 deletions test/geometry_test.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import unittest
import math

import unittest
from math import sqrt
from pygame import Vector2, Vector3, Rect, FRect

from pygame import Vector2, Vector3, Rect, FRect
from pygame.geometry import Circle


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

c.r_sqr = 100
self.assertEqual(c.area, math.pi * (10**2))
self.assertEqual(c.area, math.pi * (10 ** 2))

def test_area_invalid_value(self):
"""Ensures the area handles invalid values correctly."""
Expand Down Expand Up @@ -1176,8 +1175,11 @@ def test_contains_argnum(self):
def test_contains_return_type(self):
"""Tests if the function returns the correct type"""
c = Circle(10, 10, 4)
items = [Circle(3, 4, 15), (0, 0), Vector2(0, 0), Rect(0, 0, 10, 10),
FRect(0, 0, 10, 10)]

self.assertIsInstance(c.contains(Circle(10, 10, 4)), bool)
for item in items:
self.assertIsInstance(c.contains(item), bool)

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

# self-like
c_s = Circle(c)
self.assertTrue(c.contains(c_s))

# contained circle
self.assertTrue(c.contains(c2))

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

p1v = Vector2(10, 10)
p2v = Vector2(10, 15)
p3v = Vector2(100, 100)
p1v = Vector2(p1)
p2v = Vector2(p2)
p3v = Vector2(p3)
p4v = Vector2(p4)

# contained point
self.assertTrue(c.contains(p1))
Expand All @@ -1222,13 +1230,19 @@ def test_contains_point(self):
self.assertFalse(c.contains(p2))
self.assertFalse(c.contains(p3))

# on the edge
self.assertTrue(c.contains(p4))

# contained point
self.assertTrue(c.contains(p1v))

# not contained point
self.assertFalse(c.contains(p2v))
self.assertFalse(c.contains(p3v))

# on the edge
self.assertTrue(c.contains(p4v))

def test_contains_rect_frect(self):
"""Ensures that the contains method correctly determines if a rect is
contained within the circle"""
Expand All @@ -1237,9 +1251,17 @@ def test_contains_rect_frect(self):
r2 = Rect(10, 10, 10, 10)
r3 = Rect(10, 10, 5, 5)

angle = math.pi / 4
x = c.x - math.sin(angle) * c.r
y = c.y - math.cos(angle) * c.r
rx = c.x + math.sin(angle) * c.r
ry = c.y + math.cos(angle) * c.r
r_edge = Rect(x, y, rx - x, ry - y)

fr1 = FRect(0, 0, 3, 3)
fr2 = FRect(10, 10, 10, 10)
fr3 = FRect(10, 10, 5, 5)
fr_edge = FRect(x, y, rx - x, ry - y)

# contained rect
self.assertTrue(c.contains(r1))
Expand All @@ -1248,13 +1270,19 @@ def test_contains_rect_frect(self):
self.assertFalse(c.contains(r2))
self.assertFalse(c.contains(r3))

# on the edge
self.assertTrue(c.contains(r_edge))

# contained rect
self.assertTrue(c.contains(fr1))

# not contained rect
self.assertFalse(c.contains(fr2))
self.assertFalse(c.contains(fr3))

# on the edge
self.assertTrue(c.contains(fr_edge))


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

0 comments on commit 0913ad6

Please sign in to comment.