def constrain(self): """ Cages the flock inside the x, y, w, h area. The actual cage is a bit larger, so boids don't seem to bounce of invisible walls (they are rather "encouraged" to stay in the area). If a boid touches the ground level, it may decide to perch there for a while. """ dx = self.w * 0.1 dy = self.h * 0.1 for b in self: if b.x < self.x-dx: b.vx += random(dx) if b.y < self.y-dy: b.vy += random(dy) if b.x > self.x+self.w+dx: b.vx -= random(dx) if b.y > self.y+self.h+dy: b.vy -= random(dy) if b.z < 0: b.vz += 10 if b.z > 100: b.vz -= 10 if b.y > self._perch_y and random() < self._perch: b.y = self._perch_y b.vy = -abs(b.vy) * 0.2 b.is_perching = True try: b._perch_t = self._perch_t() except: b._perch_t = self._perch_t
def eloquate(noun, antonise=True): """Returns an alliteration with an adjective. Picks a synonym for the given noun from WordNet. Alliterates an adjective for this synonym. """ antonym = en.noun.antonym(noun) if antonise and len(antonym) > 0 and random() > 0.4: antonym = choice(choice(antonym)) return "no " + eloquate(antonym, antonise=False) noun = choice(choice(en.noun.hyponyms(noun))) adjective = alliterate(noun, type=NOUN) if adjective == None: noun = choice(choice(en.noun.hypernyms(noun))) adjective = alliterate(noun, type=NOUN) if adjective == None: return noun elif random() > 0.2: return adjective + " " + noun elif random() > 0.5: return noun + " " + adjective else: return noun + " so " + adjective
def __init__(self, n, x, y, w, h): for i in range(n): dx = random(w) dy = random(h) z = random(200) b = Boid(self, x+dx, y+dy, z) self.append(b) self.x = x self.y = y self.w = w self.h = h self.scattered = False self._scatter = 0.005 self._scatter_t = 50 self._scatter_i = 0 self._perch = 1.0 # Lower this number to simulate diving. self._perch_y = _ctx.HEIGHT self._perch_t = lambda:25+random(50) self.has_goal = False self.flee = False self._gx = 0 self._gy = 0 self._gz = 0
def mistake(txt, x, y, pt=20, slant=0.5): """Draws a piece of pixie text in strikethrough. This command is used as a callback from paragraph(), inserting some errrors in the paragraph flow here and there. """ #Somewhere in the Pixie-Dingbats font are strikethrough characters. glyph = choice(("C", "D")) #Guess the length of txt set in the paragraph() command. #The dx is a visual correction, so the typo appears not #too close to the previous word, and not too close to the next. width = len(txt) * random(pt*0.4, pt*0.5) dx = width/len(txt) * 0.5 #Create a typo in the txt string #At the current position, draw the typo txt in paragraph(). char = choice(txt[min(len(txt)-1,1):]) txt = txt.replace(char, choice("abcdefghijklmnopqrstuvwxyz")) paragraph(txt, x-dx, y, width*2, pt, slant) #Now, strikethrough the typo. _ctx.push() _ctx.scale(width/(pt*2.5), random(0.7, 1.3)) _ctx.rotate(random(-3,3)) _ctx.font("Pixie-Dingbats") _ctx.text(glyph, x-dx, y) _ctx.pop() return width
def node(x, y, d): """Draws a small pixie circle. This function is expected to work with line(), together creating a network of nodes. """ _ctx.push() #Set color to pixiecolor(), varying the K value #to simulate pen pressure. _ctx.colormode(CMYK) cc, mc, yc, kc = COLOR _ctx.fill(cc, mc, yc, kc+random(-0.2,0.2)) pt = 30 _ctx.font("Pixie-Dingbats", pt) glyphs = ("P","Q","R") #Scale the glyph to diameter d. _ctx.transform(CENTER) f = d/(0.6*pt) _ctx.scale(f) for i in range(random(1,3)): #Draw the glyph, with its center roughly at (x,y). _ctx.text(choice(glyphs), x-pt/2, y+pt/4) _ctx.pop()
def border(x, y, width, pt=20, slant=0.5): """Draws a handrwritten rectangle at (x,y) with given width. This command is usually called from within the pixie text paragraph() command as a callback that draws borders around words. """ _ctx.transform(CENTER) #Somewhere in the Pixie-Dingbats font are rectangle characters. glyph = choice(("A", "B")) #A thing about borders is that you #usually draw one, and then move your pen around on those lines, #so the border becomes really thick and visible. #The same is done here with a for-loop. for i in range(random(1,5)): glyphwidth = random(2.3, 2.4) _ctx.push() _ctx.scale(width/(glyphwidth*pt), random(1.1, 1.2)) _ctx.skew(slant*7 + random(8)) _ctx.rotate(random(-slant,0)) f = width/(glyphwidth*pt) f = f*glyphwidth*pt - glyphwidth*pt f = f/2 _ctx.font("Pixie-Dingbats", pt) _ctx.text(glyph, x+f, y, width) _ctx.pop()
def heading(txt, x, y, width, pt=30, slant=0.0): """Draws a title heading in Tom's handwriting. Draws the string txt in Tom's handwriting, positioned at x, y with the given width, font size pt. The slant argument controls the speed at which Tom writes. The lineheight setting of spacing() is taken into account. The text is drawn in color(). """ txt = txt.upper() _ctx.transform(CENTER) dx = x dy = y for i in range(len(txt)): _ctx.push() #Vary each individual character #to simulate handwriting. _ctx.scale(random(0.9, 1.5), random(0.9, 1.5)) _ctx.skew(slant*10 + random(8)) _ctx.rotate(slant*-2) #Set color to pixiecolor(), varying the K value #to simulate pen pressure. _ctx.colormode(CMYK) c, m, y, k = COLOR _ctx.fill(c, m, y, k+random(-0.0,0.3)) #Draw the current character in txt in the given fontsize. _ctx.font("Pixie-Fat", pt) _ctx.text(txt[i], dx, dy, width) #Advance the cursor to the next character in txt. dx += random(pt*1.4, pt*1.5) dy += random(-pt*0.1, pt*0.1) #Advance to a new line if this line exceeds the width, #and is at the end of a word. #The spacing() lineheight is taken into account. if txt[i] == "\n" or (dx-x > width and txt[i] in (" ", ".", ",")): dx = x + random(-pt*0.1, pt*0.2) dy += random(pt*1.3, pt*1.6) * SPACING _ctx.pop() #To top it off, draw a cool doodle next to the heading. if random(100) > 97: sprite(dx+pt*0.3, dy, pt=pt*0.9) #Return the offset of the cursor. dy += random(pt*0.7, pt*0.9) * SPACING return (dx,dy)
def list(title, list, x, y, width, pt=20, slant=0.5): """Draws a small list scheme. Draws a list scheme, with centrally, the title in a border. Arrows branch from the title to words in the given list. """ from math import radians, sin, cos #Draw the title in a border using pixie(). #A space is added to the title to ensure it draws a border around it. spacing(1.0) keywords(title.split(" ")) txt = title+" " x_end, y = paragraph(txt, x, y, width, pt, slant) y -= pt/1.25 for i in range(len(list)): _ctx.push() #Somewhere in the Pixie-Dingbats font are arrow characters. glyph = choice(("E", "F", "G")) #Set color to pixiecolor(), varying the K value #to simulate pen pressure. _ctx.colormode(CMYK) cc, mc, yc, kc = COLOR _ctx.fill(cc, mc, yc, kc+random(-0.1,0.2)) #Draw an arrow branching from the title. _ctx.transform(CORNER) _ctx.push() _ctx.translate(x+pt/2,y) a = random(-40,-35)*(i+1) _ctx.rotate(a) f = random(1.0,1.5) _ctx.scale(f, max(1,f/2)) _ctx.font("Pixie-Dingbats", pt) _ctx.text(glyph, pt/3, pt*0.35) _ctx.pop() #Good old-fashioned geometry to #calculate where we put the list item next to an arrow. #Play around with the numbers to get the position exactly right. glyphwidth = 4.5 dx = cos(radians(a)) * pt * glyphwidth * f dy = sin(radians(a)) * pt * glyphwidth * (f/1.5) if a % 360 > 0 and a % 360 < 180: dy += pt*1.5 if a % 360 > 240 and a % 360 < 360: dy -= pt/2 if a % 360 > 80 and a % 360 < 110: dy += pt/2 _ctx.transform(CENTER) paragraph(list[i], x+dx, y-dy+pt, width/2, pt*0.75)
def verse(word): """Creates a small rhyme for a given word. The rhyme is based on WordNet's description for the word. This description is eloquated (alliterated or antonated), incorporated. """ g = en.noun.gloss(word) words = g.split(" ") for i in range(len(words)): w = words[i] w = w.replace("\"", "") if en.is_noun(w): w = eloquate(w) if random(100) > 60: if en.is_noun(w): w = incorporate(w).upper() if en.is_verb(w): w = incorporate(w, VERB) if en.is_adjective(w): w = incorporate(w, ADJECTIVE) if i > 0 and i % 3 == 0: words[i] = words[i] + "\n" words[i] = w g = " ".join(words) g = g.replace("type A ", "!") g = g.replace("group A ", "!") return g
def genPath(ctx): s = ctx.spaces() + """beginpath(%s,%s)\n""" % ( nr(ctx,XCOORD),nr(ctx,YCOORD)) for i in range(random(1,10)): s += genPathDraw(ctx) s += ctx.spaces() + """endpath()\n""" return s
def genPath(ctx): s = ctx.spaces() + """beginpath(%s,%s)\n""" % (nr(ctx, XCOORD), nr(ctx, YCOORD)) for i in range(random(1, 10)): s += genPathDraw(ctx) s += ctx.spaces() + """endpath()\n""" return s
def incorporate(word, type=NOUN): """Combines this noun with another. This results in invented words that have a double meaning, for instance the lyrical "flowerewolf" that is even a palindrome! By specifying a type (None, NOUN, ADJECTIVE, ADVERB), you can specify what kind of word to mix in. """ if type == NOUN: f = nouns if type == ADJECTIVE: f = adjectives if type == VERB: word = en.verb.infinitive(word) f = verbs for i in [4,3,2,1]: a = alliterations(head=word[-i:]) if type != None: a = f(a) if len(a) > 0: tail = choice(a) if random() > 0.25: if i > len(word): return tail return word + tail[i:] return word + tail[i:]
def perch(self, ground=None, chance=1.0, frames=lambda:25+random(50)): if ground == None: ground = _ctx.HEIGHT self._perch = chance self._perch_y = ground self._perch_t = frames
def underline(x, y, width, pt=20): """Draws a horizontal line. Draws a horizontal line at (x,y) with the given width. This command serves as a callback for paragraph() to underline a paragraph of text, just as Tom does. """ _ctx.font("Pixie-Dingbats", pt) y += random(pt*0.5) #In handwriting, when you stress something by underlining it, #you never just put one measely line. This is simulated here #by drawing several lines on top of each other, like scratching. for i in range(random(1,2)): line(x, y+random(pt*0.1), x+width+random(pt), y+random(pt*0.1)) #Little construction banners to top off #that really you-just-gotta-have Tom handwriting! if random(100)>94: _ctx.scale(random(0.9,1.1)) under_construction = "L" _ctx.text(under_construction, x+width-1.5*pt, y-pt*0.2) #Return the vertical offset of the cursor. return y
def genFor(ctx): if ctx._indent >= 2: return "" s = ctx.spaces() + """for i in range(%s):\n""" % nr(ctx, LOOP) ctx.indent() for i in range(random(5)): s += genStatement(ctx) s += genVisual(ctx) ctx.dedent() return s
def genFor(ctx): if ctx._indent >= 2: return "" s = ctx.spaces() + """for i in range(%s):\n""" % nr(ctx,LOOP) ctx.indent() for i in range(random(5)): s += genStatement(ctx) s += genVisual(ctx) ctx.dedent() return s
def genProgram(): s = """# This code is generated with OTTOBOT, # the automatic PlotDevice code generator. size(%s, %s) translate(%s, %s) colormode(HSB) """ % (COMP_WIDTH, COMP_HEIGHT, COMP_WIDTH / 2, COMP_HEIGHT / 2) ctx = Context() for i in range(random(10, 20)): s += genStatement(ctx) return s
def genGrid(ctx): if ctx.inGrid(): return "" s = ctx.spaces() + """for x, y in grid(%s,%s,%s,%s):\n""" % (nr(ctx,GRIDCOUNT), nr(ctx,GRIDCOUNT), nr(ctx,GRIDWIDTH), nr(ctx,GRIDHEIGHT)) ctx.indent() ctx._grid = True for i in range(random(5)): s += genStatement(ctx) s += genVisual(ctx) ctx.dedent() ctx._grid = False return s
def genProgram(): s = """# This code is generated with OTTOBOT, # the automatic PlotDevice code generator. size(%s, %s) translate(%s, %s) colormode(HSB) """ % (COMP_WIDTH, COMP_HEIGHT, COMP_WIDTH/2, COMP_HEIGHT/2) ctx = Context() for i in range(random(10,20)): s += genStatement(ctx) return s
def line(x1, y1, x2, y2): """Draws a pixie line from coordinates (x1,y1) to (x2,y2) """ _ctx.push() #Set color to pixiecolor(), varying the K value #to simulate pen pressure. _ctx.colormode(CMYK) cc, mc, yc, kc = COLOR _ctx.fill(cc, mc, yc, kc+random(-0.2,0.2)) #Calculate the length of the line as c. from math import sqrt, pow, asin, degrees a = x2 - x1 b = y2 - y1 c = sqrt(pow(a,2) + pow(b,2)) #Choose line glyphs, according to the size of c. #This ensures that lines of different lengths #have more or less a same thickness after scaling them. pt = 30 _ctx.font("Pixie-Dingbats", pt) if c < 150: glyphs = ("S","T") glyphwidth = 1.5*pt elif c > 400: glyphs = ("U","V","W","X") glyphwidth = 10.0*pt else: glyphs = ("M", "N", "O") glyphwidth = 5.5*pt #Determine the line's angle. d = degrees(asin(b/(c+0.0001))) if x2<x1: d = 180 - d #Scale the glyph to the length of the line. _ctx.transform(CENTER) f = c/glyphwidth+0.0001 _ctx.scale(f,f) #Rotate the glyph to the line's angle. _ctx.transform(CORNER) _ctx.translate(x1/f,y1/f) _ctx.rotate(-d) #Draw the line glyph. _ctx.text(choice(glyphs), 0, 0) _ctx.pop()
def genGrid(ctx): if ctx.inGrid(): return "" s = ctx.spaces() + """for x, y in grid(%s,%s,%s,%s):\n""" % (nr( ctx, GRIDCOUNT), nr(ctx, GRIDCOUNT), nr( ctx, GRIDWIDTH), nr(ctx, GRIDHEIGHT)) ctx.indent() ctx._grid = True for i in range(random(5)): s += genStatement(ctx) s += genVisual(ctx) ctx.dedent() ctx._grid = False return s
def height(txt, width, pt=20): """Returns the height of paragraph() with txt, width and pt. This command gives "some" idea of the height, with an average deviation of 20%. """ dx = 0 dy = 0 for i in range(len(txt)): dx += random(pt*0.3, pt*0.5) dy += random(-pt*0.05, pt*0.05) if dx > width and txt[i] in (" ", ".", ","): dx = random(-pt*0.1, pt*0.2) dy += random(pt*0.7, pt*0.9) * SPACING dy += random(pt*0.7, pt*0.9) * SPACING return dy
def tornado(str, x, y): """Experimental tornade-style pixie text. Text that whirls around like a gust of wind. Provided as-is for now. """ from math import sin, cos, tan, log10 cX = random(1,10) cY = random(1,10) for i in range(len(str)): s = cos(cX)*20 x += cos(cY)*s*1.2 y += log10(cX)*1.2 + sin(cX) * 8 _ctx.push() paragraph(str[i], x-s/2, y-s/2, 100, pt=max(15,abs(s*1.5))) _ctx.pop() cX += random(0.45) cY += random(0.15)
def tree(root, nodes, x, y, width, height, pt=20, max=10, grow=False, border=False): """Draws a tree network scheme. Draws a tree scheme exploding from a central root. The nodes list is expected to contain, for example: [ ("node1",["leaf1a","leaf1b"]), ("node2",["leaf2a"]) ]. Branches connect the nodes, and each node has its leaves drawn around it. Nodes grow smaller and smaller if grow is True. Uses line() and node() as callback to draw the network. """ _ctx.push() #The number of nodes to draw count = min(max, len(nodes)) #Create a path on which nodes can #placed later on. path = [(x,y)] _ctx.beginpath(x,y) x0 = x y0 = y for i in range(count): xradius = width/count*(i+0.1) yradius = height/count*(i+0.1) #A random location. #These "grow" further and further away #from the centre #dx = x+random(-xradius,xradius)/2 #dy = y+random(-yradius,yradius)/2 dx = x + xradius * random(0.25,0.5) * choice((-1,1)) dy = y + yradius * random(0.25,0.5) * choice((-1,1)) line(x0, y0, dx, dy) path.append((dx,dy)) x0 = dx y0 = dy for x in range(count): #Get coordinates on path. dx = path[x+1][0] dy = path[x+1][1] #For each node, get its leaves. nodename, leaves = nodes[x] #Draw the leaves of a node around an oval. #The maximum of leaves is limited, #if you draw more the tree starts to look very unhumanlike. #I don't think you would draw trees with, say ten or twenty #leaves by hand. limit = 3 angle = 15 for i in range(min(limit,len(leaves))): w = random(-width/16,width/16) h = random(-width/16,width/16) line(dx, dy, dx+w, dy+h) paragraph(leaves[i], dx+w-30, dy+h+pt/4, width/5, pt*0.65) #Draw the node oval. #Oval grow smaller and smaller if grow was set to True. if grow: size = 1.0 - 0.5 * x/count else: size = 1 node(dx, dy, pt*size) #Draw the example text on the oval. #Delay the first node, #we'll put that one on top later. if random(100)>95: keywords(nodename, all=choice((True,False))) paragraph(nodename, dx+pt/3, dy-pt/3, width/2, pt) #Draw the first central example text. dx = path[0][0] dy = path[0][1] node(dx, dy, pt) paragraph(root, dx+pt/3, dy-pt/3, width/2) #Draw a border around the diagram: if border: dx -= width/2 dy -= height/2 line(dx-pt/2, dy, dx+width-pt/2, dy) line(dx+width, dy-pt/2, dx+width-random(pt), dy+height-pt/2) line(dx+width, dy+height, dx, dy+height-random(pt)) line(dx, dy+height, dx, dy) _ctx.pop()
def nr(ctx, numberclass): if not ctx.inGrid() and random() > 0.5: return "random(%s)" % nrReally(ctx, numberclass) else: return "%s" % nrReally(ctx, numberclass)
def nrReally(ctx, numberclass): if numberclass == XCOORD: if ctx.inGrid(): #return "x" return "x + %s" % nr(ctx,GRIDDELTA) else: return random(-COMP_WIDTH/2,COMP_WIDTH/2) elif numberclass == YCOORD: if ctx.inGrid(): #return "y" return "y + %s" % nr(ctx,GRIDDELTA) else: return random(-COMP_HEIGHT/2,COMP_HEIGHT/2) elif numberclass == XSIZE: return random(0,COMP_WIDTH) elif numberclass == YSIZE: return random(0,COMP_HEIGHT) elif numberclass == ROTATION: return random(0,360) elif numberclass == SCALE: return random(0.5,1.5) elif numberclass == CONTROLPOINT: return random(-100,100) elif numberclass == COLOR: return random() elif numberclass == STROKEWIDTH: return random(1,20) elif numberclass == LOOP: return random(2, 20) elif numberclass == GRIDDELTA: return random(-100,100) elif numberclass == GRIDCOUNT: return random(2, 10) elif numberclass == GRIDWIDTH: return 20 return random(1,100) elif numberclass == GRIDHEIGHT: return 20 return random(1, 100) elif numberclass == SKEW: return random(1,80) elif numberclass == STARPOINTS: return random(2,100)
def paragraph(txt, x, y, width, pt=20, slant=0.5, line=False, serif=False): """Draws a paragraph of Tom's handwriting. Draws the string txt in Tom's handwriting, positioned at x, y with the given width, font size pt. The slant argument controls the speed at which Tom writes. The lineheight setting of spacing() is taken into account, and words supplied to keywords() are framed in a border. The text is drawn in color(). The serif parameter defines serif characters to be used in the paragraph. """ #Ensure that all pixiekeywords() are found, #even in a single word pixie text. txt += " " keywords_done = [] keyword_end = -1 _ctx.transform(CENTER) dx = x dy = y for i in range(len(txt)): _ctx.push() #There is a world of difference between handwritten glyphs in a font, #and handwriting. Handwriting doesn't stay on a straight line, #two characters never look identical, the pen presses down harder #now and then. The same is simulated with scale, skew and rotate. _ctx.scale(random(0.9, 1.1), random(0.9, 1.1)) _ctx.skew(slant*10 + random(8)) _ctx.rotate(slant*-2) #Set color to pixiecolor(), varying the K value #to simulate pen pressure. _ctx.colormode(CMYK) c, m, y, k = COLOR _ctx.fill(c, m, y, k+random(-0.2,0.2)) #Draw the current character in txt in the given fontsize. #Use a bold font for text in a border (see below). fonts = ("Pixie","Pixie-SemiBold") if serif: fonts += ("Pixie-Light",) _ctx.font(choice(fonts), pt) if i <= keyword_end: _ctx.font("Pixie-Bold", pt) try: _ctx.text(txt[i].upper(), dx, dy+random(slant*pt*0.1)) except: pass _ctx.pop() #Traverse the list of keywords, #if we are at the beginning of one of those words, #set a x-coordinate flag. for keyword in KEYWORDS: j = i+len(keyword) #No need to continue if only the first encounter of a keyword #needs to be processed. if KEYWORDS_ALL == False and keyword in keywords_done: pass elif txt[i:j].lower() == keyword.lower(): keywords_done.append(keyword) keyword_x = dx keyword_end = j #When the end of that word is reached, #we know its width and can draw a border around it. if i == keyword_end: border(keyword_x, dy, dx-keyword_x, pt, slant) #Advance the cursor to the next character in txt. dx += random(pt*0.3, pt*0.5) dy += random(-pt*0.05, pt*0.05) #Advance to a new line if this line exceeds the width, #and is at the end of a word. #The spacing() lineheight is taken into account. if txt[i] == "\n" or (dx-x > width and txt[i] in (" ", ".", ",")): dx = x + random(-pt*0.1, pt*0.2) dy += random(pt*0.7, pt*0.9) * SPACING #Before drawing a nice new word, it may be possible #that a small error is made, after all, if we write #a text by hand some thing will have to be corrected as well. if txt[i] in (" ", ".", ",") and random()<DISTRACTION/2.0: dx += mistake(txt[i:i+random(3,5)], dx, dy, pt, slant) if line: #Draw a line underneath the paragraph of text. dy = underline(x, dy, width, pt) #Return the offset of the cursor. dy += (random(pt*0.7, pt*0.9) * SPACING) * 0.75 return (dx,dy)
def dada(query, foreground=None, background=None, fonts=[], transparent=False): # Create some lines of poetry based on the query. h = en.noun.hyponyms(query) h = choice(en.wordnet.flatten(h)) lines = verse(h) lines = lines.split("\n") # Setup the colors and fonts. if foreground == None: foreground = _ctx.color(1,1,1) if background == None: background = _ctx.color(1,0,0) if len(fonts) == 0: fonts = [_ctx.font()] f = _ctx.fontsize() _ctx.background(background) if transparent: _ctx.background(None) _ctx.lineheight(1) _ctx.fill(foreground) _ctx.stroke(foreground) _ctx.strokewidth(0.5) # Poem title. _ctx.text(query, _ctx.WIDTH/15, _ctx.HEIGHT/7-f) for i in range(1): _ctx.fill(foreground) x = _ctx.WIDTH / 15 y = _ctx.HEIGHT / 7 for words in lines: for word in words.split(" "): # For each word in a line, # pick a random font from the list and a random fontsize. _ctx.font(choice(fonts)) if random() > 0.7: _ctx.fontsize(random(f*0.6, f*1.2)) # A word that is s # l # a # n # t # e # d. # The text continues on the next line. if random() > 0.9: _ctx.rotate(-45 * random(1, 2)) _ctx.text(word+" ", x, y+_ctx.textwidth(word)/2) y += _ctx.textwidth(word) * 1.5 _ctx.reset() # ...or we continue on this line as normal: else: # The word is sometimes printed DESREVNI: # e.g red text in white box instead of white text on red. # Some wiggling occurs. if random() > 0.85: r = random(50) if random() > 0.8: _ctx.oval(x, y, r, r) _ctx.rotate(random(-3, 3)) _ctx.nostroke() _ctx.rect( x-2, y-_ctx.textheight(word), _ctx.textwidth(word)+4, _ctx.textheight(word) ) _ctx.fill(background) _ctx.text(word+" ", x, y) _ctx.fill(foreground) # Otherwise, just print out the word. else: _ctx.text(word+" ", x, y) # Word is repeated for poetic stress effect. # repeated if random() > 0.99: _ctx.text(word+" ", x, y+_ctx.textheight(word)) # Add a line for visual effect,. if random() > 0.9: d = random(100) _ctx.stroke(foreground) _ctx.strokewidth(0.5) _ctx.line(x+_ctx.textwidth(word), y, x+_ctx.textwidth(word)+d, y) x += d # Some play with indentation. # Now where did I leave that oval? x += _ctx.textwidth(word+" ") if x > _ctx.WIDTH * 0.65: x = _ctx.WIDTH / 15 y += _ctx.textheight(word) x = _ctx.WIDTH / 15 y += _ctx.textheight(word)
def sprite(x, y, pt=40): """Draws an imbecile. Draws a doodle sprite at (x,y), varying legs, faces, bodies, and more. """ _ctx.transform(CENTER) #Set color to pixiecolor(), varying the K value #to simulate pen pressure. _ctx.colormode(CMYK) cc, mc, yc, kc = COLOR _ctx.fill(cc, mc, yc, kc+random(-0.2,0.2)) #Somewhere in the Pixie-Dingbats font are arms, legs, ... body = choice(("a","b","c","j","k","l","t","u")) face = choice(("d","e","f","m","n","o","v","w")) legs = choice(("g","h","i","p","q","r","x","y")) balloons = choice(("s","z")) _ctx.fill(cc, mc, yc, kc+random(-0.2,0.2)) #Draw a body. _ctx.rotate(random(-20,20)) _ctx.skew(random(-20,20),random(-20,20)) _ctx.font("Pixie-Dingbats", pt * random(0.8,1.4)) _ctx.text(body, x, y) _ctx.reset() _ctx.fill(cc, mc, yc, kc+random(-0.2,0.2)) #Draw a face. _ctx.rotate(random(-20,20)) _ctx.skew(random(-20,20),random(-20,20)) _ctx.font("Pixie-Dingbats", pt * random(0.8,1.4)) _ctx.text(face, x, y) _ctx.reset() _ctx.fill(cc, mc, yc, kc+random(-0.2,0.2)) #Draw legs. _ctx.rotate(random(-20,20)) _ctx.font("Pixie-Dingbats", pt * random(0.9,1.5)) _ctx.text(legs, x, y) _ctx.reset() if random(100)>90: _ctx.fill(cc, mc, yc, kc+random(-0.2,0.2)) #Draw balloon text. if random(100)>90: _ctx.rotate(random(-20,20)) _ctx.font("Pixie-Dingbats", pt * random(0.9,1.5)) _ctx.text(balloons, x, y) _ctx.reset()
def nrReally(ctx, numberclass): if numberclass == XCOORD: if ctx.inGrid(): #return "x" return "x + %s" % nr(ctx, GRIDDELTA) else: return random(-COMP_WIDTH / 2, COMP_WIDTH / 2) elif numberclass == YCOORD: if ctx.inGrid(): #return "y" return "y + %s" % nr(ctx, GRIDDELTA) else: return random(-COMP_HEIGHT / 2, COMP_HEIGHT / 2) elif numberclass == XSIZE: return random(0, COMP_WIDTH) elif numberclass == YSIZE: return random(0, COMP_HEIGHT) elif numberclass == ROTATION: return random(0, 360) elif numberclass == SCALE: return random(0.5, 1.5) elif numberclass == CONTROLPOINT: return random(-100, 100) elif numberclass == COLOR: return random() elif numberclass == STROKEWIDTH: return random(1, 20) elif numberclass == LOOP: return random(2, 20) elif numberclass == GRIDDELTA: return random(-100, 100) elif numberclass == GRIDCOUNT: return random(2, 10) elif numberclass == GRIDWIDTH: return 20 return random(1, 100) elif numberclass == GRIDHEIGHT: return 20 return random(1, 100) elif numberclass == SKEW: return random(1, 80) elif numberclass == STARPOINTS: return random(2, 100)
def update(self, shuffled=True, cohesion=100, separation=10, alignment=5, goal=20, limit=30): """ Calculates the next motion frame for the flock. """ # Shuffling the list of boids ensures fluid movement. # If you need the boids to retain their position in the list # each update, set the shuffled parameter to False. from random import shuffle if shuffled: shuffle(self) m1 = 1.0 # cohesion m2 = 1.0 # separation m3 = 1.0 # alignment m4 = 1.0 # goal # The flock scatters randomly with a Boids.scatter chance. # This means their cohesion (m1) is reversed, # and their joint alignment (m3) is dimished, # causing boids to oscillate in confusion. # Setting Boids.scatter(chance=0) ensures they never scatter. if not self.scattered and random() < self._scatter: self.scattered = True if self.scattered: m1 = -m1 m3 *= 0.25 self._scatter_i += 1 if self._scatter_i >= self._scatter_t: self.scattered = False self._scatter_i = 0 # A flock can have a goal defined with Boids.goal(x,y,z), # a place of interest to flock around. if not self.has_goal: m4 = 0 if self.flee: m4 = -m4 for b in self: # A boid that is perching will continue to do so # until Boid._perch_t reaches zero. if b.is_perching: if b._perch_t > 0: b._perch_t -= 1 continue else: b.is_perching = False vx1, vy1, vz1 = b.cohesion(cohesion) vx2, vy2, vz2 = b.separation(separation) vx3, vy3, vz3 = b.alignment(alignment) vx4, vy4, vz4 = b.goal(self._gx, self._gy, self._gz, goal) b.vx += m1*vx1 + m2*vx2 + m3*vx3 + m4*vx4 b.vy += m1*vy1 + m2*vy2 + m3*vy3 + m4*vy4 b.vz += m1*vz1 + m2*vz2 + m3*vz3 + m4*vz4 b.limit(limit) b.x += b.vx b.y += b.vy b.z += b.vz self.constrain()