def test_numerical_protocol(): c1 = Circle(2) c2 = Circle(6) assert print(c1 + c2) == print(Circle(8)) assert print(c2 * 3) == print(Circle(18)) assert print(3 * c2) == print(Circle(18))
def setUp(self): self.p1 = Point(-30, 30) self.p2 = Point(40, 40) self.p3 = Point(-40, 40) self.p4 = Point(-40, -40) self.o = Point(0, 0) self.circle1 = Circle.circleFromGrid(self.o, self.p1) self.circle2 = Circle.circleFromGrid(self.o, self.p2)
def test_augmented(): c1 = Circle(2) c1 += 5 assert print(c1) == print(Circle(7)) c1 += Circle(12) assert print(c1) == print(Circle(19))
def test_comparisons(): c1 = Circle(2) c2 = Circle(6) assert (c1 < c2) is True assert (c1 <= c2) is True assert (c1 == c2) is False assert (c1 != c2) is True assert (c1 > c2) is False assert (c1 >= c2) is False
class TestCircle(unittest.TestCase): def setUp(self): self.c1 = Circle(0, 0, 4) self.c2 = Circle(0, 0, 2) self.c3 = Circle(2, 2, 2) self.c4 = Circle(3, 4, 2) def test_init(self): self.assertRaises(ValueError, Circle, 0, 0, -1) self.assertRaises(ValueError, Circle, 3, 3, -6) self.assertRaises(ValueError, Circle, 4, 6, -18) def test_repr(self): self.assertEqual(repr(self.c1), "Circle(0, 0, 4)") self.assertEqual(repr(self.c2), "Circle(0, 0, 2)") self.assertEqual(repr(self.c3), "Circle(2, 2, 2)") def test_eq(self): self.assertTrue(self.c1 == Circle(0, 0, 4)) self.assertTrue(self.c2 == Circle(0, 0, 2)) self.assertTrue(self.c3 == Circle(2, 2, 2)) self.assertFalse(self.c1 == Circle(4, 3, 2)) def test_ne(self): self.assertTrue(self.c1 != Circle(0, 8, 2)) self.assertTrue(self.c2 != Circle(3, 3, 3)) self.assertTrue(self.c3 != Circle(12, 43, 123)) self.assertFalse(self.c4 != Circle(3, 4, 2)) def test_area(self): self.assertEqual(self.c1.area(), 16 * pi) self.assertEqual(self.c2.area(), 4 * pi) self.assertEqual(self.c3.area(), 4 * pi) def test_move(self): self.assertEqual(self.c1.move(2, 2), Circle(2, 2, 4)) self.assertEqual(self.c2.move(1, 4), Circle(1, 4, 2)) self.assertEqual(Circle(5, 5, 5).move(4, 5), Circle(9, 10, 5)) def test_cover(self): self.assertEqual( Circle(0, 0, 4).cover(Circle(0, 0, 1)), Circle(0, 0, 4)) self.assertEqual( Circle(0, 0, 5).cover(Circle(0, 4, 1)), Circle(0, 0, 5)) self.assertEqual( Circle(0, 0, 2).cover(Circle(0, 3, 1)), Circle(0, 1, 3)) self.assertEqual( Circle(0, 0, 1).cover(Circle(3, 4, 1)), Circle(1.5, 2, 3.5)) self.assertEqual( Circle(1, 1, 2).cover(Circle(2, 1, 1)), Circle(1, 1, 2))
def _place_circle(self, r, c_idx=None): """Attempt to place a circle of radius r within the image figure. c_idx is a list of indexes into the self.colours list, from which the circle's colour will be chosen. If None, use all colours. """ if not c_idx: c_idx = range(len(self.colours)) # Get the coordinates of all non-zero image pixels img_coords = np.nonzero(self.img) if not img_coords: return False # The guard number: if we don't place a circle within this number # of trials, we give up. guard = self.guard # For this method, r must be an integer. Ensure that it's at least 1. r = max(1, int(r)) while guard: # Pick a random candidate pixel... i = np.random.randint(len(img_coords[0])) icx, icy = img_coords[0][i], img_coords[1][i] # ... and see if the circle fits there if self._circle_fits(icx, icy, r): self.apply_circle_mask(icx, icy, r) circle = Circle(icx, icy, r, icolour=np.random.choice(c_idx)) self.circles.append(circle) return True guard -= 1 print('guard reached.') return False
def newCircle(): x, y = randint(0, w - 1), randint(0, h - 1) for other in circles: d = ((x - other.x)**2 + (y - other.y)**2)**(0.5) if d < other.r + 3 or inputImg[y, x, 0] == 0: return None if len(circles) == 0 and inputImg[y, x, 0] == 0: return None return Circle(x, y, thickness)
def test_circle_validation(self): # Test radius(r) validation # Test string self.assertRaises(ValueError, Circle, "hi") # Test object input self.assertRaises(ValueError, Circle, Circle(5)) # Test negative self.assertRaises(ValueError, Circle, -5) # Test complex self.assertRaises(ValueError, Circle, 5j)
def read_circle_data(filename): circles = [] with open(filename, 'r') as file: reader = csv.reader(file) for row in reader: lat = float(row[3]) lng = float(row[4]) radius = float(row[5]) center = Point(lat, lng) circle = Circle(center, radius) circles.append(circle) return circles
def test_radius(): c = Circle(5) assert c.radius == 5
def test_circle_diameter(self): # Test circle perimeter calculation self.assertAlmostEqual(Circle(3).diameter(), 2 * 3) self.assertAlmostEqual(Circle(1).diameter(), 2) self.assertAlmostEqual(Circle(4.57).diameter(), 2 * 4.57)
def test_circle_perimeter(self): # Test circle perimeter calculation self.assertAlmostEqual(Circle(5).perimeter(), 2 * pi * 5) self.assertAlmostEqual(Circle(1).perimeter(), 2 * pi) self.assertAlmostEqual(Circle(1.52).perimeter(), 2 * pi * 1.52)
def test_circle_from_diameter(): c = Circle.from_diameter(10) assert c.radius == 5 assert c.diameter == 10
def test_print(): c = Circle(4) print_output = str(c) assert print_output.startswith('Circle with radius:') is True assert "4" in print_output
def test_diameter_setter(): c = Circle(5) c.diameter = 20 assert c.radius == 10 assert c.diameter == 20
def test_area_getter(): c = Circle(5) assert c.area == math.pi * (c.radius**2)
def test_circle_area_int(self): # Test Area function accuracy with integers self.assertAlmostEqual(Circle(1).area(), pi) self.assertAlmostEqual(Circle(5).area(), 5**2 * pi)
def test_diameter(): c = Circle(5) assert c.diameter == 10
def test_eq(self): self.assertTrue(self.c1 == Circle(0, 0, 4)) self.assertTrue(self.c2 == Circle(0, 0, 2)) self.assertTrue(self.c3 == Circle(2, 2, 2)) self.assertFalse(self.c1 == Circle(4, 3, 2))
ax.set_xlim([0,100]) # axis setting ax.set_ylim([0,100]) plt.rcParams["legend.markerscale"] = 0.3 # scale the marker ims = [] # array to store each image flame # Set up formatting for the movie files Writer = animation.writers['ffmpeg'] writer = Writer(fps=int(1.0/time_step), metadata=dict(artist='Momoko'), bitrate=1800) """ generate random circles """ for i in range(circle_num): # generate random velocity and position vel = Vector2D(np.random.randint(1,50), np.random.randint(1,50)) # generate 10~50 int randomly pos = Vector2D(np.random.randint(10, 100), np.random.randint(10, 100)) radius = 10.0 mass = 1.0 # [kg] circle = Circle(radius = radius, initial_center = pos, initial_velocity = vel, mass = mass) # setup circles.append(circle) # add one more circles """" ============ THIS PART IS MAIN PART ============ """ """ Move circles """ for i in range(int(duration/time_step)): # move circles move_all_circles(circles, circle_num, time_step) # wall collision check # If collisiong with wall, bound back the circle assuming perfect elastic collision wall_collide_check_and_move(circles, circle_num, wall) # Check if it collides with other circles's and move
def test_ne(self): self.assertTrue(self.c1 != Circle(0, 8, 2)) self.assertTrue(self.c2 != Circle(3, 3, 3)) self.assertTrue(self.c3 != Circle(12, 43, 123)) self.assertFalse(self.c4 != Circle(3, 4, 2))
def test_sorting(): circles = [ Circle(6), Circle(7), Circle(8), Circle(4), Circle(0), Circle(2), Circle(3), Circle(5), Circle(9), Circle(1) ] circles.sort() assert circles == [ Circle(0), Circle(1), Circle(2), Circle(3), Circle(4), Circle(5), Circle(6), Circle(7), Circle(8), Circle(9) ]
def setUp(self): self.c1 = Circle(0, 0, 4) self.c2 = Circle(0, 0, 2) self.c3 = Circle(2, 2, 2) self.c4 = Circle(3, 4, 2)
def test_cover(self): self.assertEqual( Circle(0, 0, 4).cover(Circle(0, 0, 1)), Circle(0, 0, 4)) self.assertEqual( Circle(0, 0, 5).cover(Circle(0, 4, 1)), Circle(0, 0, 5)) self.assertEqual( Circle(0, 0, 2).cover(Circle(0, 3, 1)), Circle(0, 1, 3)) self.assertEqual( Circle(0, 0, 1).cover(Circle(3, 4, 1)), Circle(1.5, 2, 3.5)) self.assertEqual( Circle(1, 1, 2).cover(Circle(2, 1, 1)), Circle(1, 1, 2))
def test_move(self): self.assertEqual(self.c1.move(2, 2), Circle(2, 2, 4)) self.assertEqual(self.c2.move(1, 4), Circle(1, 4, 2)) self.assertEqual(Circle(5, 5, 5).move(4, 5), Circle(9, 10, 5))
class TestCircles(): circle_test_data = [(Point(0, 0), Circle(Point(0, 0), 10), True), (Point(10, 0), Circle(Point(0, 0), 10), True), (Point(5, 5), Circle(Point(0, 0), 10), True), (Point(37, 25), Circle(Point(30, 18), 10), True), (Point(37, 25), Circle(Point(27, 15), 10), False)] rect1_test_data = [ (Rectangle(Point(0, 0), 3, 4), Circle(Point(0, 0), 5), True), (Rectangle(Point(0, 0), 3, 4), Circle(Point(0, 0), 4), False), (Rectangle(Point(100, 100), 3, 4), Circle(Point(100, 100), 6), True), (Rectangle(Point(0, 0), 3, 4.5), Circle(Point(0, 0), 5), False), (Rectangle(Point(-3, 0), 3, 4.5), Circle(Point(0, 0), 5), False), (Rectangle(Point(0, -4.5), 3, 4.5), Circle(Point(0, 0), 5), False), (Rectangle(Point(-3, -4.5), 3, 4.5), Circle(Point(0, 0), 5), False), (Rectangle(Point(-3, -4), 3, 4), Circle(Point(0, 0), 5), True), ] rect2_test_data = [ (Rectangle(Point(0, 0), 3, 4), Circle(Point(4, 5), 1), False), (Rectangle(Point(0, 0), 3, 4.5), Circle(Point(0, 0), 5), True), (Rectangle(Point(-3, 0), 3, 4.5), Circle(Point(0, 0), 5), True), (Rectangle(Point(0, -4.5), 3, 4.5), Circle(Point(0, 0), 5), True), (Rectangle(Point(-3, -4.5), 3, 4.5), Circle(Point(0, 0), 5), True), (Rectangle(Point(-3, -4), 3, 4), Circle(Point(0, 0), 5), True), ] @pytest.mark.parametrize('point, circle, expected', circle_test_data) def test_point_in_circle_on_test_data(self, point, circle, expected): assert point_in_circle(point, circle) == expected @pytest.mark.parametrize('rect, circle, expected', rect1_test_data) def test_rect_in_circle_on_test_data(self, rect, circle, expected): assert rect_in_circle(rect, circle) == expected @pytest.mark.parametrize('rect, circle, expected', rect2_test_data) def test_rect_circle_overlap_on_test_data(self, rect, circle, expected): assert rect_circle_overlap(rect, circle) == expected
def test_circle_area_float(self): # Test float calculation of area self.assertAlmostEqual(Circle(5.5).area(), 5.5**2 * pi) self.assertAlmostEqual(Circle(5.98).area(), 5.98**2 * pi)