Projection method and corner points – the code

float unitLength;
float xRange,yRange;     // visible coordinates from -(xy)Range to +(xy)Range
float xShift,yShift;     //  shifting one grid to get different parts of the tiling
float sqrt2=sqrt(2.),sqrt05=sqrt(0.5);


void setup(){
  size(600,600);
  smooth();
  unitLength=30;
  xShift=0.01;
  yShift=0.05;
  ellipseMode(CENTER);
}

void draw(){
  noLoop();
  setupCoordinates();
  background(255);
  fill(0,255,0);
  stroke(0);
  ammannBeenkerProjection();
  fill(0);
  stroke(128);
  diagonalSquares();
  straightSquares();
}

void setupCoordinates(){  
  translate(width/2,height/2);
  scale(unitLength,-unitLength);
  trueStrokeWeight(1.);
  xRange=0.5*width/unitLength;
  yRange=0.5*height/unitLength;
}

void trueStrokeWeight(float weight){
 strokeWeight(weight/unitLength);
} 

//drawing square lattices

  void diagonalSquares() {
    int i1, j1, limit;
    float x1, y1; 
    limit=int(xRange*2);
    for (i1=-limit;i1<limit;i1++) {
      for (j1=-limit;j1<limit;j1++) {
        x1=(i1+j1)*sqrt05+xShift;
        y1=(i1-j1)*sqrt05+yShift;
        ellipse(x1, y1, 2/unitLength, 2/unitLength);
        line(x1-sqrt05, y1, x1, y1-sqrt05);
        line(x1-sqrt05, y1, x1, y1+sqrt05);
      }
    }
  }
  
  
  void straightSquares() {
    int i2, j2,limit;
    float x2, y2; 
    limit=int(xRange)+2;
    for (i2=-limit;i2<limit;i2++) {
      for (j2=-limit;j2<limit;j2++) {
        x2=i2;
        y2=j2;
        ellipse(x2, y2, 2/unitLength, 2/unitLength);
        line(x2-0.5, y2-0.5, x2+0.5, y2-0.5);
        line(x2-0.5, y2-0.5, x2-0.5, y2+0.5);
      }
    }
  }
  
  
  // projektion method

  void ammannBeenkerProjection() {
    int i1, j1, i2, j2, limit;
    float x1, y1;
    int x2, y2; 
    float xm, ym, dx, dy, h;
    limit=int(xRange*2);
    for (i1=-limit;i1<limit;i1++) {
      for (j1=-limit;j1<limit;j1++) {
        //center points of diagonal lattice
        x1=(i1+j1)*sqrt05+xShift;
        y1=(i1-j1)*sqrt05+yShift;
        if((abs(x1)<xRange)&&(abs(y1)<yRange)){ //continue only if on screen
          x2=round(x1);   //next center points of straight square lattice
          y2=round(y1);
          dx=abs(x1-x2);    //  difference between points inside octagon ???
          dy=abs(y1-y2);
          if (dy>dx) {
            h=dx;
            dx=dy;
            dy=h;
          }
          if ((dx<(sqrt05-0.5))&&((dx+dy)<(1-sqrt05))) {  
            xm=(x1+x2)/2;                         // yes: got new corner point 
            ym=(y1+y2)/2;
            ellipse(xm, ym,0.25,0.25);              // show the point
          }
        }
      }
    }
  }
This entry was posted in programming and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s