def test_disk_centroid(): for j in range(100): r = random.random() * 10.0 x0, y0 = (random.random() - 0.5) * 10.0, (random.random() - 0.5) * 10.0 cx, cy = DiskShape(r, (x0, y0)).centroid() assert almost_equal(x0, cx) assert almost_equal(y0, cy)
def test_disk_centroid(): for j in range(100): r = random.random() * 10.0 x0, y0 = (random.random() - 0.5) * 10.0, (random.random() - 0.5) * 10.0 cx, cy = DiskShape(r, (x0,y0)).centroid() assert almost_equal( x0, cx ) assert almost_equal( y0, cy )
def test_physics_limit_calculations(): for i in range(100): diameter = random.random() speed = random.random() timestep = random.random() assert almost_equal( calculate_maximum_timestep( diameter, speed ) * speed, 0.5 * diameter ) assert almost_equal( timestep * calculate_maximum_speed( timestep, diameter ), 0.5 * diameter ) assert almost_equal( timestep * speed, 0.5 * calculate_minimum_diameter( timestep, speed ) )
def test_composite_centroid(): for j in range(100): w, h = (random.random()) * 10.0, random.random() * 10.0 x0, y0 = (random.random() + 1) * 10.0, (random.random() - 0.5) * 10.0 box_points = [ (x0,y0), (x0+w,y0), (x0+w,y0+h), (x0,y0+h) ] negative_box_points = [ (-x0,y0), (-x0-w,y0), (-x0-w,y0+h), (-x0,y0+h) ] centroid = CompositeShape(ConvexPolygonShape(*box_points), ConvexPolygonShape(*negative_box_points)).centroid() centroid_1 = ConvexPolygonShape(*box_points).centroid() centroid_2 = ConvexPolygonShape(*negative_box_points).centroid() assert almost_equal( centroid.x, (centroid_1.x+centroid_2.x) * 0.5 ) assert almost_equal( centroid.y, (centroid_1.y+centroid_2.y) * 0.5 )
def test_polygon_area(): for j in range(100): r = random.random() * 10.0 x0, y0 = (random.random() - 0.5) * 10.0, (random.random() - 0.5) * 10.0 circle_points = [(x0 + r * math.cos(0.001*i), y0 + r * math.sin(0.001*i)) for i in range(6283)] assert almost_equal( math.pi * r * r, ConvexPolygonShape(*circle_points).area() ) for j in range(100): w, h = random.random() * 10.0, random.random() * 10.0 x0, y0 = (random.random() - 0.5) * 10.0, (random.random() - 0.5) * 10.0 box_points = [ (x0,y0), (x0+w,y0), (x0+w,y0+h), (x0,y0+h) ] assert almost_equal( w * h, ConvexPolygonShape(*box_points).area() )
def test_physics_limit_calculations(): for i in range(100): diameter = random.random() speed = random.random() timestep = random.random() assert almost_equal( calculate_maximum_timestep(diameter, speed) * speed, 0.5 * diameter) assert almost_equal( timestep * calculate_maximum_speed(timestep, diameter), 0.5 * diameter) assert almost_equal(timestep * speed, 0.5 * calculate_minimum_diameter(timestep, speed))
def test_polygon_area(): for j in range(100): r = random.random() * 10.0 x0, y0 = (random.random() - 0.5) * 10.0, (random.random() - 0.5) * 10.0 circle_points = [(x0 + r * math.cos(0.001 * i), y0 + r * math.sin(0.001 * i)) for i in range(6283)] assert almost_equal(math.pi * r * r, ConvexPolygonShape(*circle_points).area()) for j in range(100): w, h = random.random() * 10.0, random.random() * 10.0 x0, y0 = (random.random() - 0.5) * 10.0, (random.random() - 0.5) * 10.0 box_points = [(x0, y0), (x0 + w, y0), (x0 + w, y0 + h), (x0, y0 + h)] assert almost_equal(w * h, ConvexPolygonShape(*box_points).area())
def test_composite_centroid(): for j in range(100): w, h = (random.random()) * 10.0, random.random() * 10.0 x0, y0 = (random.random() + 1) * 10.0, (random.random() - 0.5) * 10.0 box_points = [(x0, y0), (x0 + w, y0), (x0 + w, y0 + h), (x0, y0 + h)] negative_box_points = [(-x0, y0), (-x0 - w, y0), (-x0 - w, y0 + h), (-x0, y0 + h)] centroid = CompositeShape( ConvexPolygonShape(*box_points), ConvexPolygonShape(*negative_box_points)).centroid() centroid_1 = ConvexPolygonShape(*box_points).centroid() centroid_2 = ConvexPolygonShape(*negative_box_points).centroid() assert almost_equal(centroid.x, (centroid_1.x + centroid_2.x) * 0.5) assert almost_equal(centroid.y, (centroid_1.y + centroid_2.y) * 0.5)
def test_thing_angles(): world = World() sim = world.sim = PhysicsSimulator() thing = Thing( world, DiskShape(10.0), mass = 1.0, moment = 1.0 ) thing.angular_velocity_radians = 1.0 angles = [] for i in range(6000): world.sim.tick(0.01) radians = thing.angle_radians degrees = thing.angle_degrees angles.append( radians ) assert almost_equal( math.cos( radians ), math.cos( degrees_to_radians( degrees ) ) ) assert almost_equal( math.sin( radians ), math.sin( degrees_to_radians( degrees ) ) ) assert len( set(angles) ) > 1
def test_triangulation_area(): for i in range(100): s = generate_random_convex_polygon_shape() triangles = starmap( TriangleShape, s.triangulate() ) assert almost_equal( sum( [t.area() for t in triangles] ), s.area() ) for i in range(100): s = generate_random_disk_shape() triangles = starmap( TriangleShape, s.triangulate() ) assert almost_equal( sum( [t.area() for t in triangles] ), s.area(), k = 0.01 ) for i in range(100): s = generate_random_disk_shape() u = generate_random_convex_polygon_shape() triangles = starmap( TriangleShape, CompositeShape(s,u).triangulate() ) assert almost_equal( sum( [t.area() for t in triangles] ), s.area() + u.area(), k = 0.01 ) assert almost_equal( CompositeShape(s,u).area(), s.area() + u.area(), k = 0.01 )
def test_thing_angles(): world = World() sim = world.sim = PhysicsSimulator() thing = Thing(world, DiskShape(10.0), mass=1.0, moment=1.0) thing.angular_velocity_radians = 1.0 angles = [] for i in range(6000): world.sim.tick(0.01) radians = thing.angle_radians degrees = thing.angle_degrees angles.append(radians) assert almost_equal(math.cos(radians), math.cos(degrees_to_radians(degrees))) assert almost_equal(math.sin(radians), math.sin(degrees_to_radians(degrees))) assert len(set(angles)) > 1
def test_polygon_centroid(): for j in range(100): r = random.random() * 10.0 x0, y0 = (random.random() - 0.5) * 10.0, (random.random() - 0.5) * 10.0 circle_points = [(x0 + r * math.cos(0.001*i), y0 + r * math.sin(0.001*i)) for i in range(6283)] centroid = ConvexPolygonShape(*circle_points).centroid() print >> sys.stderr, r, (x0,y0), centroid assert almost_equal( x0, centroid.x ) assert almost_equal( y0, centroid.y ) for j in range(100): w, h = random.random() * 10.0, random.random() * 10.0 x0, y0 = (random.random() - 0.5) * 10.0, (random.random() - 0.5) * 10.0 box_points = [ (x0,y0), (x0+w,y0), (x0+w,y0+h), (x0,y0+h) ] centroid = ConvexPolygonShape(*box_points).centroid() assert almost_equal( x0 + 0.5 * w, centroid.x ) assert almost_equal( y0 + 0.5 * h, centroid.y )
def test_polygon_centroid(): for j in range(100): r = random.random() * 10.0 x0, y0 = (random.random() - 0.5) * 10.0, (random.random() - 0.5) * 10.0 circle_points = [(x0 + r * math.cos(0.001 * i), y0 + r * math.sin(0.001 * i)) for i in range(6283)] centroid = ConvexPolygonShape(*circle_points).centroid() print >> sys.stderr, r, (x0, y0), centroid assert almost_equal(x0, centroid.x) assert almost_equal(y0, centroid.y) for j in range(100): w, h = random.random() * 10.0, random.random() * 10.0 x0, y0 = (random.random() - 0.5) * 10.0, (random.random() - 0.5) * 10.0 box_points = [(x0, y0), (x0 + w, y0), (x0 + w, y0 + h), (x0, y0 + h)] centroid = ConvexPolygonShape(*box_points).centroid() assert almost_equal(x0 + 0.5 * w, centroid.x) assert almost_equal(y0 + 0.5 * h, centroid.y)
def test_composite_area(): for j in range(100): w, h = (random.random()+1.0) * 10.0, random.random() * 10.0 x0, y0 = (random.random() - 0.5) * 10.0, (random.random() - 0.5) * 10.0 box_points = [ (x0,y0), (x0+w,y0), (x0+w,y0+h), (x0,y0+h) ] negative_box_points = [ (-x0,y0), (-x0+w,y0), (-x0+w,y0+h), (-x0,y0+h) ] area = CompositeShape(ConvexPolygonShape(*box_points), ConvexPolygonShape(*negative_box_points)).area() assert almost_equal( 2.0 * (w*h), area )
def test_composite_area(): for j in range(100): w, h = (random.random() + 1.0) * 10.0, random.random() * 10.0 x0, y0 = (random.random() - 0.5) * 10.0, (random.random() - 0.5) * 10.0 box_points = [(x0, y0), (x0 + w, y0), (x0 + w, y0 + h), (x0, y0 + h)] negative_box_points = [(-x0, y0), (-x0 + w, y0), (-x0 + w, y0 + h), (-x0, y0 + h)] area = CompositeShape(ConvexPolygonShape(*box_points), ConvexPolygonShape(*negative_box_points)).area() assert almost_equal(2.0 * (w * h), area)
def test_triangulation_area(): for i in range(100): s = generate_random_convex_polygon_shape() triangles = starmap(TriangleShape, s.triangulate()) assert almost_equal(sum([t.area() for t in triangles]), s.area()) for i in range(100): s = generate_random_disk_shape() triangles = starmap(TriangleShape, s.triangulate()) assert almost_equal(sum([t.area() for t in triangles]), s.area(), k=0.01) for i in range(100): s = generate_random_disk_shape() u = generate_random_convex_polygon_shape() triangles = starmap(TriangleShape, CompositeShape(s, u).triangulate()) assert almost_equal(sum([t.area() for t in triangles]), s.area() + u.area(), k=0.01) assert almost_equal(CompositeShape(s, u).area(), s.area() + u.area(), k=0.01)
def test_disk_area(): for j in range(100): r = random.random() * 10.0 x0, y0 = (random.random() - 0.5) * 10.0, (random.random() - 0.5) * 10.0 assert almost_equal( math.pi * r * r, DiskShape(r, (x0,y0)).area() )
def test_disk_area(): for j in range(100): r = random.random() * 10.0 x0, y0 = (random.random() - 0.5) * 10.0, (random.random() - 0.5) * 10.0 assert almost_equal(math.pi * r * r, DiskShape(r, (x0, y0)).area())