class TPoint – the code

// a class for points in a tiling, taking into account connection by lines to other points
//  incorporates the class Vector, might also be programmed as an extension

class TPoint{
  Vector vec;
  TPoint[] connections;
  float[] angles;                      // angles of the lines to sort out the connections
  
  TPoint(Vector p){
    vec=p;
    connections=new TPoint[0];
    angles=new float[0];
  }
  
  //  adding a line of the tiling to the connections
  void addConnection(TPoint p){
    connections=(TPoint[]) append(connections,p);
    angles=(float[]) append(angles,atan2(p.vec.y-vec.y,p.vec.x-vec.x));
  }
  
  //  show the point 
  void showPoint(){
    vec.show();
  }
  
  //  giving the points an ordering ...
  boolean above(TPoint p){
    if (abs(vec.y-p.vec.y)  <  small){
      return (vec.x  >  p.vec.x);
    }
    return (vec.y  >  p.vec.y);
  }
  
  //  show connections as lines
  void showConnections(){
    for (int i=0;i < connections.length;i++){   //  draw connections only once, thus ...
      if(above(connections[i])){
        vec.showLine(connections[i].vec);
      }
    }
  }
  
  //  sort connections according to their angles...
  void sortConnections(){
    int bottom,lookAt,iMin;
    float angleMin;
    TPoint store;
    for (bottom=0;bottom < connections.length-1;bottom++){
      iMin=bottom;
      angleMin=angles[bottom];
      for (lookAt=bottom+1;lookAt < connections.length;lookAt++){
        if(angles[lookAt] < angleMin){
          iMin=lookAt;
          angleMin=angles[lookAt];
        }
      }
      angles[iMin]=angles[bottom];
      angles[bottom]=angleMin;
      store=connections[iMin];
      connections[iMin]=connections[bottom];
      connections[bottom]=store;
    }
  }

// showing the tile related to connections i and j
  void showTiles(){
    if(connections.length  <  2) return;                    // no tile
    int i,j;
    float deltaAngle;
    Vector d=new Vector(0,0);
    for (i=0;i < connections.length;i++){
      j=i+1;
      if (j==connections.length) j=0;                  //wraparound-pair
      if(above(connections[i])&&above(connections[j])){   // draw only once ...
        deltaAngle=angles[j]-angles[i];             // distinction between rhombs and squares ... and detection of border
        if (deltaAngle  <  0) deltaAngle=deltaAngle+2*PI;
        if (deltaAngle  <  PI){                      //  problems at border of tiling, no tile of angle larger than 180 degrees
          fill(128,255,255);
          if (abs(deltaAngle-0.5*PI) < small) fill(255,255,128);     // angle of 90 degrees: squares
          d.copy(connections[i].vec);
          d.add(connections[j].vec);
          d.sub(vec);
          quad(vec.x,vec.y,connections[i].vec.x,connections[i].vec.y,
            d.x,d.y,connections[j].vec.x,connections[j].vec.y);
        }
      }
    }  
  }  
}
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