Emergence is exemplified when patterns arise out of a multiplicity of relatively simple interactions. In this example, we’re again using Perlin noise to set the rotation of some graphical elements, in this case lines. By adjusting the length of the line and the resolution of the Perlin noise, we can accomplish a variety of effects. Move the mouse left and right to adjust the length of the lines; move the mouse up and down to control the resolution of the Perlin noise.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
float cellSize = 15; Node[] nodes; int xRes, yRes; boolean showsFPS = false; PFont font; float lineSize = 10; float perlinRes; float mx, my; void setup() { size(940, 540); colorMode(HSB, 100); background(0); font = createFont("Helvetica", 24); createNodes(); } void draw() { background(0); noFill(); stroke(100, 20); mx = mouseX; my = mouseY; if (mx == 0) mx = width/2; if (my == 0) my = height/2; lineSize = mx + 20; perlinRes = my/100000 + 0.003; updateNodes(); if (showsFPS) displayFPS(); } void createNodes() { xRes = ceil(width/cellSize) + 2; yRes = ceil(height/cellSize) + 2; nodes = new Node[(xRes)*(yRes)]; for (int i=0; i<yRes; i++) { for (int j=0; j<xRes; j++) { float cx = j*cellSize + cellSize/2; float cy = i*cellSize + cellSize/2; cx -= cellSize; cy -= cellSize; Node node = new Node(cx, cy); nodes[i*xRes+j] = node; } } } void updateNodes() { float noiseScale = 0.0053; noiseScale = perlinRes; float noiseSpeed = 0.001; float a; PVector vector; for (int i=0; i<yRes; i++) { for (int j=0; j<xRes; j++) { Node node = nodes[i*xRes+j]; a = noise(i*noiseScale, j*noiseScale, frameCount*noiseSpeed)*TWO_PI*4; vector = new PVector(cos(a), sin(a)); node.vector = vector; node.update(); } } } void displayFPS() { textFont(font, 18); fill(100); String output = "fps="; output += (int) frameRate; text(output, 10, 30); } void keyPressed() { if (key == 'f') showsFPS = !showsFPS; } boolean sketchFullScreen() { return false; } class Node { float x, y, vx, vy; PVector vector; Node(float _x, float _y) { x = _x; y = _y; vx = 0.0; vy = 0.0; } void update() { vector.mult(lineSize); line(x, y, x+vector.x, y+vector.y); } } |


