def __init__(self, x, y, resource, font): self.resource = resource self.font = font # counter number initialization self.num = SelectableText(font, '0', resource.value, 0, 0) font_height = get_font_height(font) font_width = get_font_width(font) # store this center location long-term for re-centering self.num_center = (int(x + ResourceCounter.font_x_offset * font_width), int(y + ResourceCounter.font_y_offset * font_height)) self.num.rect.center = self.num_center # arrow initialization arrow_size = int(font_height * ResourceCounter.arrow_size_modifier) arrow_offset = int(font_height / 2 + font_height * ResourceCounter.arrow_separation) self.up_arrow = Triangle(x, y - arrow_offset, arrow_size, ResourceCounter.arrow_color) self.down_arrow = Triangle(x, y + arrow_offset, arrow_size, ResourceCounter.arrow_color, pointed_up=False) # collision rects for detecting mouse / clicks self.up_arrow_collision = pygame.rect.Rect(0, 0, arrow_size, arrow_size) self.up_arrow_collision.center = (x, y - arrow_offset) self.down_arrow_collision = pygame.rect.Rect(0, 0, arrow_size, arrow_size) self.down_arrow_collision.center = (x, y + arrow_offset)
def main(): s1 = Square(4) s2 = Square(7) t1 = Triangle(3, 5) t2 = Triangle(7, 8) shape_list = s1, s2, t1, t2 for n, shape in enumerate(shape_list): print("Shape", n, "is a", shape.shape_type, "with an area of", shape.area())
def fan_triangulation(vertices, normals, material): triangles = [] for index in range(2, len(vertices) - 1): if len(normals) > 1: tri = Triangle.smooth_triangle(vertices[1], vertices[index], vertices[index + 1], normals[1], normals[index], normals[index + 1]) else: tri = Triangle(vertices[1], vertices[index], vertices[index + 1]) tri.material = material triangles.append(tri) return triangles
def mousePressEvent(self, event): # отслеживание кликов по мыши и добавление фигуры в список if self.instrument == 'brush': self.objects.append([]) self.objects[-1].append(BrushPoint(event.x(), event.y(), self.thickness, self.color)) self.update() elif self.instrument == 'line': self.objects.append(Line(event.x(), event.y(), event.x(), event.y(), self.thickness, self.color)) self.update() elif self.instrument == 'circle': self.objects.append(Circle(event.x(), event.y(), event.x(), event.y(), self.thickness, self.color)) self.update() elif self.instrument == 'rubber': self.objects.append([]) self.objects[-1].append(BrushPoint(event.x(), event.y(), self.thickness, QColor(240, 240, 240))) self.update() elif self.instrument == 'rect': self.objects.append(Rect(event.x(), event.y(), 0, 0, self.thickness, self.color)) self.update() elif self.instrument == 'square': self.objects.append(Rect(event.x(), event.y(), 0, 0, self.thickness, self.color)) self.update() elif self.instrument == 'triangle': self.objects.append(Triangle(event.x(), event.y(), event.x(), event.y(), self.thickness, self.color)) self.update() elif self.instrument == 'sqtriangle': self.objects.append(SqTriangle(event.x(), event.y(), event.x(), event.y(), self.thickness, self.color)) self.update()
def setUp(self): self.init_kwargs = {} self.init_kwargs["xCoord"] = 50 self.init_kwargs["yCoord"] = 50 measurements = {"side": 50} self.init_kwargs["measurements"] = measurements self.area = 4.33 * measurements["side"]**2 self.shape = Triangle(**self.init_kwargs)
def main(): shapes: list = [ Circle(5), Rectangle(3, 3), Rectangle(4, 4), Triangle(3, 4), ] for shape in shapes: logging.debug( "type: {0}, circumference = {1:.2f}, area = {2:.2f}".format( type(shape), shape.circumference(), shape.area()))
def main(): apoint = Point(x=100, y=80) bpoint = Point(x=160, y=200) cpoint = Point(x=220, y=80) circle = Circle(radius=10, point=apoint) triangle = Triangle(lines=[StraightLine(a=apoint, b=bpoint), StraightLine(a=apoint, b=cpoint), StraightLine(a=bpoint, b=cpoint)]) canvas1 = Canvas(250, 250, Point(x=200, y=200), "canvas1", [triangle, circle], show_cp=True) canvas2 = Canvas(260, 260, Point(x=450, y=450), "canvas2", [circle], show_cp=True) canvas3 = Canvas(270, 270, Point(x=710, y=710), "canvas3", [circle], show_cp=True) canvas_list = [canvas1, canvas2, canvas3] screen = TkinterScreen() screen.configure(canvas_list) board = Board(screen) board.draw_at()
def createShapes(): shapes = [] for i in range(randint(5, 10)): v = randint(1, 4) x, y = randint(0, 700), randint(0, 700) length = randint(10, 100) width = randint(10, 100) colorChoice = COLORS[randint(0, len(COLORS) - 1)] if v == 1: pass shapes.append(Triangle((x, y), colorChoice, length)) if v == 2: shapes.append(Rectangle((x, y), colorChoice, length, width)) if v == 3: shapes.append(Square((x, y), colorChoice, length)) if v == 4: shapes.append(Circle((x, y), colorChoice, length)) return shapes
def setUp(self): self.triangle_init_kwargs = {} self.rectangle_init_kwargs = {} self.triangle_init_kwargs["xCoord"] = 50 self.triangle_init_kwargs["yCoord"] = 50 self.rectangle_init_kwargs["xCoord"] = 50 self.rectangle_init_kwargs["yCoord"] = 50 triangle_measurements = {"side": 50} rectangle_measurements = {"width": 50, "length": 50} self.triangle_init_kwargs["measurements"] = triangle_measurements self.rectangle_init_kwargs["measurements"] = rectangle_measurements self.side = 50 self.length = 50 self.width = 50 self.tag = "test" self.area = (4.33 * triangle_measurements["side"]**2) + ( rectangle_measurements["width"] * rectangle_measurements["length"]) triangle = Triangle(**self.triangle_init_kwargs) rectangle = Rectangle(**self.rectangle_init_kwargs) self.components = [triangle, rectangle] self.shape = CompositeShape(tag=self.tag, components=self.components)
def _fan_triangulation(self, line): triangles = [] if '/' in line[1]: v1 = line[1].split('/')[0] n1 = line[1].split('/')[2] else: v1 = line[1] for i in range(2, len(line) - 1): if '/' in line[i]: v2 = line[i].split('/')[0] n2 = line[i].split('/')[2] else: v2 = line[i] if '/' in line[i + 1]: v3 = line[i + 1].split('/')[0] n3 = line[i + 1].split('/')[2] else: v3 = line[i + 1] try: if len(self.normals) > 0: tri = SmoothTriangle( self.default_material, matrix4x4identity(), self.vertices[int(v1)], self.vertices[int(v2)], self.vertices[int(v3)], self.normals[int(n1)], self.normals[int(n2)], self.normals[int(n3)]) else: tri = Triangle(self.default_material, matrix4x4identity(), self.vertices[int(v1)], self.vertices[int(v2)], self.vertices[int(v3)]) triangles.append(tri) except IndexError as e: print(e, v1, v2, v3, n1, n2, n3) raise return triangles
def step_create_triangle_t(context): context.t = Triangle(context.p1, context.p2, context.p3)
def step_create_triangle_t_with_points(context, x1, y1, z1, \ x2, y2, z2, \ x3, y3, z3): context.t = Triangle(point(x1, y1, z1), point(x2, y2, z2), point(x3, y3, z3))
def main(): #---- Prepare # Game field size SCREEN_W, SCREEN_H = (640, 420) # Shape drag machine states (ST_MOVE, ST_ROTATE, ST_NONE) = range(3) state = ST_NONE # Setup game window display.set_caption(TITLE) display.set_icon(image.load('pytang.bmp')) # Create and setup game field screen = display.set_mode((SCREEN_W, SCREEN_H)) screen.fill(BACKGROUND_COLOR) # Create a game field copy for internal purposes background = screen.copy() # Game objects - different shapes shapes = [ Triangle((0, 0), (60, 60), (0, 120)), Triangle((0, 0), (60, 0), (30, 30)), Triangle((60, 0), (120, 0), (120, 60)), Triangle((0, 120), (60, 60), (120, 120)), Triangle((60, 60), (90, 30), (90, 90)), Parallelogram((60, 0), (90, 30), (60, 60)), Parallelogram((90, 30), (120, 60), (120, 120)) ] # Game object bounding rectangles as a dictionary, # where key is the game object instance, and # the appropriate value is it's bounding rectangle frames = {} # Each shape is located in a separate cell of the game field # at first time. The game field is divided by 3 rows and # 3 columns, ergo, 3 x 3 = 9 cells. # One cell size CELL_SIZE = (SCREEN_W / 3, SCREEN_H / 3) # Shapes initial locations as (column, row) tuples. locations = ((0, 0), (1, 0), (2, 1), (2, 2), (0, 1), (0, 2), (2, 0)) if DRAFT_MODE: draw_shape = _draw_shape_draft else: draw_shape = _draw_shape_filled # Move shapes to the initial locations for i in range(len(shapes)): shapes[i].move_to([(locations[i][coord] + 0.5) * CELL_SIZE[coord] for coord in (0, 1)]) frames[shapes[i]] = draw_shape(screen, shapes[i]) #---- Event loop do_exit = False # Shape object which is moving or rotating now active_shape = None # Mouse previous position, is valid when some shape # is rotating or moving prev_pos = None while not do_exit: display.flip() for ev in event.get(): # Game is over - user close main window if ev.type == QUIT: do_exit = True break # Mouse button is push down: # user wants to move or rotate one of the shapes elif (ev.type == MOUSEBUTTONDOWN) and (state == ST_NONE): touchpoint = mouse.get_pos() # Search for the first shape including # the mouse button down point. for shape in shapes: if shape.include(touchpoint): active_shape = shape prev_pos = touchpoint # If the touchpoint is laying inside the # inner circle, the shape s.b. moved; # the shape s.b. rotated otherwise if (distance(touchpoint, shape.get_ref_point()) <= shape.get_r_inner()): state = ST_MOVE else: state = ST_ROTATE # Take the active shape from the shapes # common list, # draw other shapes on the background surface. shapes.remove(active_shape) background.fill(BACKGROUND_COLOR) for shape in shapes: draw_shape(background, shape) break # Mouse button is up: shape moving/rotating is done elif (ev.type == MOUSEBUTTONUP) and (state != ST_NONE): shapes.append(active_shape) active_shape = None state = ST_NONE # Mouse is moving. If the mouse button is down # (i.e. if we are in ST_MOVE or ST_ROTATE state), # the active shape s.b. moved or rotated. elif (ev.type == MOUSEMOTION) and (state != ST_NONE): screen.blit(background, frames[active_shape].topleft, frames[active_shape]) curr_pos = mouse.get_pos() if state == ST_MOVE: active_shape.move_by(vectorAB(prev_pos, curr_pos)) else: active_shape.rotate( inclination(active_shape.get_ref_point(), curr_pos) - inclination(active_shape.get_ref_point(), prev_pos)) prev_pos = curr_pos frames[active_shape] = draw_shape(screen, active_shape) # Game is over, print shapes location if needed if PRINT_OUT: for i in range(len(shapes)): print "Shape %u vertices:" % (i, ) for vertex in shapes[i].get_vertices(): print "\t (%.2f, %.2f)" % vertex
from shapes import Triangle, Rectangle, Oval, Paper rect1 = Rectangle() rect1.set_width(200) rect1.set_height(100) rect1.set_color("blue") rect1.draw() rect2 = Rectangle() rect2.set_width(50) rect2.set_height(150) rect2.set_color("yellow") rect2.set_x(100) rect2.set_y(100) rect2.draw() oval1 = Oval() oval1.randomize() oval1.draw() tri1 = Triangle(5, 5, 100, 5, 100, 200) tri1.draw() Paper.display()
from shapes import Triangle, Rectangle, Oval, Paper tri_sup = Triangle(50, 150, 20, 50, 80, 50) tri_sup.draw() Paper.display()
def appInit(self): """ Setup the scene and add the shapes to it """ glClearColor(1.0, 1.0, 1.0, 1.0) glColor(0.0, 0.0, 0.0) glPointSize(4.0) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(0.0, WINDOW_WIDTH, 0.0, WINDOW_HEIGHT) self.scene = Scene() tri = Triangle() tri.set_location(10, 50) tri.set_color(0, 1, 1) self.scene.addShape(tri) tri = Triangle() tri.set_location(70, 50) tri.set_color(1, 0, 1) tri.set_size(2, 2) self.scene.addShape(tri) tri = Triangle() tri.set_location(300, 50) self.scene.addShape(tri)
from shapes import Point from shapes import Triangle high = Point(0, 0, 4) low = Point(0, 0, -1) we = Point(0, 0, 3) we2 = Point(0, 0, 3) print we.is_equal(high) print we.is_equal(we2) tri = Triangle(high, low, we) print "high: " + tri.z_high.point_tos() + " expected: 0 0 4" print "low: " + tri.z_low.point_tos() + " expected: 0 0 -1" tri2 = Triangle(low, high, we) print "high: " + tri2.z_high.point_tos() + " expected: 0 0 4" print "low: " + tri2.z_low.point_tos() + " expected: 0 0 -1" tri3 = Triangle(we, high, low) print "high: " + tri3.z_high.point_tos() + " expected: 0 0 4" print "low: " + tri3.z_low.point_tos() + " expected: 0 0 -1" tri4 = Triangle(we, low, high) print "high: " + tri4.z_high.point_tos() + " expected: 0 0 4" print "low: " + tri4.z_low.point_tos() + " expected: 0 0 -1" tri5 = Triangle(high, we, low) print "high: " + tri5.z_high.point_tos() + " expected: 0 0 4" print "low: " + tri5.z_low.point_tos() + " expected: 0 0 -1"
from guizero import App, PushButton from shapes import Triangle, Rectangle, Oval, Paper def changeMe(): r.move(20, -20) t2.move(5, 0) app = App(width=600, height=600) button = PushButton(app, changeMe, text="Click me") p = Paper(app) t = Triangle(p) t.randomize() t.draw() t2 = Triangle(p) t2.randomize() t2.draw() r = Rectangle(p) r.randomize() r.width = 400 r.draw() r.color = 'red' r.width = 20 app.display()
from shapes import Triangle, Rectangle, Oval rect = Rectangle() rect.set_width(200) rect.set_height(100) rect.set_color('blue') rect.draw() square = Rectangle() square.set_width(50) square.set_height(50) square.set_color('yellow') square.set_x(120) square.set_y(120) square.draw() tri = Triangle(5, 5, 100, 5, 100, 200) tri.set_color('red') tri.draw()
from shapes import Paper, Shape, Triangle, Rectangle, Oval #*This Shape class is created in the shapes.py file paper = Paper() rect1 = Rectangle() rect1.set_width(200) rect1.set_height(100) rect1.set_x(0) rect1.set_y(0) rect1.set_color("blue") rect1.draw() rect2 = Rectangle() rect2.set_width(50) rect2.set_height(100) rect2.set_color("red") rect2.draw() oval1 = Oval() oval1.randomize() oval1.draw() tri1 = Triangle(121, 223, 2, 2, 234, 3) tri1.draw() paper.display()
from shapes import Triangle, Rectangle, Oval, Paper rect1 = Rectangle() rect1.set_height(100) rect1.set_width(200) rect1.set_color('orange') rect1.draw() rect2 = Rectangle() rect2.set_x(100) rect2.set_y(100) rect2.set_width(50) rect2.set_height(150) rect2.set_color('yellow') rect2.draw() oval = Oval() oval.randomize() oval.draw() triangle = Triangle(5, 5, 100, 5, 100, 200) triangle.set_color('green') triangle.draw() Paper.display()
def appInit(self): """ Setup the scene and add the shapes to it """ self.shapes = [] glClearColor(1.0, 1.0, 1.0, 1.0) glColor(0.0, 0.0, 0.0) # glPointSize(4.0) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(0.0, WINDOW_WIDTH, 0.0, WINDOW_HEIGHT) tri = Triangle() tri.set_location(10, 50) tri.set_color(0, 1, 1) self.shapes.append(tri) tri = Triangle() tri.set_location(70, 50) tri.set_color(1, 0, 1) tri.set_size(2, 2) self.shapes.append(tri) tri = Triangle() tri.set_location(300, 50) self.shapes.append(tri) self.Bind(wx.EVT_CONTEXT_MENU, self.OnContextMenu)
rect1 = Rectangle() rect1.set_width(200) rect1.set_width(100) rect1.set_color("blue") rect1.draw() rect2 = Rectangle() rect2.set_x(100) rect2.set_y(100) rect2.set_width(100) rect2.set_width(50) rect2.set_color("red") rect2.draw() oval = Oval() oval.randomize() oval.draw() triangle = Triangle(5, 5, 100, 100, 200, 100) triangle.set_x(150) triangle.set_y(250) triangle.set_color("green") triangle.draw() paper.display()
def appInit(self): """ Create the scene and intitialize the shapes to be put in the scene """ self.shapes = [] # set viewing projection glClearColor(1.0, 1.0, 1.0, 0.0) glColor(0.0, 0.0, 0.0) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(0.0, WINDOW_WIDTH, 0.0, WINDOW_HEIGHT) tri = Triangle() tri.set_location(10, 50) tri.set_color(0, 1, 1) self.shapes.append(tri) tri = Triangle() tri.set_location(70, 50) tri.set_color(1, 0, 1) tri.set_size(2, 2) self.shapes.append(tri) tri = Triangle() tri.set_location(300, 50) self.shapes.append(tri)
from shapes import Paper, Triangle, Rectangle, Oval # Instance of paper paper = Paper() # instance of the rectangle rect1 = Rectangle() rect2 = Rectangle() oval1 = Oval() triangle = Triangle(0,0,20,0,40,40,color="black") # Attributes rect1.set_width(200) rect1.set_height(100) rect1.set_color("blue") rect2.set_width(50) rect2.set_height(150) rect2.set_color("yellow") oval1.randomize(smallest=20, largest=200) # setting the position of the rect1 rect2.set_y(100) rect2.set_x(100) # draw the rectangle on paper rect1.draw() rect2.draw()
rect1.set_width(200) rect1.set_height(100) rect1.set_color("blue") #invoking draw method rect1.draw() # Paper.display() # Challenge Rectangle rect2 = Rectangle() rect2.set_x(100) rect2.set_y(100) rect2.set_width(50) rect2.set_height(150) rect2.set_color("yellow") #Challenge Oval oval1 = Oval(100,100) oval1.randomize() oval1.draw() rect2.draw() # Challenge Triangle tri1 = Triangle(5,5,100,5,100,200) tri1.set_color("green") tri1.draw() Paper.display()
def step_create_smooth_triangle_tri(context): context.tri = Triangle.smooth_triangle(context.p1, context.p2, context.p3, context.n1, context.n2, context.n3)
from shapes import Paper, Triangle, Rectangle, Oval paper = Paper() rect1 = Rectangle() rect1.set_width(200) rect1.set_height(100) rect1.set_color("orange") rect1.draw() rect2 = Rectangle() rect2.set_width(200) rect2.set_height(100) rect2.set_color("yellow") rect2.set_x(100) rect2.set_y(100) rect2.draw() tri1 = Triangle(5, 5, 12, 5, 20, 30) tri1.set_color("purple") tri1.set_x(398) tri1.set_y(500) tri1.draw() oval1 = Oval() oval1.randomize(25, 100) oval1.draw() paper.display()
class ResourceCounter: """A colored number for each resource representing how much has been selected along with up / down arrows to change how much is selected""" arrow_size_modifier = 0.3 arrow_separation = 0.3 arrow_color = common.BLACK font_x_offset = 0.15 # centering the font intuitively looks off-center :\ font_y_offset = 0.1 min_value = 0 max_value = 15 arrow_color = common.BLACK def __init__(self, x, y, resource, font): self.resource = resource self.font = font # counter number initialization self.num = SelectableText(font, '0', resource.value, 0, 0) font_height = get_font_height(font) font_width = get_font_width(font) # store this center location long-term for re-centering self.num_center = (int(x + ResourceCounter.font_x_offset * font_width), int(y + ResourceCounter.font_y_offset * font_height)) self.num.rect.center = self.num_center # arrow initialization arrow_size = int(font_height * ResourceCounter.arrow_size_modifier) arrow_offset = int(font_height / 2 + font_height * ResourceCounter.arrow_separation) self.up_arrow = Triangle(x, y - arrow_offset, arrow_size, ResourceCounter.arrow_color) self.down_arrow = Triangle(x, y + arrow_offset, arrow_size, ResourceCounter.arrow_color, pointed_up=False) # collision rects for detecting mouse / clicks self.up_arrow_collision = pygame.rect.Rect(0, 0, arrow_size, arrow_size) self.up_arrow_collision.center = (x, y - arrow_offset) self.down_arrow_collision = pygame.rect.Rect(0, 0, arrow_size, arrow_size) self.down_arrow_collision.center = (x, y + arrow_offset) def check_for_mouse(self, mouse_pos, mouse_click): """"Checks if the mouse is close enough to either arrows to highlight them, and if there was a click updates the counter (assuming bounds not passed)""" if self.up_arrow_collision.collidepoint(mouse_pos): # mouse is near the up arrow self.up_arrow.color = common.WHITE if mouse_click: cur_val = int(self.num.text) new_val = cur_val + 1 # bounds checking if new_val <= ResourceCounter.max_value: if cur_val == 9: # need to re-initialize text obj for centering self.num = SelectableText(self.font, str(new_val), self.resource.value, 0, 0) self.num.rect.center = self.num_center else: self.num.text = str(new_val) self.num.deselect() # up arrow is being moused over, so down arrow shouldn't be selected self.down_arrow.color = ResourceCounter.arrow_color elif self.down_arrow_collision.collidepoint(mouse_pos): # mouse is near the down arrow self.down_arrow.color = common.WHITE if mouse_click: cur_val = int(self.num.text) new_val = cur_val - 1 if new_val >= ResourceCounter.min_value: if cur_val == 10: # need to re-initialize text obj for centering self.num = SelectableText(self.font, str(new_val), self.resource.value, 0, 0) self.num.rect.center = self.num_center else: self.num.text = str(cur_val - 1) self.num.deselect() # down arrow is being moused over, so up arrow shouldn't be selected self.up_arrow.color = ResourceCounter.arrow_color else: # mouse is away; deselect all self.up_arrow.color = ResourceCounter.arrow_color self.down_arrow.color = ResourceCounter.arrow_color def draw(self, screen): self.up_arrow.draw(screen) self.num.draw(screen) self.down_arrow.draw(screen)
self.label = Label(master, text="This is the Root Window") self.label.pack() self.close_button = Button(master, text="Close", command=master.quit) self.close_button.pack() rectangle1 = Rectangle() rectangle1.set_width(200) rectangle1.set_height(100) rectangle1.set_color("blue") rectangle1.set_x(50) rectangle1.set_y(250) triangle1 = Triangle() # triangle1.randomise() triangle1.set_color("red") triangle1.x=150 triangle1.y=150 triangle1.x2=150 triangle1.y2=200 triangle1.x3=200 triangle1.y3=150 oval1 = Oval() # oval1.randomise() oval1.set_color("green") oval1.set_x(300) oval1.set_y(300)