def test_shapes(): circle = shape.Circle(2) print(dump_json(circle)) print() print(circle.area()) print(circle.sector_head(0)) print(circle.sector_head(0.5)) print(circle.sector_head(1)) print(circle.sector_head(1.5)) print(circle.sector_head(2)) triangle = shape.TriangleDown(10, 5) print(dump_json(triangle)) print() print(triangle.area()) print(triangle.area_slice(0, 1)) print(triangle.area_slice(1, 2)) print(triangle.area_slice(0, 2)) print(triangle.area_slice(0, 3)) print(triangle.area_slice(0, 4)) print(triangle.area_slice(0, 5)) diameter = 355.6 beam = shape.Circle(diameter) for i in range(0, 19): start = diameter / 2 - 190 area = beam.area_slice(start + i * 20, start + (i + 1) * 20)
def setupScene(width, height): centerA = np.array([width / 4, height / 2]) centerB = np.array([width / 2, height / 2]) centerC = np.array([3 * width / 4, height / 2]) global X, A, B, C, AB, BC X = shape.Ellipse(centerB, width / 3, height / 5, color=colors.BLUE) A = shape.Circle(centerA, height / 13) B = shape.Circle(centerB, height / 13) C = shape.Circle(centerC, height / 13) AB = shape.Ellipse((centerA + centerB) / 2, width / 5, height / 8, color=colors.GREEN) BC = shape.Ellipse((centerB + centerC) / 2, width / 5, height / 8, color=colors.WHITE) global my_vertices my_vertices = shape.Points([centerA, centerB, centerC]) global definition1, definition2a, definition2b, definition3a, definition3b definition1 = image.Image('image/topology1.png', width / 12, 10 * height / 12, 1, 10) definition2a = image.Image('image/topology2c.png', width / 12, 10 * height / 12 + 50, 12, 24) definition2b = image.Image('image/topology2d.png', 5 * width / 12 - 150, 8 * height / 12 + 15, 16, 24) definition3a = image.Image('image/topology3a.png', width / 12, 10 * height / 12 + 50, 25, 37) definition3b = image.Image('image/topology3b.png', 5 * width / 12 - 150, 8 * height / 12, 29, 37) definition3b.sprite.update(scale=0.9) fadeInX = animation.fadeIn(3, 5, X) fadeOutX = animation.fadeOut(5, 6, X) # Arbitrary union fadeinA, fadeOutA = animation.fadeIn(12, 14, A), animation.fadeOut(22, 24, A) fadeinB, fadeOutB = animation.fadeIn(12.5, 14, B), animation.fadeOut(22, 24, B) fadeinAB, fadeOutAB = animation.fadeIn(17, 19, AB), animation.fadeOut(22, 24, AB) # Finite intersection fadeinAB_2, fadeOutAB_2 = animation.fadeIn(24, 26, AB), animation.fadeOut( 35, 37, AB) fadeinBC, fadeOutBC = animation.fadeIn(25, 27, BC), animation.fadeOut(35, 37, BC) fadeinB_2, fadeOutB_2 = animation.fadeIn(30, 32, B), animation.fadeOut(35, 37, B)
def _move_on_sq9(self): self.board.grid[2][2] = self.current_player.shape if self.current_player == self.player1: shape_9 = shape.Cross(340, 340, 400, 400, 400, 340, 340, 400) else: shape_9 = shape.Circle(370, 370) self.board.shapes['shape_9'] = shape_9
def test_circle_section(): #create materials conc = material.Concrete(40) steel = material.Steel(463, 194000) #define profile profile = shape.Circle(355.6) bars = [ section.Bar(steel, 12.5, 50.00), section.Bar(steel, 12.5, 115.25), section.Bar(steel, 12.5, 115.25), section.Bar(steel, 12.5, 244.75), section.Bar(steel, 12.5, 244.75), section.Bar(steel, 12.5, 309.50) ] sec = section.Section(conc, profile, bars) #set material factors sec.concrete_factor = 1 sec.steel_factor = 1 #calc peak axial strength #print(sec.calc_peak_axial_strength()) #print MN Chart file = open('mn.csv', 'w') for M, N in sec.calc_M_N().entries: file.write(str(M) + "," + str(N) + "\n")
def _move_on_sq1(self): self.board.grid[0][0] = self.current_player.shape if self.current_player == self.player1: shape_1 = shape.Cross(120, 120, 180, 180, 180, 120, 120, 180) else: shape_1 = shape.Circle(150, 150) self.board.shapes['shape_1'] = shape_1
def _move_on_sq2(self): self.board.grid[0][1] = self.current_player.shape if self.current_player == self.player1: shape_2 = shape.Cross(230, 120, 290, 180, 290, 120, 230, 180) else: shape_2 = shape.Circle(260, 150) self.board.shapes['shape_2'] = shape_2
def _move_on_sq3(self): self.board.grid[0][2] = self.current_player.shape if self.current_player == self.player1: shape_3 = shape.Cross(340, 120, 400, 180, 400, 120, 340, 180) else: shape_3 = shape.Circle(370, 150) self.board.shapes['shape_3'] = shape_3
def _move_on_sq4(self): self.board.grid[1][0] = self.current_player.shape if self.current_player == self.player1: shape_4 = shape.Cross(120, 230, 180, 290, 180, 230, 120, 290) else: shape_4 = shape.Circle(150, 260) self.board.shapes['shape_4'] = shape_4
def _move_on_sq5(self): self.board.grid[1][1] = self.current_player.shape if self.current_player == self.player1: shape_5 = shape.Cross(230, 230, 290, 290, 290, 230, 230, 290) else: shape_5 = shape.Circle(260, 260) self.board.shapes['shape_5'] = shape_5
def _move_on_sq6(self): self.board.grid[1][2] = self.current_player.shape if self.current_player == self.player1: shape_6 = shape.Cross(340, 230, 400, 290, 400, 230, 340, 290) else: shape_6 = shape.Circle(370, 260) self.board.shapes['shape_6'] = shape_6
def _move_on_sq7(self): self.board.grid[2][0] = self.current_player.shape if self.current_player == self.player1: shape_7 = shape.Cross(120, 340, 180, 400, 180, 340, 120, 400) else: shape_7 = shape.Circle(150, 370) self.board.shapes['shape_7'] = shape_7
def _move_on_sq8(self): self.board.grid[2][1] = self.current_player.shape if self.current_player == self.player1: shape_8 = shape.Cross(230, 340, 290, 400, 290, 340, 230, 400) else: shape_8 = shape.Circle(260, 370) self.board.shapes['shape_8'] = shape_8
def do_effect(self, painting): """Process an image so that it is made up of circles. This method processes an image so that it is made up of circles of a supplied radius, on top of a given background colour. The color of the circles correspond to the pixel that would have been at its centre. Arguments: painting -- the painting.Painting that the effect should be applied to """ distance_between_centres = self.diameter + self.gap # Half of distance so that circles fully visible on top and left edges first_centre = distance_between_centres / 2 canvas = painting.copy() canvas.clear_image(self.background) for x in range(first_centre, painting.width, distance_between_centres): for y in range(first_centre, painting.height, distance_between_centres): centre = point.Point(x, y) centre_color = painting.get_pixel_color(centre) circle = shape.Circle(centre, self.radius, centre_color) circle.draw(canvas) painting.img = canvas
def setupScene(width, height): centerA = np.array([width / 4, height / 2]) centerB = np.array([width / 2, height / 2]) centerC = np.array([3 * width / 4, height / 2]) global AC AC = shape.Boomerang(centerB + np.asarray([0, 100]), 390, 175, 75, color=[235, 235, 235, 235]) global X, A, B, C, AB, BC X = shape.Ellipse(centerB, width / 3, height / 5, color=colors.WHITE) A = shape.Circle(centerA, height / 13) B = shape.Circle(centerB, height / 13) C = shape.Circle(centerC, height / 13) AB = shape.Ellipse((centerA + centerB) / 2, width / 5, height / 8, color=colors.WHITE) BC = shape.Ellipse((centerB + centerC) / 2, width / 5, height / 8, color=colors.WHITE) global my_vertices my_vertices = shape.Points([centerA, centerB, centerC]) global redAB, redB redB = shape.Circle(centerB, height / 13, color=colors.RED) redAB = shape.Ellipse((centerA + centerB) / 2, width / 5, height / 8, color=colors.RED) fadeinAB = animation.fadeIn(15, 17, redAB) fadeinB = animation.fadeIn(25, 27, redB)
def _set_geometry(self, data): offset = [float(data['PADXOFF']), float(data['PADYOFF'])] width = float(data['PADWIDTH']) height = float(data['PADHGHT']) if data["PADSHAPE1"] == "CIRCLE": self.geometry = shape.Circle( [ offset[0] - width / 2, offset[1] ], offset, False ) elif data["PADSHAPE1"] == "RECTANGLE" or data["PADSHAPE1"] == "SQUARE": self.geometry = shape.Rectangle( [ offset[0] - width / 2, offset[1] - height / 2 ], [ offset[0] + width / 2, offset[1] + height / 2 ] ) elif data["PADSHAPE1"] == "OBLONG_X": self.geometry = shape.OblongX( [ offset[0] - width / 2, offset[1] - height / 2 ], [ offset[0] + width / 2, offset[1] + height / 2 ] ) elif data["PADSHAPE1"] == "OBLONG_Y": self.geometry = shape.OblongY( [ offset[0] - width / 2, offset[1] - height / 2 ], [ offset[0] + width / 2, offset[1] + height / 2 ] ) else: pass
def load_image(self): # load image imagepath = self.imageList[self.cur - 1] img = Image.open(imagepath) width, height = img.size img = img.resize((int(width / 2), int(height / 2)), Image.ANTIALIAS) self.tkimg = ImageTk.PhotoImage(img) self.mainPanel.config(width=max(self.tkimg.width(), 100), height=max(self.tkimg.height(), 100)) self.mainPanel.create_image(0, 0, image=self.tkimg, anchor=tk.NW) self.progLabel.config(text="{0}/{1}".format(os.path.basename(self.imageList[self.cur - 1]), os.path.basename(self.imageList[self.total - 1]))) # reset mouse state self.shapeId = None self.shape = None self.selected_shape_idx = -1 self.dragging = False # load labels self.clear_shape() self.image_name = os.path.split(imagepath)[-1].split('.')[0] label_name = self.image_name + '.txt' print("label directory:" + self.outDir) self.label_filename = os.path.join(self.outDir, label_name) print("label save path:" + self.label_filename) if os.path.exists(self.label_filename): with open(self.label_filename) as f: for (i, line) in enumerate(f): if i == 0: continue split = line.split(' ') parsable = " ".join(split[1:]) shape_type = split[0] if shape_type == 'POLY': tmp = shape.Polygon(parse=parsable) elif shape_type == 'CIRC': tmp = shape.Circle(parse=parsable) else: raise RuntimeError("unknown shape: " + shape_type) self.shapeList.append(tmp) tmp_id = self.draw_shape(tmp, idx=i-1) self.shapeIdList.append(tmp_id) self.listbox.insert(tk.END, str(i - 1) + ': ' + tmp.to_string())
def mouse_click(self, event): if self.shape: self.shape.handle_click([event.x, event.y]) if self.shape.defined: self.del_shape_id(self.shapeId) self.listbox.insert(tk.END, str(len(self.shapeList)) + ': ' + self.shape.to_string()) self.shapeIdList.append(self.draw_shape(self.shape, idx=len(self.shapeList))) self.shapeList.append(self.shape) self.shapeId = None self.shape = None self.save_image() else: closest_idx = -1 closest_dist = m.inf for i, shp in enumerate(self.shapeList): dist = shape.Shape.dist(shp.location[0], shp.location[1], event.x, event.y) if dist < closest_dist: closest_idx = i closest_dist = dist if closest_dist <= SELECT_RADIUS and closest_idx != self.selected_shape_idx: self.dragging = True if self.selected_shape_idx != -1: self.listbox.selection_clear(0, tk.END) self.del_shape_id(self.shapeIdList[self.selected_shape_idx]) self.shapeIdList[self.selected_shape_idx] = self.draw_shape(self.shapeList[self.selected_shape_idx], idx=self.selected_shape_idx) self.selected_shape_idx = closest_idx self.listbox.selection_set(self.selected_shape_idx) self.del_shape_id(self.shapeIdList[self.selected_shape_idx]) self.shapeIdList[self.selected_shape_idx] = self.draw_shape(self.shapeList[self.selected_shape_idx], idx=self.selected_shape_idx, selected=True, color='red') elif closest_dist <= SELECT_RADIUS and closest_idx == self.selected_shape_idx: self.listbox.selection_clear(0, tk.END) self.del_shape_id(self.shapeIdList[self.selected_shape_idx]) self.shapeIdList[self.selected_shape_idx] = self.draw_shape(self.shapeList[self.selected_shape_idx], idx=self.selected_shape_idx) self.selected_shape_idx = -1 else: if self.shape_type.get() != 'Select Shape Type': new_shape_opts = {'Polygon': shape.Polygon(), 'Circle': shape.Circle()} self.shape = new_shape_opts[self.shape_type.get()] self.shape.handle_click([event.x, event.y])
def setupScene(width, height): # Some shapes triangle_vertices = [[width / 5, height / 5], [2 * width / 5, height / 5], [3 * width / 10, 4 * height / 10]] global my_triangle my_triangle = shape.Polygon(triangle_vertices, color=colors.WHITE) center = [width / 2, 2 * height / 3] global my_circle my_circle = shape.Circle(center, color=colors.WHITE, radius=height / 6) square_vertices = [[4 * width / 6, height / 6], [4 * width / 6 + width / 7, height / 6], [4 * width / 6 + width / 7, height / 6 + width / 7], [4 * width / 6, height / 6 + width / 7]] global my_square my_square = shape.Polygon(square_vertices, color=colors.WHITE) # Animate them! animation.Morph(1, 3, my_triangle, my_circle) animation.Morph(1.5, 3.5, my_square, my_circle)
def setupScene(width, height): # Some shapes triangle_vertices = [[width / 5, height / 5], [2 * width / 5, height / 5], [3 * width / 10, 4 * height / 10]] global my_triangle my_triangle = shape.Polygon(triangle_vertices, color=colors.RED) center = [width / 2, 2 * height / 3] global my_circle my_circle = shape.Circle(center, color=colors.GREEN, radius=height / 5) square_vertices = [[3 * width / 5, height / 5], [3 * width / 5 + width / 5, height / 5], [3 * width / 5 + width / 5, height / 5 + width / 5], [3 * width / 5, height / 5 + width / 5]] global my_square my_square = shape.Polygon(square_vertices, color=colors.BLUE) # Animate them! animation.Morph(1, 3, my_triangle, my_circle) animation.Morph(3, 4.5, my_circle, my_square)
def setupScene(width, height): # Some shapes triangle_vertices = [[width / 3, height / 3], [2 * width / 3, height / 3], [width / 2, 2 * height / 3]] global my_triangle my_triangle = shape.Polygon(triangle_vertices) my_triangle.translate(np.asarray([0, 100])) center = [width / 2, 5 * height / 7] global my_circle my_circle = shape.Circle(my_triangle.center, radius=height / 5 - 10) square_vertices = [[4 * width / 7, height / 5], [4 * width / 7 + width / 6, height / 5], [4 * width / 7 + width / 6, height / 5 + width / 6], [4 * width / 7, height / 5 + width / 6]] global my_square my_square = shape.Polygon(square_vertices) my_square.translate(my_triangle.center - my_square.center) my_square.scale(1.3) # Animate them! animation.Morph(1, 3, my_square, my_circle) animation.Morph(3.1, 4.5, my_circle, my_triangle)
C. 가로, 세로가 5, 10인 직사각형을 정의하여 r에 저장한다. D. 세변이 3(밑변), 4, 5이고, 높이가 4인 삼각형을 정의하여 t에 저장한다. E. c의 면적과 둘레를 출력한다. F. r의 면적과 둘레를 출력한다. G. t의 면적과 둘레를 출력한다. H. t의 변들을 리스트로 받아 출력한다. I. 리스트 l을 정의하여, s, c와 r을 요소로 추가한다. J. l의 각 요소에 대해, 해당 요소를 출력하고, 면적과 둘레를 계산하여 출력한다. K. for문 안에서 테스트: getRadius() 메쏘드를 수행한다.(오류 발생) """ # 필요한 module을 수입하기 import shape #shape.py에 정의된 클래스, 함수등을 수입해서 사용하겠다는 의미 확장자는 붙이지 않는다. s = shape.Shape() #shape.의 의미는 "shape.py"에서 정의된 의미 # shape가 shape.py에 정의된 클래스임을 의미 c = shape.Circle(5) r = shape.Rectangle(5, 10) t = shape.Triangle(3, 4, 5, 4) print(s) print(c) print(r) print(t) print(t.getSides()) l = [] l.append(s) l.append(c) l.append(r) for i in range(0, 3): print(l[i])
def test_shape_Circle_is_equal_to_another_circle_with_the_same_radius(): c1 = shape.Circle(2) c2 = shape.Circle(2) assert c1.is_equal(c2)
import shape #print(dir(shape)) square = shape.Square(2.0) s_area = square.Area() print("Square area: {0:f}".format(s_area)) circle = shape.Circle(2.0) c_area = circle.Area() c_list = circle.GetList() print("Circle area: {0:s}".format(str(c_list)))
def test_exception_text_is_a_string(): try: shape.Circle(-1) except shape.Shape_error as e: assert isinstance(str(e), str)
def test_shape_Circle_is_called_Circle(): c = shape.Circle(3) assert c.name == "Circle"
def test_exception_on_negative_radius(): shape.Circle(-1)
def test_shape_Circle_is_not_equal_to_square(): c = shape.Circle(2) s = shape.Square(2) assert not c.is_equal(s)
def test_shape_Circle_is_not_equal_to_circle_with_different_radius(): c1 = shape.Circle(2) c2 = shape.Circle(3) assert not c1.is_equal(c2)
# 載入shape套件 import shape as sh circle_radius = [1, 2, 3, 4, 5] circles = [] # 儲存Circle物件的資料組 # 用for迴圈建立5個Circle物件,把它們存入circles資料組 for radius in circle_radius: c = sh.Circle(radius) circles += [c] # 顯示Circle物件的面積 print('\n圓面積: ', end='') for c in circles: # 利用字串物件的format()方法設定取2位小數 print('{:.2f}'.format(c.get_area()), end=' ') # 顯示Circle物件的周長 print('\n圓周長: ', end='') for c in circles: # 利用字串物件的format()方法設定取2位小數 print('{:.2f}'.format(c.get_perimeter()), end=' ')
def test_shape_Circle_is_equal_to_itself(): c = shape.Circle(2) assert c.is_equal(c)