Projection method with polygons – the code

// ********* use processing 2 **************** you can download from processing.org
//------------------------------------------------------------------------------------
// this is the main code to generate quasiperiodic designs
//  similar to the one shown in "Finding polygons"
//  put this code in the main tab of processing at the left
// requires the code of "finding polygons - the code",
// "class stripes - the code", "setting up the coordinates - the modified code",
// "class Tiling - the code", "class TPoint - the code",
// "class Vector - the code" and "saving images - the code" 
float unitLength;
float xRange,yRange;     // visible coordinates from -(xy)Range to +(xy)Range
Stripes[] allStripes;
int numDirections;
float xTrans,yTrans;
float small,lineLengthSquare;
Tiling tiling;

void setup(){
  size(600,600);
  smooth();
  unitLength=5;
  setupCoordinates();
  xRange*=1.2;
  yRange*=1.2;
  vectorDiameter=6/unitLength;
  ellipseMode(CENTER);
  small=0.01;
  tiling=new Tiling();
  numDirections=5;
  //  --------------    above determines symmetry
  xTrans=50;
  yTrans=50;
  // above defines translation
  allStripes=new Stripes[numDirections];
  float alfa=PI/numDirections;
  //  choose appropriate value below:
 //  0 for 2*numDirections-fold rotational symmetry
 // 0.41 approximately for 5-fold rotational symmetry
  float plusMinus=0.;
  for(int i=0;i<numDirections;i++){
    allStripes[i]=new Stripes(i*alfa,-0.5+plusMinus);
    plusMinus*=-1;
  }
}

void draw(){
  noLoop();
  doTransformations();   // attention: translate and scale are reset at start of draw
  background(color(255,250,220));
  testPoints();
  lineLengthSquare=tiling.minDistanceSquare();
 // lineLengthSquare*=sq(0.5/sin(PI/2/numDirections));
  //  use this above if you have rhombs
  tiling.generateLines(lineLengthSquare);
  tiling.sortConnections();
  fill(color(0,100,230));
  tilingShowRegularPolygons(5);       
  fill(color(200,0,100));
  tilingShowRegularPolygons(10);       
  stroke(50,30,0);
  trueStrokeWeight(1.5);
  tiling.showConnections();
  tilingShowPointsWithoutPolygons();
//  saveImage();
}

//  test all possible points
void testPoints(){
  int k0,k1;
  int k0Max=allStripes[0].kmax;
  int k1Max=allStripes[1].kmax;
  for(k0=-k0Max;k0 < k0Max;k0++){
    for(k1=-k1Max;k1 < k1Max;k1++){
        test(k0,k1);
    }
  }
}

//show upper and lower linits of stripes at an intersection point
//  and points of star

void test(int k0,int k1){
  Vector[] starPoints=new Vector[2*numDirections];
  int[] kLow=new int[numDirections];
  int i;
  // begin with first point of the star defined by k0 and k1
  //intersection between line set 1 and numdirections-1
  kLow[numDirections-1]=k0-1;
  kLow[1]=k1;
  starPoints[0]=allStripes[1].intersection(allStripes[numDirections-1],k1,k0);
  //
  if(abs(starPoints[0].x) > xRange) return;
  if(abs(starPoints[0].y) > yRange) return;
  // we determine line of set 0
  kLow[0]=floor(allStripes[0].getK(starPoints[0]))+1;
  // now the lines of the other sets, as well as many points of the star
  for (i=2;i < numDirections-1;i++){// starpoints 1 to numDirections-3, kLow 2 to numDirections-2
    kLow[i]=floor(allStripes[i].getK(starPoints[i-2]));
    starPoints[i-1]=allStripes[i-2].intersection(allStripes[i],kLow[i-2],kLow[i]);
    if (allStripes[i-1].getK(starPoints[i-1]) > kLow[i-1]) return;
  }
  // all k-values are now determined, check remaining points
  //special points
  starPoints[numDirections-2]=allStripes[numDirections-3].intersection(allStripes[numDirections-1],kLow[numDirections-3],kLow[numDirections-1]);
  if (allStripes[numDirections-2].getK(starPoints[numDirections-2]) > kLow[numDirections-2]) return;
  starPoints[numDirections-1]=allStripes[numDirections-2].intersection(allStripes[0],kLow[numDirections-2],kLow[0]+1);
  if (allStripes[numDirections-1].getK(starPoints[numDirections-1]) > kLow[numDirections-1]) return;
  starPoints[numDirections]=allStripes[numDirections-1].intersection(allStripes[1],kLow[numDirections-1],kLow[1]+1);
  if (allStripes[0].getK(starPoints[numDirections]) < kLow[0]+1) return;

  for (i=1;i < numDirections-1;i++){  // test starpoints numDirections+1 to 2*numDirections-2
  starPoints[numDirections+i]=allStripes[i-1].intersection(allStripes[i+1],kLow[i-1]+1,kLow[i+1]+1);
  if (allStripes[i].getK(starPoints[numDirections+i]) < kLow[i]+1) return;
  }
  //the last point
  starPoints[2*numDirections-1]=allStripes[numDirections-2].intersection(allStripes[0],kLow[numDirections-2]+1,kLow[0]);
  if (allStripes[numDirections-1].getK(starPoints[2*numDirections-1]) < kLow[numDirections-1]+1) return;

          // get center=average of all points
  Vector center=new Vector(starPoints[0]);
  for (i=1;i < 2*numDirections;i++){
    center.add(starPoints[i]);
  }
  center.mult(0.5/numDirections);
  tiling.add(new TPoint(center));
}
This entry was posted in programming, Quasiperiodic design 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