def draw(): global m ctx = Canvas(width=500, height=500, gtk_draw=True) wn = 500.0 / (grid_size - 1) hn = 500.0 / (grid_size - 1) #bg = ctx.radial_gradient() a.solve() shapes = shape() length = int(sqrt(len(a.vectors()[0]))) U = a.vectors()[0] V = a.vectors()[1] colors = a.rgb() s = shape() p = path() for i in xrange(length): for j in xrange(length): idx = (j * length) + i start_x = (i * wn) start_y = (j * hn) #r = s.rectangle(start_x, start_y, wn, hn) #color = colors[idx] #r.stroke_color = tuple(color) #r.stroke_width = 0.25 #ctx.add(r) p.moveto(start_x, start_y) p.rellineto(U[idx]*1000.0, V[idx]*1000.0) p.stroke_color = (0.0, 0.0, 0.0) p.stroke_width = 0.25 ctx.add(p) if (m%100) == 0: a.add_force(random.randint(0, grid_size)/1000.0, random.randint(0, grid_size)/1000.0, random.randint(0, grid_size)/1000.0, random.randint(0, grid_size)/1000.0, #random.random(), #random.random() ) m += 1 #ctx.draw() return ctx
def draw(): ctx = Canvas(width=500, height=500, gtk_draw=True) bg = ctx.background() bg.fill_color = 0.2 ctx.add(bg) ## Update each flock. global flocks for flock in flocks: flock.update(goal=60) ## Draw a grey arrow for each boid in a block. ## Radius and opacity depend on the boids z-position. for boid in flock: r = 10 + boid.z * 0.25 alpha = 0.5 + boid.z*0.01 s = shape() arrow = s.arrow(boid.x-r/2, boid.y-r/2, r) arrow.fill_color = (0.6, 0.6, 0.6, alpha) arrow.rotate(-boid.angle) ctx.add(arrow) ctx.draw() return ctx.gtk()
def draw(): global colony ctx = Canvas(width=500, height=500, gtk_draw=True) bg = ctx.background() bg.fill_color = 0.2 ctx.add(bg) ## Draw the hoarded food in the colony. shapes = shape() s = colony.food oval = shapes.oval(colony.x-s/2, colony.y-s/2, s, s) oval.fill_color = (0.3) ctx.add(oval) ## Draw each foodsource in green. ## Watch it shrink as the ants eat away its size parameter! for f in colony.foodsources: oval = shapes.oval(f.x-f.size/2, f.y-f.size/2, f.size, f.size) oval.fill_color = (0.6, 0.8, 0, 0.1) ctx.add(oval) for ant in colony: p = path() ## Draw the pheromone trail for each ant. ## Ants leave a trail of scent from the foodsource, ## enabling other ants to find the food as well! if len(ant.trail) > 0: p.moveto(ant.trail[0].x, ant.trail[0].y) for k in ant.trail: p.lineto(k.x, k.y) p.stroke_color = (0.8, 0.8, 0.8, 1.0) p.stroke_width = (0.5) ctx.add(p) ## Change ant color when carrying food. ## The main ant behaviour: ## 1) follow an encountered trail, ## 2) harvest nearby food source, ## 3) bring food back to colony, ## 4) wander aimlessly ant.forage() oval = shapes.oval(ant.x, ant.y, 3, 3) if ant.has_food: oval.fill_color = (0.6, 0.8, 0) else: oval.fill_color = (0.8, 0.8, 0.8, 0.5) ctx.add(oval) ctx.draw() return ctx.gtk()
def radial_gradient( self, colors=[Color(0.05, 0.06, 0.0), Color(0.18, 0.23, 0.28, 1.00)], x=-150, y=-150, radius=900, steps=200 ): def _step(colors, i, n): l = len(colors) - 1 a = int(1.0 * i / n * l) a = min(a + 0, l) b = min(a + 1, l) base = 1.0 * n / l * a d = (i - base) / (n / l) r = colors[a].r * (1 - d) + colors[b].r * d g = colors[a].g * (1 - d) + colors[b].g * d b = colors[a].b * (1 - d) + colors[b].b * d return Color(r, g, b) for i in range(steps): Shape = shape() oval = Shape.oval(x + i, y + i, radius - i * 2, radius - i * 2) oval.fill_color = _step(colors, i, steps) self.add(oval)
def background(self): s = shape() rect = s.rectangle(0, 0, self.width, self.height) return rect