// tentacle_4 (2005. 1. 24.) // by SeungJoon Choi // erucipe@hanmail.net // // http://processing.org BGraphics buffer; Tentacle[] tentacles; int n_tentacles; void setup() { size(300, 300); buffer = new BGraphics(width, height); buffer.format = RGB; buffer.background(0); buffer.noFill(); buffer.ellipseMode(CENTER_RADIUS); n_tentacles = 4; tentacles = new Tentacle[n_tentacles]; for(int i = 0; i < n_tentacles; i++) { tentacles[i] = new Tentacle(40, 5); } } void loop() { //buffer.background(0); Tentacle tentacle = tentacles[0]; tentacle.root_fix = true; tentacle.leaf_fix = true; tentacle.Fix(0, 0, mouseX, mouseY); tentacle.Draw(buffer, 10); tentacle = tentacles[1]; tentacle.root_fix = true; tentacle.leaf_fix = true; tentacle.Fix(width, 0, mouseX, mouseY); tentacle.Draw(buffer, 10); tentacle = tentacles[2]; tentacle.root_fix = true; tentacle.leaf_fix = true; tentacle.Fix(width, height, mouseX, mouseY); tentacle.Draw(buffer, 10); tentacle = tentacles[3]; tentacle.root_fix = true; tentacle.leaf_fix = true; tentacle.Fix(0, height, mouseX, mouseY); tentacle.Draw(buffer, 10); image(buffer, 0, 0); blur(buffer); } int Red(int c) { return (c & 0xff0000) >> 16; } int Green(int c) { return (c & 0x00ff00) >> 8; } int Blue(int c) { return c & 0x0000ff; } void blur(BGraphics target) { int index,left,right,top,bottom; int sumr, sumg, sumb; for(int j=0;j0) left=-1; else left=width-1; if(j==(width-1)) right=-width+1; else right=1; if(i>0) top=-width; else top=(height-1)*width; if(i==(height-1)) bottom=-(height-1)*width; else bottom=width; // Calculate the sum of n neighbors sumr=Red(target.pixels[left+index]); // left middle sumg=Green(target.pixels[left+index]); sumb=Blue(target.pixels[left+index]); sumr+=Red(target.pixels[right+index]); // right middle sumg+=Green(target.pixels[right+index]); sumb+=Blue(target.pixels[right+index]); sumr+=Red(target.pixels[index]); // middle middle sumg+=Green(target.pixels[index]); sumb+=Blue(target.pixels[index]); sumr+=Red(target.pixels[index+top]); // middle top sumg+=Green(target.pixels[index+top]); sumb+=Blue(target.pixels[index+top]); sumr+=Red(target.pixels[index+bottom]); // middle bottom sumg+=Green(target.pixels[index+bottom]); sumb+=Blue(target.pixels[index+bottom]); sumr=(int)((float)sumr/4.96); sumg=(int)((float)sumg/5); sumb=(int)((float)sumb/4.99); target.pixels[index]=(sumr<<16)+(sumg<<8)+sumb; } } } class Node { Node(float _x, float _y, float _length) { x = _x; y = _y; px = x; py = y; vx = 0; vy = 0; dt = 0.1; length = _length; decay = 0.9; Nodes = new Vector(); } void Interaction() { int n = Nodes.size(); for(int i = 0; i < n; i++) { Node node = (Node)Nodes.elementAt(i); float dx = x - node.x; float dy = y - node.y; theta = atan2(dy, dx); float d = sqrt(dx*dx + dy*dy); if(d > 0) { float dx_d = dx / d; float dy_d = dy / d; if(d > length) { vx += -dx_d*dt; vy += -dy_d*dt; node.vx += dx_d*dt; node.vy += dy_d*dt; } else { vx += dx_d*dt; vy += dy_d*dt; node.vx += -dx_d*dt; node.vy += -dy_d*dt; } x += vx*dt; y += vy*dt; dx = x - node.x; dy = y - node.y; d = sqrt(dx*dx + dy*dy); dx_d = dx / d; dy_d = dy / d; float k = d - length; float kx = -k*dx_d; float ky = -k*dy_d; x += kx; y += ky; node.x += -kx*0.9; node.y += -ky*0.9; vx *= decay; vy *= decay; node.vx *= decay; node.vy *= decay; } } px = x; py = y; } void Fix(float _x, float _y) { x = _x; y = _y; } void Aim(float _x, float _y) { float dx = x - _x; float dy = y - _y; float d = sqrt(dx*dx+dy*dy); if(d > 0) { vx += -dx/d*dt*10; vy += -dy/d*dt*10; } } void Draw(BGraphics target, float r) { target.vertex(x + r*cos(theta + PI*0.5), y+r*sin(theta + PI*0.5)); target.vertex(x + r*cos(theta - PI*0.5), y+r*sin(theta - PI*0.5)); } float x, y, px, py; float vx, vy, dt, decay; float length , theta; Vector Nodes; } class Tentacle { Tentacle(int n, float seg_l) { Nodes = new Node[n]; for(int i = 0; i < n; i++) { Nodes[i] = new Node(0, seg_l*i, seg_l); if(i > 0) { Nodes[i].Nodes.addElement(Nodes[i-1]); } } root_fix = false; leaf_fix = false; } void Fix(float _ox, float _oy, float _x, float _y) { ox = _ox; oy = _oy; x = _x; y = _y; if(root_fix) Nodes[0].Fix(ox, oy); if(leaf_fix) Nodes[Nodes.length - 1].Fix(x, y); } void Draw(BGraphics target, int opacity) { target.stroke(127, 255, 0, opacity); target.beginShape(TRIANGLE_STRIP); for(int i = 0; i < Nodes.length; i++) { Nodes[i].Interaction(); if(i > 0) { Nodes[i].Draw(target, (Nodes.length - 1)*0.5 - i*0.5); } } target.endShape(); } Node[] Nodes; float ox, oy, x, y; boolean root_fix, leaf_fix; }