// Maya1, 2002 Glen Murphy // Maya1 is viewable at http://bodytag.org/ // This is a straight port to Proce55ing int WIDTH = 700; int HEIGHT = 300; int NUMPARTICLES = 200; int NUMNODES = 4; particle[] p = new particle[NUMPARTICLES]; node[] n = new node[NUMNODES]; class node { public int x, y; public node(int x, int y) { this.x = x; this.y = y; } } class particle { public int targx, targy, aitype, target; public float x, y, lastx, lasty, xvel, yvel; public particle(float x, float y, float xvel, float yvel, int targx, int targy, int target, int aitype) { this.x = x; this.lastx = x; this.y = y; this.lasty = y; this.xvel = xvel; this.yvel = yvel; this.targx = targx; this.targy = targy; this.target = target; this.aitype = aitype; } void updatepos() { lastx = x; lasty = y; int xy = (int)(y*height+x); float xpos = targx-x; float ypos = targy-y; float dd = xpos*xpos + ypos*ypos; if(dd > 100) { xvel += 10*xpos/dd; yvel += 10*ypos/dd; } else { this.target = -1; } x += xvel; y += yvel; } } void drawline(int x0, int y0, int x1, int y1, int pix, int buffer[]) { if(x0 < 0 || x0 >= WIDTH || y0 < 0 || y0 >= HEIGHT || x1 < 0 || x1 >= WIDTH || y1 < 0 || y1 >= HEIGHT) return; int dy = y1 - y0; int dx = x1 - x0; int stepx, stepy; if (dy < 0) { dy = -dy; stepy = -WIDTH; } else { stepy = WIDTH; } if (dx < 0) { dx = -dx; stepx = -1; } else { stepx = 1; } dy <<= 1; dx <<= 1; y0 *= WIDTH; y1 *= WIDTH; if (dx > dy) { int fraction = dy - (dx >> 1); while (x0 != x1) { if (fraction >= 0) { y0 += stepy; fraction -= dx; } x0 += stepx; fraction += dy; modpixel(x0+y0, pix, buffer); } } else { int fraction = dx - (dy >> 1); while (y0 != y1) { if (fraction >= 0) { x0 += stepx; fraction -= dy; } y0 += stepy; fraction += dx; modpixel(x0+y0, pix, buffer); } } } void modpixel(int pos, int col, int buffer[]) { int alp=245; int r1=(buffer[pos])&0x0000ff; int g1=(buffer[pos])&0x00ff00; int b1=(buffer[pos])&0xff0000; int r2=(col)&0x0000ff; int g2=(col)&0x00ff00; int b2=(col)&0xff0000; int r3=(((alp*(r1-r2))>>8)+r2)&0x000000ff; int g3=(((alp*(g1-g2))>>8)+g2)&0x0000ff00; int b3=(((alp*(b1-b2))>>8)+b2)&0x00ff0000; buffer[pos]=(r3)|(g3)|(b3); } void setup() { size(700,300); int targx = 0; int targy = 0; int x, y; float xvel, yvel; background(#ffffff); noBackground(); // create gravity targets for(int i = 0; i < NUMNODES; i++) { x = (int)random(WIDTH); y = (int)random(HEIGHT); n[i] = new node(x,y); } // create particles for(int i = 0; i < NUMPARTICLES; i++) { x = (int)random(WIDTH); y = (int)random(HEIGHT); xvel = random(-6,6); yvel = random(-6,6); int nodeid = (int)random(NUMNODES); targx = n[nodeid].x; targy = n[nodeid].y; p[i] = new particle(x, y, xvel, yvel, targx, targy, nodeid,0); } } void loop() { for(int i = 0; i < NUMPARTICLES; i++) { p[i].updatepos(); if(p[i].target == -1) { int nodeid = (int)random(NUMNODES); p[i].targx = n[nodeid].x; p[i].targy = n[nodeid].y; p[i].target = nodeid; } drawline((int)p[i].x, (int)p[i].y, (int)p[i].lastx, (int)p[i].lasty,0xff000000, pixels); } }