def test_is_line(self): p1 = Point(-1, -1) p2 = Point(2, 3) p4 = Point(-1, 3) p5 = Point(-4, -5) self.assertFalse(Triangle(p1, p2, p4).is_line()) self.assertTrue(Triangle(p1, p2, p5).is_line())
def res(): circle = Circle(1, 2) triangle = Triangle(Point(0, 1), Point(2, 3), Point(10, 2)) square = Square(Point(0, 1), Point(4, 4)) spis_figure = [] spis_figure.append(circle.ploshad()) spis_figure.append(triangle.ploshad()) spis_figure.append(square.ploshad()) return spis_figure
def test_case_8(self): ans = "\n" + inspect.stack()[0][3] + ": \n" s = Triangle([(0, 0), (10, 0), (5, 5)]) ans += str(s) + "\n" ans += s.details() + "\n" self.check(ans, 6084, True)
def main(): point_a = Point(2, 4) point_b = Point(6, 11) point_c = Point(3, 4) triangle_1 = Triangle(point_a, point_b, point_c) print(triangle_1.triangle_S()) print(triangle_1.triangle_P()) print('=' * 20) square_1 = Square(point_a, point_b) print(square_1.square_S()) print(square_1.square_P()) print('=' * 20) circle_1 = Circle(point_a, 15) print(circle_1.circle_S()) print(circle_1.circle_P())
class TestTriangle(unittest.TestCase): def setUp(self): self.a_triangle = Triangle('Ted') #self.allies= ['Circle'] def testAllies(self): value = self.a_triangle.add_ally('Square') allies = ['Circle'] self.assertNotEqual(allies, value) def testShapeType(self): self.assertEqual(Triangle.shape_type, 'Triangle') def testPerimeter(self): self.assertEqual(self.a_triangle.perimeter(), (3 * 2)) def testArea(self): self.assertNotEqual(self.a_triangle.area(), 0)
def sierpinski(layers, t): assert type(t) == Triangle, "input was not a Triangle" if layers > 9: print("Too many layers specified, resetting to max of 9") layers = 9 if layers == 0: return [t] else: AB = t.A.midpoint(t.B) BC = t.B.midpoint(t.C) CA = t.C.midpoint(t.A) t1 = Triangle(t.A, AB, CA) t2 = Triangle(AB, t.B, BC) t3 = Triangle(CA, BC, t.C) return sierpinski(layers - 1, t1) + sierpinski( layers - 1, t2) + sierpinski(layers - 1, t3)
def mutateSimpleShape(shape): ran = random.uniform(0, 99) if ran > 60: nshape = random.choice([ Square(shape.children), Circle(shape.children), Triangle(shape.children) ]) if nshape.name == shape.name: return mutateSimpleShape(shape) return nshape else: return shape
def create(pt, r=None): n = len(pt) if (r == None): if n == 1: x, y = pt[0] return Point(x, y) elif n == 2: return Segment(pt) elif n == 3: return Triangle(pt) else: return Polygon(pt) elif n == 1: x, y = pt[0] if (r > 0): return Circle(x, y, r) else: return Point(x, y)
def atomconvert(text, nts): parr = [] # eg.[a 1 b 2 ] atomparr = [] # eg.[1 2 3] parr.clear() atomparr.clear() sumflag = 0 for chip in reversed(text): # print(chip) if "]" in chip: sumflag = sumflag + chip.count("]") # print(sumflag) chip = chip.replace("[", "") chip = chip.replace("]", "") # print(chip) if is_number(chip): # 如果是数字 atomparr.append(float(chip)) else: # 如果是字母 list.reverse(atomparr) if chip == "a" or chip == "alpha": parr.append(Alpha(*atomparr)) elif chip == "b" or chip == "brightness": parr.append(Brightness(*atomparr)) elif chip == "sat" or chip == "saturation": parr.append(Saturation(*atomparr)) elif chip == "h" or chip == "hue": parr.append(Hue(*atomparr)) elif chip == "y": parr.append(Y(*atomparr)) elif chip == "r" or chip == "rotate": parr.append(Rotate(*atomparr)) elif chip == "f" or chip == "flip": parr.append(Flip(*atomparr)) elif chip == "x": parr.append(X(*atomparr)) elif chip == "s" or chip == "size": parr.append(Size(*atomparr)) elif chip == "trans" or chip == "transform": parr.append(Transform(*atomparr)) elif chip == "skew": parr.append(Skew(*atomparr)) elif chip == "..": parr.append(randRange(*atomparr)) # 统计完毕一个参数,清空数字列表 atomparr.clear() list.reverse(parr) if sumflag == 2: # print("GG") parr = [Leftbrac()] + parr + [Rightbrac()] if text[0] == "CIRCLE": return Circle(parr) elif text[0] == "TRIANGLE": return Triangle(parr) elif text[0] == "SQUARE": return Square(parr) else: ntName = text[0] for n in nts: if ntName == n.name: return RuleCall(n, parr) newNT = NonTerminal(ntName, []) nts.append(newNT) return RuleCall(newNT, parr)
def test_perimeter(self, a, b, c, expected): t = Triangle(a, b, c) assert t.perimeter() == expected
def test_tipo_lado(self, a, b, c, expected): t = Triangle(a, b, c) assert t.triangle_type() == expected
from classes import Cirlce, Triangle, Point, Square # При потребности создавать все необходимые методы не описанные в задании. # Создать список фигур и в цикле подсчитать и вывести площади всех фигур на экран[my-oop-03] # Примечание: в рамках задание создать два файла: classes.pyи main.py. # В первом будут описаны все классы, во втором классы будут импортированы и использованы. list = [ Cirlce(0, 5, 10), Triangle(0, 3, 0, 4, 5, 0), Square(0, 0, 0, 4)] for x in list: print(x.square())
def plot_sier(ts): from matplotlib.patches import Circle, Wedge, Polygon from matplotlib.collections import PatchCollection import matplotlib.pyplot as plt fig, ax = plt.subplots() patches = [] for t in ts: polygon = t.to_polygon() patches.append(polygon) colors = 100*np.random.rand(len(patches)) p = PatchCollection(patches, alpha=0.5) p.set_array(np.array(colors)) ax.add_collection(p) ax.set_xlim(0,2) ax.set_ylim(0,sqrt(3)) plt.savefig('output/sierpinski.png') if __name__=="__main__": T = Triangle( Point2(0,0), Point2(2,0), Point2(1,sqrt(3)) ) ts = sierpinski(5, T) plot_sier(ts)
from classes import Point, Triangle, Circle, Square point1 = Point(x=0, y=2) point2 = Point(x=1, y=0) point3 = Point(x=5, y=0) triangle_1 = Triangle(point_1=point1, point_2=point2, point_3=point3) point1 = Point(x=0, y=2) point2 = Point(x=2, y=0) square_1 = Square(point_1=point1, point_2=point2) circle_1 = Circle(radius=5, center_point=5) figures_list = [triangle_1, square_1, circle_1] for figure in figures_list: print( f'Периметр {figure.__class__.__name__} равен {figure.get_perimeter()}') print(f'Площадь {figure.__class__.__name__} равна {figure.get_square()}')
def test_case_8(self): ans = "\n" + inspect.stack()[0][3] + ": \n" s = Triangle([(0, 0), (10, 0), (5, 5)]) ans += str(s) + "\n" ans += s.details() + "\n" self.check(ans, 6084, True) TestShapeMethod.test_case_8 = test_case unittest.main(argv=[''], verbosity=0, exit=False) s = """ Expected outputs (except the id numbers and the spaces could be different test_case: Triangle(id=192) Triangle(id=192): <vertex:(0,0)(10,0)(5,5)>, area=25, perimeter=24 test_case: pass! """ def test_case_9(self): ans = "\n" + inspect.stack()[0][3] + ": \n" s = Rectangle(width=3, height=4)
def setUp(self): self.a_triangle = Triangle('Ted')
def test_perimeter(self): p1 = Point(-1, -1) p2 = Point(2, 3) p4 = Point(-1, 3) self.assertAlmostEqual(Triangle(p1, p2, p4).perimeter(), 12)