def draw(target: cairo.ImageSurface, t: float): ctx = cairo.Context(target) ctx.set_line_width(0.5) ctx.set_source_rgba(0.92, 0.82, 1, 0.9) ctx.translate(10, 40) ctx.scale(3, 3) #path = Line(10+10j, 0.8*target.get_width() + 0.8*target.get_height()*1j) n = 1024 for path in paths: ws = np.linspace(0, 1, n) ops = [path.point(w) for w in ws] pr = 1.5 px = np.array([as_tuple(cmath.rect(pr, 4*w*TAU + 6*t) + pr*(1 + 1j)) for w in ws]) py = np.array([as_tuple(cmath.rect(pr, 4*w*TAU + 6*t) + pr*(2 + 2j)) for w in ws]) dpxs = perlin.noise(px[:, 0][:, None], px[:, 1][:, None], size=64, seed=6) - 0.5 dpys = perlin.noise(py[:, 0][:, None], py[:, 1][:, None], size=64, seed=6) - 0.5 low = 1 prev = 0 for w, op, dpx, dpy in zip(ws, ops, dpxs, dpys): #intensity = 5*np.sin(w*TAU) intensity = np.clip(9 * np.sin(w*TAU - t), low, 100) - low dp = complex(dpx, dpy) * intensity pp = op + dp if path.lift(prev, w): ctx.move_to(*as_tuple(pp)) else: ctx.line_to(*as_tuple(pp)) prev = w ctx.stroke()
def getcontours(IM,sc=2): print("generating contours...") IM = find_edges(IM) IM1 = IM.copy() IM2 = IM.rotate(-90,expand=True).transpose(Image.FLIP_LEFT_RIGHT) dots1 = getdots(IM1) contours1 = connectdots(dots1) dots2 = getdots(IM2) contours2 = connectdots(dots2) for i in range(len(contours2)): contours2[i] = [(c[1],c[0]) for c in contours2[i]] contours = contours1+contours2 for i in range(len(contours)): for j in range(len(contours)): if len(contours[i]) > 0 and len(contours[j])>0: if distsum(contours[j][0],contours[i][-1]) < 8: contours[i] = contours[i]+contours[j] contours[j] = [] for i in range(len(contours)): contours[i] = [contours[i][j] for j in range(0,len(contours[i]),8)] contours = [c for c in contours if len(c) > 1] for i in range(0,len(contours)): contours[i] = [(v[0]*sc,v[1]*sc) for v in contours[i]] for i in range(0,len(contours)): for j in range(0,len(contours[i])): contours[i][j] = int(contours[i][j][0]+10*perlin.noise(i*0.5,j*0.1,1)),int(contours[i][j][1]+10*perlin.noise(i*0.5,j*0.1,2)) return contours
def hatch(IM, sc=16, noise=False): print("hatching...") PX = IM.load() w, h = IM.size lg1 = [] lg2 = [] for x0 in range(w): for y0 in range(h): x = x0 * sc y = y0 * sc if PX[x0, y0] > 144: pass elif PX[x0, y0] > 64: lg1.append([(x, y + sc / 4), (x + sc, y + sc / 4)]) elif PX[x0, y0] > 16: lg1.append([(x, y + sc / 4), (x + sc, y + sc / 4)]) lg2.append([(x + sc, y), (x, y + sc)]) else: lg1.append([(x, y + sc / 4), (x + sc, y + sc / 4)]) lg1.append([(x, y + sc / 2 + sc / 4), (x + sc, y + sc / 2 + sc / 4)]) lg2.append([(x + sc, y), (x, y + sc)]) lines = [lg1, lg2] for k in range(0, len(lines)): for i in range(0, len(lines[k])): for j in range(0, len(lines[k])): if lines[k][i] != [] and lines[k][j] != []: if lines[k][i][-1] == lines[k][j][0]: lines[k][i] = lines[k][i] + lines[k][j][1:] lines[k][j] = [] lines[k] = [l for l in lines[k] if len(l) > 0] lines = lines[0] + lines[1] if noise: for i in range(0, len(lines)): for j in range(0, len(lines[i])): lines[i][j] = int(lines[i][j][0] + sc * perlin.noise(i * 0.5, j * 0.1, 1) ), int(lines[i][j][1] + sc * perlin.noise(i * 0.5, j * 0.1, 2)) - j return lines
def generateRandomData(self): sigma = 10 if (self._angle == 0): self._noise = perlin.noise(400, 50, 50) mu = 100 + int(self._noise[self._angle]) + random.randint(-1, 1) self._data = "".join([ chr( int(255 * np.exp(-np.power(x - mu, 2.) / (2 * np.power(sigma, 2.))))) for x in range(self._number_of_samples) ])
def __init__(self, port='COM1', baudrate=115200, timeout=1, bytesize=8, parity='N', stopbits=1, xonxoff=0, rtscts=0): self.name = port self.port = port self.timeout = timeout self.parity = parity self.baudrate = baudrate self.bytesize = bytesize self.stopbits = stopbits self.xonxoff = xonxoff self.rtscts = rtscts self._isOpen = True self._receivedData = "" self.in_waiting = 1 self.parser = PingParser( ) # used to parse incoming client comunications self._gain_setting = 0 self._mode = 0 self._angle = 0 self._transmit_duration = 0 self._sample_period = 0 self._transmit_frequency = 100 self._number_of_samples = 10 self._data = "".join([chr(0) for _ in xrange(self._number_of_samples)]) self._data_length = 10 self._noise = perlin.noise(400, 50, 50)
def update(self): self.xoff += 0.1 self.noise = perlin.noise(self.xoff) self.lines.insert(0, self.pos) if len(self.lines) > self.trail_length + 30 * self.noise: self.lines.pop() ax, ay = self.acc vx, vy = self.vel px, py = self.pos vx += ax vy += ay self.vel = (vx, vy) if get_distance((0, 0), self.vel) > self.maxvel + 2 * self.noise: self.vel = lendir((0, 0), get_direction((0, 0), self.vel), self.maxvel) vx, vy = self.vel px += vx py += vy if px < 0: px = width if py < 0: py = height if px > width: px = 0 if py > height: py = 0 self.pos = (px, py) self.acc = (0, 0)
def field_vector_at(x, y, rot): radian = 2.0*math.pi*rot xv = perlin.noise(x*scale, y*scale, radius*math.cos(radian)+offset, radius*math.sin(radian)) yv = perlin.noise(x*scale, y*scale + 128, radius*math.cos(radian)+offset, radius*math.sin(radian)) return round(xv, 4), round(yv, 4)
############ Land size width = 150 # map width length = 100 # map length ############ Noise variables n1div = 30 # landmass distribution n2div = 4 # boulder distribution n3div = 1 # rock distribution n1scale = 20 # landmass height n2scale = 2 # boulder scale n3scale = 0.5 # rock scale noise1 = perlin.noise(width / n1div, length / n1div) # landmass / mountains noise2 = perlin.noise(width / n2div, length / n2div) # boulders noise3 = perlin.noise(width / n3div, length / n3div) # rocks ############ z modifiers zroot = 2 zpower = 2.5 ############ colors colors = {0: 'blue', 1: 'yellow', 20: 'green', 25: 'gray', 1000: 'white'} ############ 3D shapes points = []
screen = pygame.display.set_mode(size) running = True zoff = 0 fish = [] for i in range(0, len(colors)): fish.append(Fish()) fish[i].c = colors[i] while running: screen.fill(colors[0]) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False yoff = 0 for y in range(1, width, scale): xoff = 0 for x in range(1, height, scale): i = x / scale + (y / scale) * col c = int(len(colors) * perlin.noise(xoff, yoff, zoff)) direction = (2 * 360) * perlin.noise(xoff, yoff, zoff) grid[i] = direction xoff += inc yoff += inc zoff += inc / 10 for f in fish: f.update() f.swim(grid) f.draw() pygame.display.flip()