#
import math g.paper(20) #### #establish polygon as a list of coordinates #### def globalpoint(cx, cy, r, degrees): #positions dot in relation to centerpoint cx, cy x = cx + r * (math.cos(degrees*math.pi/180.0)) #x=rcos(theta)+shiftx y = cy + r * (math.sin(degrees*math.pi/180.0)) #y=rsin(theta)+shifty return [x, y] #returns list [x, y] describing global point position def polygon (cx, cy, r, polypts, degreeshift): polypoints = [] polypoints.extend(polypts) globalpolylist = [] while len(polypoints)>0: polypoints[0] = degreeshift + polypoints[0] #shift first vertex by degreeshift number of degrees pair = globalpoint (cx, cy, r, polypoints[0]) globalpolylist.append (pair) #adds pair to globalpolylist in the form [[x0, y0], [x1, y1], ...,] del polypoints[0] #removes first vertex recursively return globalpolylist #command polygon returns globalpolylist for polypoints decpoints = [0, 36, 72, 108, 144, 180, 216, 252, 288, 324] #gives the angle vertices of a regular decagon def decagon (cx, cy, r, degreeshift): return polygon (cx, cy, r, decpoints, degreeshift) #this polygon is a decagon, and returns a globalpolylist for itself ##abstraction def xcoor (polygonname, vertexindex): #extracts the x-coordinate of a particular vertex from the # globalpolylist of a particular polygon return(polygonname[vertexindex])[0] def ycoor (polygonname, vertexindex): return(polygonname[vertexindex])[1] #### #a list of operations on the polygon #### def connectlines (polygonname, color): ##draws edges of polygon g.pen(color) polyname = [] polyname.extend (polygonname) #makes a duplicate of globalpolylist for polygonname polyname.append (polyname[0]) #sets up for last point to connect to first while len(polyname)>1: #1 rather than 0 sets up for g.line using pairs of points g.line(int(xcoor(polyname, 0)), int(ycoor(polyname, 0)), int(xcoor(polyname, 1)), int(ycoor(polyname, 1))) del polyname[0] def dot (cx, cy, color): ##creates a dot centered at (cx, cy) for i in range (0, 360): angle = math.pi*i/180.0 for r in range (0, 2): c = int (cx + math.cos(angle)*r) s = int (cy + math.sin(angle)*r) g.setPixel(c, s, color) def drawdots (polygonname, color): ##draws points of polygon polyname = [] polyname.extend (polygonname) while len(polyname)>0: dot (xcoor(polyname, 0), ycoor(polyname, 0), color) del polyname[0] def circle (cx, cy, radius, color): for degrees in range (0, 360): radians = degrees * math.pi/180.0 sin = int(radius*math.sin(radians)) cos = int(radius*math.cos(radians)) g.setPixel (int(cx)+cos, int(cy)+sin, color) def circlepoint (polygonname, radius, color): ##draws circles around points polyname = [] polyname.extend (polygonname) while len(polyname)>0: circle (xcoor(polyname, 0), ycoor(polyname, 0), radius, color) del polyname[0] #### #drawing pictures #### while 1: m1 = g.getMouse(1) m2 = g.getMouse(2) decradius = (m1+100-m2)/4 if (g.getMouse(3) > 0): decagonA = (decagon(m1, m2, decradius, 5)) connectlines(decagonA, 40) drawdots(decagonA, 70) circlepoint(decagonA, decradius/2, 50) else: g.paper (20) |
5c) relate a 10 point polygon to the position of the mouse |
![]() click to see movie |
|
full code | back to movies |