float orx,ory; void setup(){ size(720,480); background(0); fill(255); stroke(0); orx = 360; ory = 240; //frameRate(2); noStroke(); } void draw(){ float rx = random(0,720); float ry = random(0,480); //strokeWeight(2); fill(0); bresenhamLine((int)rx,(int)ry, (int)orx, (int)ory,5,true); fill(255); bresenhamLine((int)rx,(int)ry, (int)orx, (int)ory,5,false); orx = rx; ory = ry; saveFrame(); } void bresenhamLine(int x0, int y0, int x1, int y1,int sz,boolean outline){ int topY = Math.min(y0,y1); int botY = Math.max(y0,y1); int lftX = Math.min(x0,x1); int rtX = Math.max(x0,x1); double dX = (rtX-lftX); double dY = (botY-topY); if(dX==0){ rect(lftX,topY,sz,botY-topY); }else if(dY==0){ rect(lftX,topY,rtX-lftX,sz); } else if(dX>dY){ float curY = topY; //if(dY/dX<0.01||dY/dX>1000) println("dX = "+dX+" dY = "+dY); for(int i=0; i<=dX; i+=sz){ if(outline) rect((float)(lftX+i)-1,(float)Math.floor(curY/sz)*sz-1,sz+2,sz+2); else rect((float)(lftX+i),(float)Math.floor(curY/sz)*sz,sz,sz); curY+= ((dY/dX)*5); } }else{ float curX = lftX; //if(dY/dX<0.01||dY/dX>1000) println("dX = "+dX+" dY = "+dY); for(int i=0; i<=dY; i+=sz){ if(outline) rect((float)Math.floor(curX/sz)*sz-1,(float)(topY+i)-1,sz+2,sz+2); else rect((float)Math.floor(curX/sz)*sz,(float)(topY+i),sz,sz); curX+= ((dX/dY)*5); } } }