def polyFlow(numPetals, petalSides, petalLen): '''Draws "flowers" with numPetals arranged around a center point. Each petal is a polygon with petalSides sides of length petalLen.''' # solution angle = 360.0/numPetals #angle to turn after each petal for i in range(numPetals): polygon(petalSides, petalLen) lt(angle)
def myTurtle(): num_side = raw_input("Enter the number of sides: " ) num_shap = raw_input("Enter the number of shapes: " ) num_sides = int(num_side) num_shape = int(num_shap) #window = turtle.Screen() # window.bgcolor("red") #polygon = turtle.Turtle() side_length = polygon(100, num_sides) ''' radius = 100 angle = 360.0 // num_sides delta = 100 #this value you must count colors = ['blue','white','black','green'] for i in range(num_shape): polygon.penup() polygon.goto(-200+delta*i, 100) polygon.pendown() polygon.pencolor(colors[i%4]) polygon.circle(radius) for j in range(num_sides): polygon.forward(side_length) polygon.right(angle) window.exitonclick() ''' print side_length.find_lenth()
def build_polygon(): points = [ point((0.0, 0.0)), point((100.0, 100.0), 5, 50.0), point((200.0, 0.0), 10, 30.0), point((300.0, 100.0), 7, 5.0), point((0.0, 100.0)), ] p = polygon(closed = False) for x in points: p.add(x) return p
def createNewpolygon(self, wherex, wherey, screenCoordinates=1): self.fromClass = None self.toClass = None # try the global constraints... res = self.ASGroot.preCondition(ASG.CREATE) if res: self.constraintViolation(res) self.mode = self.IDLEMODE return new_semantic_obj = polygon(self) ne = len(self.ASGroot.listNodes["polygon"]) if new_semantic_obj.keyword_: new_semantic_obj.keyword_.setValue( new_semantic_obj.keyword_.toString() + str(ne)) if screenCoordinates: new_obj = graph_polygon(self.UMLmodel.canvasx(wherex), self.UMLmodel.canvasy(wherey), new_semantic_obj) else: # already in canvas coordinates new_obj = graph_polygon(wherex, wherey, new_semantic_obj) new_obj.DrawObject(self.UMLmodel, self.editGGLabel) self.UMLmodel.addtag_withtag("polygon", new_obj.tag) new_semantic_obj.graphObject_ = new_obj self.ASGroot.addNode(new_semantic_obj) res = self.ASGroot.postCondition(ASG.CREATE) if res: self.constraintViolation(res) self.mode = self.IDLEMODE return res = new_semantic_obj.postCondition(ASGNode.CREATE) if res: self.constraintViolation(res) self.mode = self.IDLEMODE return self.mode = self.IDLEMODE if self.editGGLabel: self.statusbar.event(StatusBar.TRANSFORMATION, StatusBar.CREATE) else: self.statusbar.event(StatusBar.MODEL, StatusBar.CREATE) return new_semantic_obj
def createNewpolygon(self, wherex, wherey, screenCoordinates = 1): self.fromClass = None self.toClass = None # try the global constraints... res = self.ASGroot.preCondition(ASG.CREATE) if res: self.constraintViolation(res) self.mode=self.IDLEMODE return new_semantic_obj = polygon(self) ne = len(self.ASGroot.listNodes["polygon"]) if new_semantic_obj.keyword_: new_semantic_obj.keyword_.setValue(new_semantic_obj.keyword_.toString()+str(ne)) if screenCoordinates: new_obj = graph_polygon(self.UMLmodel.canvasx(wherex), self.UMLmodel.canvasy(wherey), new_semantic_obj) else: # already in canvas coordinates new_obj = graph_polygon(wherex, wherey, new_semantic_obj) new_obj.DrawObject(self.UMLmodel, self.editGGLabel) self.UMLmodel.addtag_withtag("polygon", new_obj.tag) new_semantic_obj.graphObject_ = new_obj self.ASGroot.addNode(new_semantic_obj) res = self.ASGroot.postCondition(ASG.CREATE) if res: self.constraintViolation(res) self.mode=self.IDLEMODE return res = new_semantic_obj.postCondition(ASGNode.CREATE) if res: self.constraintViolation(res) self.mode=self.IDLEMODE return self.mode=self.IDLEMODE if self.editGGLabel : self.statusbar.event(StatusBar.TRANSFORMATION, StatusBar.CREATE) else: self.statusbar.event(StatusBar.MODEL, StatusBar.CREATE) return new_semantic_obj
def cheb(points, show=False): # preprocessing points y = points[:, 1] mean_y = (np.max(y) - np.min(y)) / 2 points[:, 1] = points[:, 1] - mean_y p = polygon(points) #hull = ConvexHull(p) c = ChebyshevCenter(p) c._transform() #c.showPolygon() center = c.solve(show=show) center[1] += mean_y points[:, 1] = points[:, 1] + mean_y return center
def update(dt): global gameState if gameState == "Menu": menuPolygon.update(dt) if menuPolygon.x > globals.window_width or menuPolygon.x < 0: menuPolygon.velX *= -1 if menuPolygon.y > globals.window_height or menuPolygon.y < 0: menuPolygon.velY *= -1 elif gameState == "Play": #SPAWNING ENEMIES if random.randint(0,100) < 5 and len(game_objects) < 20: pn = polygon(5, spawnShard) game_objects.append(pn) for o in game_objects: o.update(dt) player1.setTarget() if keys['up']: player1.velY = 9 elif keys['down']: player1.velY = -9 else: player1.velY = 0 if keys['right']: player1.velX = 9 elif keys['left']: player1.velX = -9 else: player1.velX = 0 #collisions: for o in game_objects: for o2 in game_objects: if o is o2: continue elif o.__class__ == o2.__class__: continue #elif distance(o.x, o.y, o2.x, o2.y) > 2*(o.radius + o2.radius): # continue else: if checkCollision(o, o2): if o.__class__.__name__ == 'bullet' and o2.__class__.__name__ == 'polygon': o.handleCollision() o2.handleCollision() if o.__class__.__name__ == 'shard' and o2.__class__.__name__ == 'player': o.handleCollision() player1.addSide(o.x, o.y, o.color) if o.__class__.__name__ == 'polygon' and o2.__class__.__name__ == 'player': player1.handleCollision() game_objects.remove(o) for o in game_objects: if o.__class__.__name__ is not "player": if o.x > globals.window_width: o.x = 0 elif o.x < 0: o.x = globals.window_width if o.y > globals.window_height: o.y = 0 elif o.y < 0: o.y = globals.window_height if not o.alive: game_objects.remove(o) del(o) if player1.sides < 3: gameState = "Menu"
def offscreen(o): if o.__class__.__name__ == 'player': return False return o.x > window.width or o.x < 0 or o.y > window.height or o.y < 0 keys = {'up': False, 'down': False, 'right': False, 'left': False} game_objects = [] player1 = player() font.add_file('TulpenOne-Regular.ttf') title = text.Label("Taking Sides", font_name="Tulpen One", x=100, y=globals.window_height/2, font_size=85) subtitle = text.Label("Press 'P' to play...", font_name="Tulpen One", x=106, y = globals.window_height/2 - 100, font_size = 38) menuPolygon = polygon(5,None) menuPolygon.x = 600 menuPolygon.y = 2 * globals.window_height / 3 menuPolygon.radius = -(globals.window_width / 10) menuPolygon.velY = random.randint(-4,4) menuPolygon.velX = random.randint(-4,4) menuPolygon.setVertices() game_objects.append(player1) def reset(): global game_objects, player1 game_objects = [] player1 = player() game_objects.append(player1)
# simple.py # - a simple python program from random import random from polygon import * from turtle import up,down,goto def move(x,y): up() goto(x,y) down() #move(10,10) for i in range(10): x = int(random()*200) y = int (random()*200) move(x,y) side =int(random()*5+3) if (side == 3): color('green') elif side == 5: color('blue') elif side == 7: color('red') else: color('black') polygon(int(random()*50),side)
from polygon import * world = TurtleWorld() bob = Turtle() arc(bob, 100, 50) polygon(bob, 10, 30)
def update(dt): global gameState if gameState == "Menu": menuPolygon.update(dt) if menuPolygon.x > globals.window_width or menuPolygon.x < 0: menuPolygon.velX *= -1 if menuPolygon.y > globals.window_height or menuPolygon.y < 0: menuPolygon.velY *= -1 elif gameState == "Play": #SPAWNING ENEMIES if random.randint(0, 100) < 5 and len(game_objects) < 20: pn = polygon(5, spawnShard) game_objects.append(pn) for o in game_objects: o.update(dt) player1.setTarget() if keys['up']: player1.velY = 9 elif keys['down']: player1.velY = -9 else: player1.velY = 0 if keys['right']: player1.velX = 9 elif keys['left']: player1.velX = -9 else: player1.velX = 0 #collisions: for o in game_objects: for o2 in game_objects: if o is o2: continue elif o.__class__ == o2.__class__: continue #elif distance(o.x, o.y, o2.x, o2.y) > 2*(o.radius + o2.radius): # continue else: if checkCollision(o, o2): if o.__class__.__name__ == 'bullet' and o2.__class__.__name__ == 'polygon': o.handleCollision() o2.handleCollision() if o.__class__.__name__ == 'shard' and o2.__class__.__name__ == 'player': o.handleCollision() player1.addSide(o.x, o.y, o.color) if o.__class__.__name__ == 'polygon' and o2.__class__.__name__ == 'player': player1.handleCollision() game_objects.remove(o) for o in game_objects: if o.__class__.__name__ is not "player": if o.x > globals.window_width: o.x = 0 elif o.x < 0: o.x = globals.window_width if o.y > globals.window_height: o.y = 0 elif o.y < 0: o.y = globals.window_height if not o.alive: game_objects.remove(o) del (o) if player1.sides < 3: gameState = "Menu"
player1 = player() font.add_file('TulpenOne-Regular.ttf') title = text.Label("Taking Sides", font_name="Tulpen One", x=100, y=globals.window_height / 2, font_size=85) subtitle = text.Label("Press 'P' to play...", font_name="Tulpen One", x=106, y=globals.window_height / 2 - 100, font_size=38) menuPolygon = polygon(5, None) menuPolygon.x = 600 menuPolygon.y = 2 * globals.window_height / 3 menuPolygon.radius = -(globals.window_width / 10) menuPolygon.velY = random.randint(-4, 4) menuPolygon.velX = random.randint(-4, 4) menuPolygon.setVertices() game_objects.append(player1) def reset(): global game_objects, player1 game_objects = [] player1 = player() game_objects.append(player1)
# simple.py # - a simple python program from random import random from polygon import * from turtle import up, down, goto def move(x, y): up() goto(x, y) down() #move(10,10) for i in range(10): x = int(random() * 200) y = int(random() * 200) move(x, y) side = int(random() * 5 + 3) if (side == 3): color('green') elif side == 5: color('blue') elif side == 7: color('red') else: color('black') polygon(int(random() * 50), side)
def main(): print("Section 4.12 Exercises on page 53") #print("Exercise 4.3 on page ") # Here is the first line of the main function's code. root = tkinter.Tk() root.title("Draw!") cv = tkinter.Canvas(root, width=600, height=600) cv.pack(side=tkinter.LEFT) # This is how we create a turtle to draw # on the canvas we created above. t = turtle.RawTurtle(cv) screen = t.getscreen() center_position = t.position() t.penup() t.setposition(0, 100) t.pendown() s1 = "Exercise 4.3.1" print(s1) t.write(s1, font=("Arial", 16, "normal")) t.penup() t.setposition(center_position) t.pendown() square(t, 100) reply = input("Tap ENTER to continue...") t.reset() t.penup() t.setposition(0, 100) t.pendown() s1 = "Exercise 4.3.2" print(s1) t.write(s1, font=("Arial", 16, "normal")) t.penup() t.setposition(center_position) t.pendown() pass_bob(t, 20) reply = input("Tap ENTER to continue...") t.reset() t.penup() t.setposition(-250, 100) t.pendown() s1 = "Exercise 4.3.3 already done in Exercise 4.1.1 and 4.1.2." print(s1) t.write(s1, font=("Arial", 16, "normal")) t.penup() t.setposition(center_position) t.pendown() reply = input("Tap ENTER to continue...") t.reset() t.penup() t.setposition(0, 200) t.pendown() s1 = "Exercise 4.3.3a" print(s1) t.write(s1, font=("Arial", 16, "normal")) t.penup() t.setposition(center_position) t.pendown() polygon(t, 5, 100) reply = input("Tap ENTER to continue...") t.reset() t.penup() t.setposition(0, 200) t.pendown() s1 = "Exercise 4.3.4." print(s1) t.write(s1, font=("Arial", 16, "normal")) t.penup() t.setposition(center_position) t.pendown() circle(t, 100) reply = input("Tap ENTER to continue...") t.reset() t.penup() t.setposition(0, 200) t.pendown() s1 = "TODO; Exercise 4.3.5." print(s1) t.write(s1, font=("Arial", 16, "normal")) t.penup() t.setposition(center_position) t.pendown() circle(t, 100) reply = input("Tap ENTER to continue...") #t.done() #turtle.done() """ # With the lines below, the "turtle" will look like a pencil. screen.register_shape("pencil.gif") t.shape("pencil.gif") # This sets the lower left corner to 0,0 and the upper right corner to 600,600. screen.setworldcoordinates(0, 0, 600, 600) screen.bgcolor("white") # A frame is an invisible widget that holds other widgets. This frame goes # on the right hand side of the window and holds the buttons and Entry widgets. frame = tkinter.Frame(root) frame.pack(side=tkinter.RIGHT, fill=tkinter.BOTH) pointLabel = tkinter.Label(frame, text="Width") pointLabel.pack() # This entry widget allows the user to pick a width for their lines. # With the pointSize variable below you can write pointSize.get() to to # the contents of the entry widget and pointSize.set(val) to set the value # of the entry widget to val. Initially the pointSize is set to 1. str(1) is needed because # the entry widget must be given a string. pointSize = tkinter.StringVar() pointEntry = tkinter.Entry(frame, textvariable=pointSize) pointEntry.pack() pointSize.set(str(1)) # This is an event handler. Handling the quit button press results in destroying the window # and quitting the application. def quitHandler(): root.destroy() root.quit() # This is how a button is created in the frame. The quitHandler is the event handler for button # presses of the "Quit" button. quitButton = tkinter.Button(frame, text="Quit", command=quitHandler) quitButton.pack() # Here is another event handler. This one handles mouse clicks on the screen. def clickHandler(x, y): # When a mouse click occurs, get the pointSize entry value and set the width of the # turtle called "t" to the pointSize value. The int(pointSize.get()) is needed because # the width is an integer, but the entry widget stores it as a string. t.width(int(pointSize.get())) t.goto(x, y) # Here is how we tie the clickHandler to mouse clicks. screen.onclick(clickHandler) # Finally, this code is last. It tells the application to enter its event processing loop # so the application will respond to events. tkinter.mainloop() #print("Exercise 4.1") ## download http://greenteapress.com/thinkpython2/code/polygon.py ## http://web.cecs.pdx.edu/~lmd/cs161/turtle-excerpt.htm (not everything works with Dale's version of turtle. ## https://docs.python.org/3/library/turtle.html#module-turtle #bob = turtle.Turtle() ## bob.reset() ## bob.screen.setworldcoordinates(100,100,250,207.5) ## bob.screen.setworldcoordinates(-50,-7.5,50,7.5) ## for _ in range(72): ## left(10) ## for _ in range(8): ## left(45); fd(2) # a regular octagon ## reply = input("Tap ENTER to continue...") ## bob.reset() #bob.penup() ## bob.setx(100) ## bob.sety(100) #radius = 20 #bob.pendown() #polygon.circle(bob, radius) #reply = input("Tap ENTER to continue...") """ print("Section 4.12 Exercises on page 59") t.reset() t.penup() t.setposition(0, 200) t.pendown() s1 = "Exercise 4.1.1 on page 59" print(s1) t.write(s1, font=("Arial", 16, "normal")) print("Stack diagram should come out of the execution of circle(), next.") t.penup() t.setposition(center_position) t.pendown() circle(t, 100) reply = input("Tap ENTER to continue...") t.reset() t.penup() t.setposition(0, 200) t.pendown() s1 = "Exercise 4.1.2 on page 59" print(s1) t.write(s1, font=("Arial", 16, "normal")) t.penup() t.setposition(center_position) t.pendown() s1 = "TODO; compare arc versions accuracy." print(s1) t.write(s1, font=("Arial", 16, "normal")) reply = input("Tap ENTER to continue...") t.reset() t.penup() t.setposition(0, 200) t.pendown() s1 = "Exercise 4.2" print(s1) t.write(s1, font=("Arial", 16, "normal")) t.penup() t.setposition(center_position) t.pendown() flower.flower(t, 6, 100, 100) # turtle, petals, radius, angle reply = input("Tap ENTER to continue...") t.reset() t.penup() t.setposition(0, 200) t.pendown() s1 = "Exercise 4.3" print(s1) t.write(s1, font=("Arial", 16, "normal")) t.penup() t.setposition(center_position) t.pendown() pie.draw_pie( t, 5, 10 ) # t: Turtle, n: number of segments, r: length of the radial spokes reply = input("Tap ENTER to continue...") t.reset() t.penup() t.setposition(0, 200) t.pendown() s1 = "TODO; Exercise 4.4" print(s1) t.write(s1, font=("Arial", 16, "normal")) t.penup() t.setposition(center_position) t.pendown() s1 = "TODO; draw letters. How do I exit from this?" print(s1) t.write(s1, font=("Arial", 16, "normal")) typewriter.main(t) reply = input("Tap ENTER to continue...")