class Grid – the code

class Grid{
  Points gridPoints,dualPoints;
  Lines gridLines,dualLines;
  Polygons gridPolygons,dualPolygons;
  float cosAngle,sinAngle;    // parameters for translation and rotation
  Vector shift;
  
  Grid(){
    gridPoints=new Points();
    dualPoints=new Points();
    gridLines=new Lines();
    dualLines=new Lines();
    gridPolygons=new Polygons();
    dualPolygons=new Polygons();
    shift=new Vector(0.,0.);                             // default: no translation,no rotation
    cosAngle=1;
    sinAngle=0;
  }
  
  //  this is for setting up the grid, it should fill the xy-range region
  
  void setTranslation(float sx,float sy){
    shift.set(sx,sy);
  }
  
  void setRotation(float angle){
    cosAngle=cos(angle);
    sinAngle=sin(angle);
  }
   
  //  transformation: rotation and translation of grid
  Vector transform(Vector v){
    Vector r=add(v,shift);
    r.set(cosAngle*r.x-sinAngle*r.y,sinAngle*r.x+cosAngle*r.y);
    return r;
  }
 
//---------------------------------------------------------------------------

 //  adding a grid line, and preparing the dual
  
  void addGridLine(Vector aVec,Vector bVec){   // do transformation
    aVec=transform(aVec);
    bVec=transform(bVec);
    if(!inside(aVec,bVec)) return;             //  do nothing if line is outside
    Line newGridLine=new Line(gridPoints.findOrCreate(aVec),gridPoints.findOrCreate(bVec));
    // connections at endpoints
    newGridLine.endPointA.addConnection(newGridLine);
    newGridLine.endPointB.addConnection(newGridLine);    
    gridLines.add(newGridLine);
  }

  void makeGridPolygons(){
    gridPoints.makePolygons(gridPolygons);
  }
  
  void makeDualPolygons(){
    dualPoints.makePolygons(dualPolygons);
  }
  
  void createDualPoints(){
    gridPolygons.createDualPoints(dualPoints,dualLines);
  }
  
  void eliminateUndefinedDualLines(){
    gridLines.eliminateUndefinedDuals();
    dualLines.eliminateUndefined();
    dualPoints.eliminateUndefinedConnections();
  }
  
  //  find positions of dual points, general procedure
  
  void setDualPoints(){
    Point start=grid.gridPoints.closestToOrigin();   // choosing an appropriate grid point around the center to start
    start=start.connections[0].dualLine.endPointA;   // finding a dual point
    start.vec=new Vector(0.,0.);                     //  giving it a vector
    start.dualPolygon.setDualPoints();                           // setting points around this point and others by recursion
  }
  
  //  make the dual tiling to the grid
  
  void makeDual(){
      gridPoints.sortConnections();
      makeGridPolygons();
      createDualPoints();
      setDualPoints();
  //   dualPoints.setToCenterOfPolygon(); 
      eliminateUndefinedDualLines();
      dualPoints.sortConnections();
      makeDualPolygons();
  }
}
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