FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   Bresenhamming it up again
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: Bresenhamming it up again  (Read 279 times)
st33d

WWW Email
Bresenhamming it up again
« on: Dec 22nd, 2004, 3:49am »

I'm busy working on a html parser and the old Java string functions are keeping me busy. But since I just recently got my pixel to line scanner to work in colour and the AIExport class has been updated (thankyou William) I thought I'd go back to this old chestnut.
 
And I really am stumped.
 
I'm not bothering to write a horizontal / diagonal / vertical scan till I get this right.
 
The problem isn't really visible until you get it into Illustrator and magnify it 1000%. It becomes apparent that although the pattern should be uniform in all directions, it isn't. And it's when you un-rem the image load-and-place that you can see it going really wrong (oil spill wrong). The scan should draw over existing lines with out side stepping off the mark. It should be drawing all the lines that could possibly exist between the pixels. But it seems to be using it's imagination (which is my job, not the PC's).
 
Code:

int i = 0;
AIExport ai;
void setup(){
  size (50,30);
  background (255);
  //BImage scan = loadImage ("Rachel.gif");
  //image (scan,0,0);
  line (0,0,20,20);line (20,5,5,20);line (21,6,6,21);
  ai = new AIExport( this, 1 );
  ai.toggleContinuousRecording();
  ai.turnTransparencyOff();
  ai.setLineWeight( 0.25 );
  fill(0);
  //framerate(1);
  //ellipse(20,10,10,3);
}
void loop(){
  ai.run();
  if (i<pixels.length){
  if (pixels[i] != #FFFFFF){bscan(i);}
    println(pixels.length - i);
    i++;
  }
  if (i == pixels.length){
    ai.toggleContinuousRecording();
    i++;
    println("done");
  }
}//loop;
void bscan(int it){
  int x1 = (it) % width;
  int y1 = it / width;
  int xd; int yd; int oct=0; boolean d;
  color c = #000000;
  for (int y2 = 0; y2 < height; y2++){
    for (int x2 = 0; x2 < width; x2++){
 //println("x1:"+x1+"y1:"+y1+"x2:"+x2+"y2:"+y2);
 d = false;
 xd = x2 - x1;
 yd = y2 - y1;
 //octant 0
 if (x2 > x1 && y2 >= y1 && abs(yd) < abs(xd)){
   int e = 0;
   int y = y1;
   for (int x = x1; x <= x2; x++){
  if (pixels[x + y*width] == c){d = true;}else{d = false; break;}
     if(((e + yd)<<1) < xd){
  e += yd;
     }else{
  y++;
  e += (yd-xd);
     }
   }//for;
 }
 //octant 1
 else if (x2 > x1 && y2 > y1 && abs(yd) >= abs(xd)){
   int e = 0;
   int x = x1;
   for (int y = y1; y <= y2; y++){
  if (pixels[x + y*width] == c){d = true;}else{d = false; break;}
     if(((e + xd)<<1) < yd){
  e += xd;
     }else{
  x++;
  e += (xd - yd);
     }
   }//for;
 }
 //octant 2
 else if (x2 <= x1 && y2 > y1 && abs(yd) > abs(xd)){
   int e=0;
   int x=x1;
   for (int y = y1; y <= y2; y++){
  if (pixels[x + y*width] == c){d = true;}else{d = false; break;}
     if(((e + xd)<<1) > (0 - yd)){
  e += xd;
     }else{
  x--;
  e += (xd + yd);
     }
   }//for;
 }
 //octant 3
 else if ( x2 < x1 && y2 > y1 && abs(yd) <= abs(xd)){
   xd = abs(xd);
   int e = 0;
   int y = y1;
   for (int x = 0; x <= xd; x++){
  if (pixels[(x1 - x) + y*width] == c){d = true;}else{d = false; break;}
     if(((e + yd)<<1) < xd){
  e += yd;
     }else{
  y++;
  e += (yd - xd);
     }
   }//for;
 }
 if(d){
   ai.ai_line (x1,y1,x2,y2);
   //line (x1,y1,x2,y2);
 }
    }//xfor;
  }//yfor;
}//bscan;

 
There must be some hiccup in the way I've written the Bresenhams. Can anyone see it?
 

I could murder a pint.
Pages: 1 

« Previous topic | Next topic »