//-------------------------------------------------------------------------------- class DParticle { double x, y, xv, yv; color c; DParticle(double ix, double iy, double ixv, double iyv) { x = ix; y = iy; xv = ixv; yv = iyv; } void update() { x += xv; y += yv; } void update(double d) { xv *= d; yv *= d; x += xv; y += yv; } void spring(double sx, double sy, double rest, double amount) { double dx = sx - x; double dy = sy - y; if(dx != 0 || dy != 0) { // Prevent / by zero double mag = Math.sqrt(dx*dx + dy*dy); double ext = mag - rest; xv += (dx / mag * ext) * amount; yv += (dy / mag * ext) * amount; } } void goal(double gx, double gy, double amount) { if(x != gx) xv += (gx - x) * amount; if(y != gy) yv += (gy - y) * amount; } void avoid(double x, double y, double r, double amount) { if(touching(x, y, r)) spring(x, y, r, amount); } void contain(double l, double t, double r, double b, double bouncy) { if(x < l) { x = l; xv *= -bouncy; } if(x > r) { x = r; xv *= -bouncy; } if(y < t) { y = t; yv *= -bouncy; } if(y > b) { y = b; yv *= -bouncy; } } void containWrap(double l, double t, double r, double b) { if(x < l) { x = r+(x-l); } if(x > r) { x = l+(x-r); } if(y < t) { y = b+(y-t); } if(y > b) { y = t+(y-b); } } boolean touching(double jx, double jy, double radii) { double dx = jx-x; double dy = jy-y; return dx*dx+dy*dy < radii*radii; } } //-------------------------------------------------------------------------------- class Particle { float x, y, xv, yv; color c; Particle(float ix, float iy, float ixv, float iyv) { x = ix; y = iy; xv = ixv; yv = iyv; } void update() { x += xv; y += yv; } void update(float d) { xv *= d; yv *= d; x += xv; y += yv; } void spring(float sx, float sy, float rest, float amount) { float dx = sx - x; float dy = sy - y; if(dx != 0 || dy != 0) { // Prevent / by zero float mag = sqrt(dx*dx + dy*dy); float ext = mag - rest; xv += (dx / mag * ext) * amount; yv += (dy / mag * ext) * amount; } } void spring(Particle p, float rest, float amount) { float dx = p.x - x; float dy = p.y - y; if(dx != 0 || dy != 0) { // Prevent / by zero float mag = sqrt(dx*dx + dy*dy); float ext = mag - rest; xv += (dx / mag * ext) * amount; yv += (dy / mag * ext) * amount; } } void goal(float gx, float gy, float amount) { if(x != gx) xv += (gx - x) * amount; if(y != gy) yv += (gy - y) * amount; } void goal(float l, float t, float r, float b, float amount) { if(x < l) goal(l, y, amount); if(x > r) goal(r, y, amount); if(y < t) goal(x, t, amount); if(y > b) goal(x, b, amount); } void avoid(float x, float y, float r, float amount) { if(touching(x, y, r)) spring(x, y, r, amount); } void bounce(float hitx, float hity, float angle, float amount) { // Rotate a new vector to the angle float ax = cos(angle); float ay = sin(angle); float dotp = dot(xv, yv, ax, ay); xv = (ax * dotp - xv)*amount+ax; yv = (ay * dotp - yv)*amount+ay; x = hitx; y = hity; } void contain(float l, float t, float r, float b, float bouncy) { if(x < l) { x = l; xv *= -bouncy; } if(x > r) { x = r; xv *= -bouncy; } if(y < t) { y = t; yv *= -bouncy; } if(y > b) { y = b; yv *= -bouncy; } } void containWrap(float l, float t, float r, float b) { if(x < l) { x = r+(x-l); } if(x > r) { x = l+(x-r); } if(y < t) { y = b+(y-t); } if(y > b) { y = t+(y-b); } } boolean touching(float jx, float jy, float radii) { float dx = jx-x; float dy = jy-y; return dx*dx+dy*dy < radii*radii; } void copy(Particle p) { x = p.x; y = p.y; xv = p.xv; yv = p.yv; c = p.c; } } //-------------------------------------------------------------------------------- class DVector2 { double x, y; DVector2(double ix, double iy) { x = ix; y = iy; } } //-------------------------------------------------------------------------------- // Helpers float rotatedX(float v,float angle) { return v*cos(angle); } float rotatedY(float v,float angle) { return v*sin(angle); } float rotatedX(float x,float y,float angle) { return x*cos(angle) - y*sin(angle); } float rotatedY(float x,float y,float angle) { return x*sin(angle) + y*cos(angle); } float dot(float x1, float y1, float x2, float y2) { return x1*x2 + y1*y2; } // generic linear interpolation int mix(int a,int b, float f) { return (int)(a+(b-a)*f); } double mix(double a, double b, double f) { return a+(b-a)*f; } // blend 2 colours int blendC(int a, int b, float f) { return mix(a>>16&0xff,b>>16&0xff,f)<<16 | mix(a>>8&0xff,b>>8&0xff,f)<<8 | mix(a&0xff,b&0xff,f) ; } int blendC0(int a, int b, float f) { return mix(a>>16,b>>16,f)<<16 | mix(a>>8&0xff,b>>8&0xff,f)<<8 | mix(a&0xff,b&0xff,f); } //-------------------------------------------------------------------------------- // Drawing void aapixel(float x, float y, int c, float a) { int xint = (int)x; int yint = (int)y; float fx = x - xint; float fy = y - yint; //pixels[xint+yint*width] = blendC(pixels[xint+yint*width], c, ((1-fx)*(1-fy))*a); // dub sez... blend with color of image pixels[xint+yint*width] = blendC( pixels[xint+yint*width], myFoodImg.pixels[xint+yint*width], ((1-fx)*(1-fy))*a); if(xint