//***************************************************************************** // Edge rasterizer, Jarek Rossignac, Feb 2007 //***************************************************************************** int vn=2, n=10; int bi=-1; float s=1; color[][] C = new color [n][n]; pt[] P = new pt[vn]; pt Mouse = new pt(0,0); // current mouse position color red = color(200, 10, 10); color blue = color(10, 10, 200); color green = color(100, 235, 100); color black = color(10, 10, 10); color yellow = color(255, 235, 180); vec DD = new vec(-3,3); void setup() { size(800, 800); PFont font = loadFont("Courier-14.vlw"); textFont(font, 12); strokeJoin(ROUND); strokeCap(ROUND); P[0]=new pt(width*0.13,height*0.87); P[1]=new pt(width*0.76,height*0.37); s=width/n; for(int i=0; i=i0) for (int i=i0; i<=i1; i++) { int j = round(A.y+slope*(i-A.x)); if(((i!=round(A.x))||(j!=round(A.y)))&&((i!=round(B.x))||(j!=round(B.y)))) { C[i][j]=c; }; }; } else { float slope = (B.x-A.x) / (B.y-A.y); int i0 = min(min(ceil(A.y),ceil(B.y)),ys+1), i1=max(max(floor(A.y),floor(B.y)),yf-1); if(i1>=i0) for (int i=i0; i<=i1; i++) { int j = round(A.x+slope*(i-A.y)); if(((i!=round(A.y))||(j!=round(A.x)))&&((i!=round(B.y))||(j!=round(B.x)))) { C[j][i]=c; }; }; }; } void rasterizeShow(pt A, pt B, color c) { pt X=new pt(A.x,A.y); X.mul(s); fill(blue); stroke(blue); X.show(5); X.setTo(B.x,B.y); X.mul(s); fill(blue); stroke(blue); X.show(3); int xs=min(round(A.x),round(B.x)), ys=min(round(A.y),round(B.y)); int xf=max(round(A.x),round(B.x)), yf=max(round(A.y),round(B.y)); int count=0; fill(blue); if (B.y-A.y+B.x-A.x>0) {X.setTo(round(A.x),round(A.y)); X.mul(s); X.label(str(count),DD); } else { X.setTo(round(B.x),round(B.y)); X.mul(s); X.label(str(count),DD); }; stroke(red); fill(red); if ((abs(B.y-A.y) < abs(B.x-A.x))) { float slope = (B.y-A.y) / (B.x-A.x); vec D = new vec(B.x-A.x,B.y-A.y); D.unit(); D.left(); D.mul(10); D.add(DD); int i0 = min(min(ceil(A.x),ceil(B.x)),xs+1), i1=max(max(floor(A.x),floor(B.x)),xf-1); if(i1>=i0) for (int i=i0; i<=i1; i++) { int j = round(A.y+slope*(i-A.x)); if(((i!=round(A.x))||(j!=round(A.y)))&&((i!=round(B.x))||(j!=round(B.y)))) { count++; X.setTo(i,A.y+slope*(i-A.x)); X.mul(s); X.show(3); X.label(str(count),D); }; }; } else { float slope = (B.x-A.x) / (B.y-A.y); vec D = new vec(B.x-A.x,B.y-A.y); D.unit(); D.left(); D.mul(10); D.add(DD); int i0 = min(min(ceil(A.y),ceil(B.y)),ys+1), i1=max(max(floor(A.y),floor(B.y)),yf-1); if(i1>=i0) for (int i=i0; i<=i1; i++) { int j = round(A.x+slope*(i-A.y)); if(((i!=round(A.y))||(j!=round(A.x)))&&((i!=round(B.y))||(j!=round(B.x)))) { count++; X.setTo(A.x+slope*(i-A.y),i); X.mul(s); X.show(3); X.label(str(count),D); }; }; }; count++; fill(blue); if (B.y-A.y+B.x-A.x<=0) { X.setTo(round(A.x),round(A.y)); X.mul(s); X.label(str(count),DD); } else { X.setTo(round(B.x),round(B.y)); X.mul(s); X.label(str(count),DD); }; } void mousePressed() { // to do when mouse is pressed float bd=900; // init square of smallest distance to selected point Mouse.setFromMouse(); // save current mouse location for (int i=0; i10) bi=-1; // nothing selected } void mouseReleased() {bi=-1;} // to do when mouse is released float d2(int j) {return (Mouse.disTo(P[j]));}; // squared distance from mouse to vertex P[j] //********************************** //*** CONTROL WITH KEYS //**********************************e void keyPressed() { if (key=='0') ; if (key=='i') { saveFrame("pix-####.tif");}; if (key=='A') { // axis aligns the edges for (int i=0; iabs(P[in(i)].y-P[i].y)) {float dy=(P[in(i)].y-P[i].y)/2; P[in(i)].y-=dy; P[i].y+=dy;} else {float dx=(P[in(i)].x-P[i].x)/2; P[in(i)].x-=dx; P[i].x+=dx;} }; }; if (key=='F') { float sx=width; float sy=height; float bx=0.0; float by=0.0; for (int i=0; ibx) {bx=P[i].x;}; if (P[i].xby) {by=P[i].y;}; if (P[i].y