def split(self): if self.growth >= 15: cell1 = Cell(linalg.addition(self.pos, linalg.vector((0, 15))), self.vel) cell2 = Cell(linalg.addition(self.pos, linalg.vector((0, -15))), self.vel) self.cells.append(cell1) self.cells.append(cell2) self.cells.remove(self)
def drawControlCurve(self): self.app.canvas.delete("obj") if (self.mode): p0, p1 = point(self.cps[1]), point(self.cps[2]) t0 = vector(self.cps[1], self.cps[0]) t1 = vector(self.cps[3], self.cps[2]) r = 1 #for v in range(5): #r = 0.2*(1-v/5)+1.8*(v/5) c1, c2, c3, a, b = biarc_h(p0, t0, p1, t1, r) cx, rx = circparam(p0, proj(c1), proj(c2)) cy, ry = circparam(proj(c2), proj(c3), p1) cid = self.app.canvas.create_oval(cx[0] - 15, cx[1] - 15, cx[0] + 15, cx[1] + 15, outline="black", fill="green", tags="obj") cid = self.app.canvas.create_oval(cy[0] - 15, cy[1] - 15, cy[0] + 15, cy[1] + 15, outline="black", fill="green", tags="obj") if (c1 == None): return cl = [p0, c1, c2, c3, p1] cas = [] for j in range(0, 11): cas.append((circarc_h(j / 10, [hom(p0, 1), c1, c2]))) for j in range(0, 11): cas.append((circarc_h(j / 10, [c2, c3, hom(p1, 1)]))) #clc = [*x for x in cl] carc = [(x[0], x[1]) for x in cas] #self.app.canvas.create_line(clc,fill="lightblue",tag="obj") self.app.canvas.create_line(carc, fill="darkgreen", tag="obj") else: for i in range(2, len(self.cps)): cas = [] for j in range(0, 11): cas.append((circarc(j / 10, self.cls[i - 2], self.cps[i - 2:i + 1]))) #print(cas) cl = [(x[0], x[1]) for x in self.cps[i - 2:i + 1]] carc = [(x[0], x[1]) for x in cas] self.app.canvas.create_line(cl, fill="lightblue", tag="obj") self.app.canvas.create_line(carc, fill="darkgreen", tag="obj") sys.stdout.flush()
def circarc(t, v, ps): n1, l1 = unit_length(vector(ps[1], ps[0])) n2, l2 = unit_length(vector(ps[1], ps[2])) l = min(l1, l2) p0 = hom(point(ps[1]) - l * v * n1, 1) p2 = hom(point(ps[1]) - l * v * n2, 1) w = dot(n1, unit((p2[0] - p0[0], p2[1] - p0[1]))) p1 = hom(point(ps[1]), w) x = bezier2(t, [p0, p1, p2]) return proj(x)
def collide(self, cell): if self.pos.dist_between( cell.pos) < self.growth or self.pos.dist_between( cell.pos) < cell.growth: neg = cell.pos.scalar_multiple(-1) adjust_vect = linalg.addition(self.pos, neg) adjust_unit = adjust_vect.scalar_multiple(1 / adjust_vect.norm) velocity = adjust_unit.scalar_multiple(self.vel) for i in range(self.pos.rowcount): self.pos.itself[i][0] += velocity.itself[i][0] new_pos = [a[0] for a in self.pos.itself] self.pos = linalg.vector(new_pos)
def drawControlCurve(self): self.app.canvas.delete("obj") if (self.mode): p0, p1 = point(self.cps[1]), point(self.cps[2]) t0 = vector(self.cps[1], self.cps[0]) t1 = vector(self.cps[3], self.cps[2]) r = 1 #for v in range(5): #r = 0.2*(1-v/5)+1.8*(v/5) c1, c2, c3, a, b = biarc_h(p0, t0, p1, t1, r) if (c1 == None): return cl = [p0, c1, c2, c3, p1] cas = [] for j in range(0, 11): cas.append((circarc_h(j / 10, [hom(p0, 1), c1, c2]))) for j in range(0, 11): cas.append((circarc_h(j / 10, [c2, c3, hom(p1, 1)]))) #clc = [self.app.o2c(*x) for x in cl] carc = [self.app.o2c(*x) for x in cas] #self.app.canvas.create_line(clc,fill="lightblue",tag="obj") self.app.canvas.create_line(carc, fill="darkgreen", tag="obj") else: for i in range(2, len(self.cps)): cas = [] for j in range(0, 11): cas.append((circarc(j / 10, self.cls[i - 2], self.cps[i - 2:i + 1]))) #print(cas) cl = [self.app.o2c(*x) for x in self.cps[i - 2:i + 1]] carc = [self.app.o2c(*x) for x in cas] self.app.canvas.create_line(cl, fill="lightblue", tag="obj") self.app.canvas.create_line(carc, fill="darkgreen", tag="obj") sys.stdout.flush()
def move(self, foods): food_dist = [] for food in foods: food_dist.insert(foods.index(food), self.pos.dist_between(food.pos)) sorted_list = food_dist.copy() sorted_list.sort() for i in food_dist: if i == sorted_list[0]: destination = foods[food_dist.index(i)].pos break v = self.pos.scalar_multiple(-1) direction = linalg.addition(destination, v) direction_unit = direction.scalar_multiple(1 / direction.norm) velocity = direction_unit.scalar_multiple(self.vel) for i in range(self.pos.rowcount): self.pos.itself[i][0] += velocity.itself[i][0] new_pos = [a[0] for a in self.pos.itself] self.pos = linalg.vector(new_pos)
def main(): counter = 0 run = True def draw(): WIN.fill((100, 100, 100)) for cell in Cell.cells: cell.draw() for food in Food.foods: food.draw() pygame.display.update() while run: clock.tick(FPS) if len(Food.foods) <= 5: Food.foods.append( Food( linalg.vector((random.randrange(100, WIDTH - 100), random.randrange(100, HEIGHT - 100))))) for cell in Cell.cells: cell.move(Food.foods) for cell1 in Cell.cells: if cell1 != cell: cell.collide(cell1) for food in Food.foods: cell.eat(food) cell.split() if counter % 150 == 0: for cell in Cell.cells: if cell.growth >= 1: cell.growth -= 1 if cell.growth == 0: Cell.cells.remove(cell) counter += 1 draw() for event in pygame.event.get(): if event.type == pygame.QUIT: run = False keys = pygame.key.get_pressed() if keys[pygame.K_SPACE]: purple = [i for i in Cell.cells if i.vel == 1] blue = [i for i in Cell.cells if i.vel == 2] green = [i for i in Cell.cells if i.vel == 3] cyan = [i for i in Cell.cells if i.vel == 4] font = pygame.font.SysFont('freesans', 40, True) label1 = font.render(f'Number of purple cells: {len(purple)}', 1, (255, 255, 255)) label2 = font.render(f'Number of blue cells: {len(blue)}', 1, (255, 255, 255)) label3 = font.render(f'Number of green cells: {len(green)}', 1, (255, 255, 255)) label4 = font.render(f'Number of cyan cells: {len(cyan)}', 1, (255, 255, 255)) for i in range(200): WIN.blit(label1, (300, 200)) WIN.blit(label2, (300, 250)) WIN.blit(label3, (300, 300)) WIN.blit(label4, (300, 350)) pygame.display.update()
class Food: foods = [] def __init__(self, pos): self.pos = pos def draw(self): pygame.draw.circle(WIN, (255, 0, 0), (self.pos.itself[0][0], self.pos.itself[1][0]), 10) for i in range(10): Cell.cells.append( Cell( linalg.vector((random.randrange(100, WIDTH - 100), random.randrange(100, HEIGHT - 100))), random.randrange(1, 5))) def main(): counter = 0 run = True def draw(): WIN.fill((100, 100, 100)) for cell in Cell.cells: cell.draw() for food in Food.foods: food.draw()
def viewpoint(self, viewpoint): ''' Set the camera position in world coordinates ''' self._viewpoint = vector(viewpoint)