class Bug { private float DRAG = 0.98; private float HAIR_INFLUENCE = 0.01; private float SCREEN_SIZE = 2.0; private QColor SCREEN_COLOR = new QColor(color(168,130,55)); public static final int DOT_MODE_NORMAL = 0; public static final int DOT_MODE_ANTIALIASED = 1; private int DOT_MODE = DOT_MODE_ANTIALIASED; private float PRESSURE = 0.1; private float lastX=0, lastY=0; private float x=0, y=0; private float xm=0, ym=0; private int overX = 0, overY = 0; private float wid, hei; private Hair[][] hair; Bug(Hair[][] _hair, float p, float drag, float w, float h) { wid = w; hei = h; x = random(wid); y = random(hei); xm = random(-5,5); ym = random(-5,5); hair = _hair; setPressure(p); setDrag(drag); } public void setDotMode(int dm) { DOT_MODE = dm; } public void randomizePosition() { x = random(wid); y = random(hei); } public void updateHair(Hair[][] _hair) { hair = _hair; } public void setPressure(float p) { PRESSURE = p; //SCREEN_COLOR_TRACE = color(red(SCREEN_COLOR_TRACE),green(SCREEN_COLOR_TRACE),blue(SCREEN_COLOR_TRACE),p); } public void setDrag(float drag) { DRAG = drag; } void update() { int ix = int(hair.length*x/wid); int iy = int(hair[0].length*y/hei); if (ix<0) ix=0; if (ix>=hair.length) ix=hair.length-1; if (iy<0) iy=0; if (iy>=hair[0].length) iy=hair[0].length-1; xm += HAIR_INFLUENCE * hair[ix][iy].getDirX(); ym += HAIR_INFLUENCE * hair[ix][iy].getDirY(); lastX = x; lastY = y; x += xm; y += ym; if (x<0) { x+=wid; lastX+=wid; overX=-1; } if (x>=wid) { x-=wid; lastX-=wid; overX=1; } if (y<0) { y+=hei; lastY+=hei; overY=-1; } if (y>=hei) { y-=hei; lastY-=hei; overY=1; } xm *= DRAG; ym *= DRAG; } void show() { color co = SCREEN_COLOR.toColor(); for (float xo=-SCREEN_SIZE;xo<=SCREEN_SIZE;xo++) { for (float yo=-SCREEN_SIZE;yo<=SCREEN_SIZE;yo++) { int sx = int(x+xo); int sy = int(y+yo); if (DOT_MODE==DOT_MODE_NORMAL) { set(sx,sy,colorMixType(co,get(sx,sy),0.5,BLENDING_MODE.NORM)); } else { if (xo==0||yo==0) set(sx,sy,colorMixType(co,get(sx,sy),0.5,BLENDING_MODE.NORM)); } } } } void drawOn(QImage img) { float distance = sqrt(sq(lastX-x)+sq(lastY-y)); float strength = 0.0; if (distance>1.0) { strength = PRESSURE/distance; } else { strength = PRESSURE; } // draw two lines when crossed over edge for (int i=0;i<((overX==0&&overY==0)?1:2);i++) { for (float step = 0; step