def frac(val, denom=64): "Formats a floating point number as a fraction" if val < 0: return "-%s" % (frac(math.fabs(val), denom)) else: whole = math.floor(val) dec = val - whole numer = 0 if dec: numer = round(dec * denom) while not (numer % 2) and numer: numer = numer / 2 denom = denom / 2 # if denom == 1: # whole += 1 # numer = 0 if almost_equal(numer, denom): numer = 0 whole = whole + 1 if whole and numer: return "%d %d/%d" % (whole, numer, denom) elif numer: return "%d/%d" % (numer, denom) else: return "%d" % (whole)
def test_quad_block_to_shape(): for side in (16.0,17.5,32): q = QuadBlock( side ) vs = q.vertices shape = q.create_collision_shape() assert isinstance( shape, physics.ConvexPolygonShape ) for a, b in zip( q.vertices, shape.vertices ): assert vectors_almost_equal( a, b ) assert almost_equal( shape.area(), side * side )
def Cs(roof, Ct): if almost_equal(Ct, 1.0): if roof.slippery: x1, y1 = 5.0, 1.0 else: x1, y1 = 30.0, 1.0 x2, y2 = 70.0, 0.0 elif almost_equal(Ct, 1.1): if roof.slippery: x1, y1 = 10.0, 1.0 else: x1, y1 = 37.5, 1.0 x2, y2 = 70.0, 0.0 elif almost_equal(Ct, 1.2): if roof.slippery: x1, y1 = 15.0, 1.0 else: x1, y1 = 45.0, 1.0 x2, y2 = 70.0, 0.0 else: raise ValueError("Ct must be 1.0, 1.1 or 1.2") return min(1.0, max(0.0, interpolate(x1, y1, x2, y2, roof.theta())))
def recalc(self): "Perform calculations and store results" self.pf = pf(pg=self.pg, I=self.I, Ce=self.Ce, Ct=self.Ct) self.hb = self.pf / self.gamma() lu = max(self.lu, 25.0) self.hd0 = 0.43 * (lu)**(1.0 / 3.0) * (self.pg + 10.0)**(1.0 / 4.0) - 1.5 if not self.is_leeward: self.hd0 = self.hd0 * 0.75 self.hd = min(self.hd0, self.hc - self.hb) self.pd = snow_density(self.pg) * self.hd self.pm = self.pd + self.pf self.is_truncated = not almost_equal(self.hd0, self.hd) if self.is_truncated: self.w = round(4.0 * self.hd0**2 / self.hc * 2.0) / 2.0 else: self.w = round(4.0 * self.hd * 2.0) / 2.0
def test_quad_block(): for side in (16.0,17.5,32): q = QuadBlock( side ) assert all( [ almost_equal(side, edge.length) for edge in q.edges ] ) expected_angles = (0,90,180,270) for angle, expected_angle in zip(sorted([edge.angle_degrees for edge in q.edges]),expected_angles): assert almost_equal( angle, expected_angle ) bottom_edge, right_edge, top_edge, left_edge = q.edges assert almost_equal(270, bottom_edge.angle_degrees) assert almost_equal(0, right_edge.angle_degrees) assert almost_equal(90, top_edge.angle_degrees) assert almost_equal(180, left_edge.angle_degrees) q.rotate_degrees( 45.0 ) expected_angles = (45,135,225,315) for angle, expected_angle in zip(sorted([edge.angle_degrees for edge in q.edges]),expected_angles): assert almost_equal( angle, expected_angle ) q.rotate_degrees( 11.0 ) expected_angles = (56,146,236,326) for angle, expected_angle in zip(sorted([edge.angle_degrees for edge in q.edges]),expected_angles): assert almost_equal( angle, expected_angle ) assert q.free_edge_indices == [0,1,2,3]
def __eq__(self, other): return almost_equal(self.x, other.x, places=3) and almost_equal( self.y, other.y, places=3)
def test_almost_equal(self): self.assertFalse(util.almost_equal(5.4432434, 5.4432477, places=5)) self.assertFalse(util.almost_equal(5.4432434, 5.4432477, places=6)) self.assertTrue(util.almost_equal(5.4432434, 5.4432477, places=3)) self.assertTrue(util.almost_equal(5.4432434, 5.4432477, places=4))
def test_attach_four_blocks_in_square(): # 2 1 # 3a12c0 # 0 3 # 1 3 # 2b00d2 # 3 1 a = QuadBlock(1) b = QuadBlock(1) c = QuadBlock(1) d = QuadBlock(1) s = BlockStructure(a) assert len( s.free_edge_indices ) == 4 assert degrees_almost_equal( s.edge( (0,0) ).angle_degrees, 270.0 ) a_index = 0 assert vectors_almost_equal( s.blocks[a_index].create_collision_shape().centroid(), (0,0) ) b_index = s.attach((0,0), b, 1) assert vectors_almost_equal( s.blocks[b_index].create_collision_shape().centroid(), (0,-1) ) assert len( s.free_edge_indices ) == 6 assert degrees_almost_equal( s.edge( (b_index,1) ).angle_degrees, 90.0 ) assert degrees_almost_equal( s.edge( (0,1) ).angle_degrees, 0.0 ) c_index = s.attach((0,1), c, 2) assert vectors_almost_equal( s.blocks[c_index].create_collision_shape().centroid(), (1,0) ) assert len( s.free_edge_indices ) == 8 assert degrees_almost_equal( s.edge( (c_index,3) ).angle_degrees, 270.0 ) d_index = s.attach((c_index,3), d, 3) assert vectors_almost_equal( s.blocks[d_index].create_collision_shape().centroid(), (1,-1) ) assert degrees_almost_equal( s.edge( (d_index,3) ).angle_degrees, 90.0 ) assert len( s.free_edge_indices ) == 8 assert almost_equal( s.edge( (a_index,0) ).angle_degrees, 270 ) assert almost_equal( s.edge( (a_index,1) ).angle_degrees, 0 ) assert almost_equal( s.edge( (a_index,2) ).angle_degrees, 90 ) assert almost_equal( s.edge( (a_index,3) ).angle_degrees, 180 ) assert almost_equal( s.edge( (b_index,0) ).angle_degrees, 0 ) assert almost_equal( s.edge( (b_index,1) ).angle_degrees, 90 ) assert almost_equal( s.edge( (b_index,2) ).angle_degrees, 180 ) assert almost_equal( s.edge( (b_index,3) ).angle_degrees, 270 ) assert almost_equal( s.edge( (c_index,0) ).angle_degrees, 0 ) assert almost_equal( s.edge( (c_index,1) ).angle_degrees, 90 ) assert almost_equal( s.edge( (c_index,2) ).angle_degrees, 180 ) assert almost_equal( s.edge( (c_index,3) ).angle_degrees, 270 ) assert almost_equal( s.edge( (d_index,0) ).angle_degrees, 180 ) assert almost_equal( s.edge( (d_index,1) ).angle_degrees, 270 ) assert almost_equal( s.edge( (d_index,2) ).angle_degrees, 0 ) assert almost_equal( s.edge( (d_index,3) ).angle_degrees, 90 ) connections = s.extract_connections_map() for block_index, block_connections in connections.items(): for edge_index, connected in block_connections.items(): a, b = connected assert connections[ a ][ b ] == (block_index, edge_index) assert len( s.extract_connections() ) == 4
def test_attach_four_blocks_in_knightsmove(): # 2 1 1 # 3a12c02b0 # 0 3 3 # 1 # 2d0 # 3 a = QuadBlock(1) b = QuadBlock(1) c = QuadBlock(1) d = QuadBlock(1) s = BlockStructure(a) assert len( s.free_edge_indices ) == 4 a_index = 0 c_index = s.attach((a_index,1), c, 2) assert len( s.free_edge_indices ) == 6 b_index = s.attach((c_index,0), b, 2) assert len( s.free_edge_indices ) == 8 d_index = s.attach((b_index,3), d, 1) assert len( s.free_edge_indices ) == 10 assert almost_equal( s.edge( (a_index,0) ).angle_degrees, 270 ) assert almost_equal( s.edge( (a_index,1) ).angle_degrees, 0 ) assert almost_equal( s.edge( (a_index,2) ).angle_degrees, 90 ) assert almost_equal( s.edge( (a_index,3) ).angle_degrees, 180 ) assert almost_equal( s.edge( (b_index,0) ).angle_degrees, 0 ) assert almost_equal( s.edge( (b_index,1) ).angle_degrees, 90 ) assert almost_equal( s.edge( (b_index,2) ).angle_degrees, 180 ) assert almost_equal( s.edge( (b_index,3) ).angle_degrees, 270 ) assert almost_equal( s.edge( (c_index,0) ).angle_degrees, 0 ) assert almost_equal( s.edge( (c_index,1) ).angle_degrees, 90 ) assert almost_equal( s.edge( (c_index,2) ).angle_degrees, 180 ) assert almost_equal( s.edge( (c_index,3) ).angle_degrees, 270 ) assert almost_equal( s.edge( (d_index,0) ).angle_degrees, 0 ) assert almost_equal( s.edge( (d_index,1) ).angle_degrees, 90 ) assert almost_equal( s.edge( (d_index,2) ).angle_degrees, 180 ) assert almost_equal( s.edge( (d_index,3) ).angle_degrees, 270 ) connections = s.extract_connections_map() for block_index, block_connections in connections.items(): for edge_index, connected in block_connections.items(): a, b = connected assert connections[ a ][ b ] == (block_index, edge_index) assert len( s.extract_connections() ) == 3