# import math def sin (deg, rad): radians = deg*math.pi/180.0 return int(rad*math.sin(radians)) def cos (deg, rad): radians = deg*math.pi/180.0 return int(rad*math.cos(radians)) g.antialias(1) #### #establish polygon as a list of coordinates #### def globalpoint(cx, cy, r, degrees): #positions dot in relation to centerpoint cx, cy x = cx + cos(degrees, r) #x=rcos(theta)+shiftx y = cy + sin(degrees, r) #y=rsin(theta)+shifty return [x, y, degrees] #returns list [x, y, degrees] describing global point position def findegrees(count): list = [] for i in range (0, count): m = 360/count list.append (m*i) return list decpoints = findegrees(10) #decpoints = [0, 36, 72, 108, 144, 180, 216, 252, 288, 324] #gives the angle vertices of a regular decagon def polygon (cx, cy, r, count, degreeshift): globalpolylist = [] parameters = [cx, cy, r, count, degreeshift] globalpolylist.append (parameters) polypts = [] polypts.extend (findegrees(count)) while len(polypts)>0: polypts[0] = degreeshift + polypts[0] #shift first vertex by degreeshift number of degrees pair = globalpoint (cx, cy, r, polypts[0]) globalpolylist.append (pair) #adds pair to globalpolylist in the form [x0, y0, degrees0], [x1, y1, degrees1]... del polypts[0] #removes first vertex recursively return globalpolylist #command polygon returns globalpolylist for polypoints: #[parameters, [x0, y0, degrees0], [x1, y1, degrees1], ...] def decagon (cx, cy, r, degreeshift): return polygon (cx, cy, r, decpoints, degreeshift) #this polygon is a decagon, and returns a globalpolylist for itself def triangle (cx, cy, r, degreeshift): return polygon (cx, cy, r, 3, degreeshift) def square (cx, cy, r, degreeshift): return polygon (cx, cy, r, 4, degreeshift) def pentagon (cx, cy, r, degreeshift): return polygon (cx, cy, r, 5, degreeshift) def hexagon (cx, cy, r, degreeshift): return polygon (cx, cy, r, 6, degreeshift) #et cetera ##abstraction ##parameters def polycenter (polygonname): return [(polygonname[0])[0], (polygonname[0])[1]] #gives center of polygon in form [cx, cy] def r (polygonname): return (polygonname[0]) [2] def count (polygonname): return (polygonname[0]) [3] def degreeshift (polygonname): return (polygonname[0]) [4] ##particulars #use the usual vertexindex + 1; the first entry would be #polygonname[1] due to 'parameters' occupying polygonname[0] 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] def degrees (polygonname, vertexindex): return(polygonname[vertexindex])[2] #### #a list of operations on the polygon #### def circle (cx, cy, radius, color): for degrees in range (0, 360): g.setPixel (int(cx)+cos(degrees, radius), int(cy)+sin(degrees, radius), color) def circlepoint (polygonname, radius, color): ##draws circles around points polyname = [] polyname.extend (polygonname) for i in range (1, len(polyname)): #bypass 'parameters' of polyname by starting at polyname[1] circle (xcoor(polyname, i), ycoor(polyname, i), radius, color) def surroundpoly (polygonname, surrounders, radius): #this lets you position polygon shapes around other polygons polyname = [] polyname.extend (polygonname) for i in range (1, len(polyname)): x = xcoor(polyname, i) + cos (degrees(polyname, i), radius) y = ycoor(polyname, i) + sin (degrees(polyname, i), radius) surrounders (x, y, degrees(polyname, i)) def arrowhead (cx, cy, degrees, angle, radius, width, color): cx = int(cx) cy = int(cy) angle = 90-angle/2 g.pen (color) for i in range (0, width): x = cx+cos(degrees+angle, i) y = cy+sin(degrees+angle, i) g.line (x, y, x+cos(degrees, radius), y+sin(degrees, radius)) x = cx+cos(degrees-angle, i) y = cy+sin(degrees-angle, i) g.line (x, y, x+cos(degrees, radius), y+sin(degrees, radius)) def arrowheads (polygonname, radius, angle, width, null, color): polyname = [] polyname.extend (polygonname) for i in range(1, len(polyname)): ##bypass 'parameters' in globalpolylist by starting from 1 arrowhead (xcoor(polyname, i), ycoor(polyname, i), degrees(polyname, i), angle, radius, width, color) def parallelogram (cx, cy, degrees, angle, radius, width, spacing, color): #draws 2 parallelograms side by side cx = int(cx) cy = int(cy) angle = 90-angle/2 g.pen (color) width = width-spacing/2 for i in range (spacing/2, width/2): x = cx-cos(degrees+angle, i) y = cy-sin(degrees+angle, i) g.line (x, y, x+cos(degrees, radius), y+sin(degrees, radius)) x = cx-cos(degrees-angle, i) y = cy-sin(degrees-angle, i) g.line (x, y, x+cos(degrees, radius), y+sin(degrees, radius)) def parallelograms (polygonname, radius, angle, width, spacing, color): polyname = [] polyname.extend (polygonname) for i in range (1, len(polyname)): #bypass 'parameters' by starting with polyname[1] parallelogram (xcoor(polyname, i), ycoor(polyname, i), degrees(polyname, i), angle, radius, width, spacing, color) def diamond (cx, cy, degrees, angle, width, color): cx = int(cx) cy = int(cy) angle = angle/2 def sin (deg, rad): radians = deg*math.pi/180.0 return int(rad*math.sin(radians)) def cos (deg, rad): radians = deg*math.pi/180.0 return int(rad*math.cos(radians)) g.pen (color) for i in range (0, width): x = cx+cos(degrees+angle, i) y = cy+sin(degrees+angle, i) g.line(x, y, x+cos(degrees-angle, i), y+sin(degrees-angle, i)) x = cx+cos(degrees-angle, i) y = cy+sin(degrees-angle, i) g.line(x, y, x+cos(degrees+angle, i), y+sin(degrees+angle, i)) def diamonds (polygonname, null, angle, width, null, color): polyname = [] polyname.extend (polygonname) for i in range (1, len(polyname)): ##start with polyname[1] to bypass 'parameters' diamond (xcoor(polyname, i), ycoor(polyname, i), degrees(polyname, i), angle, width, color) def longdiamond (cx, cy, degrees, angle, radius, width, color): arrowhead (cx, cy, degrees, angle, radius, width, color) diamond (cx+cos(degrees, radius), cy+sin(degrees, radius), degrees, angle, width, color) def longdiamonds (polygonname, radius, angle, width, null, color): polyname = [] polyname.extend (polygonname) for i in range (1, len(polyname)): ##bypass 'parameters' by starting with 1 in globalpolylist longdiamond (xcoor(polyname, i), ycoor(polyname, i), degrees(polyname, i), angle, radius, width, color) #### #drawing pictures #### #random number function def random(mod): rseed = g.getTime(4) bb = 198621 mm = (98621+g.getTime(4)) bl = (long(rseed)*bb+1) temp = int(bl%mm) return temp%mod ## INDEX OF COMMANDS ## arrowheads (polygonname, radius, angle, width, null, color) ## parallelograms (polygonname, radius, angle, width, spacing, color) ## diamonds (polygonname, null, angle, width, null, color) ## longdiamonds (polygonname, radius, angle, width, null, color) commandlist = [arrowheads, parallelograms, diamonds, longdiamonds] ##doing things def p(cx, cy): p = [] rinit = random(10) + 10 ct = (random(6)+1)*2 angle = int(360/ct*random(3)) shift = random(90) for i in range (0, random(10)): i = i+1 r = rinit*i*i/5 degreeshift = angle/2*(random(2)+1) newp = polygon(cx, cy, r, ct, degreeshift) p.append ([r, newp]) return p def makeshapes (pol): pp = [] pp.extend (pol) while len(pp)>0: polyname = (pp[0])[1] r = (pp[0])[0] deg = int(degrees(polyname, 1)*2) width = int(math.pi*r/count(polyname)/(random(2)+1)) shapeheight = width spacing = (width/2) color = random(101) commandlist[random(4)] (polyname, shapeheight, deg, width, spacing, color) del pp[0] while 1: if g.getMouse(3) == 100: g.paper(40) g.paper(40) g.paper(40) mx = g.getMouse(1) my = g.getMouse(2) p1 = p(mx, my) makeshapes(p1) g.refresh() |
9c) Do problem 9a, this time with an interactive display of the difference in display area.
this program makes random patterns centered at the mouse click. |
![]() click to see movie |
|
full code | back to movies |