class Point – the code

class Point{
  Vector vec;
  Line[] connections;
  Polygon dualPolygon;
  
//  create point with unknown coordinates

  Point(){
    connections=new Line[0];
  }

//  create point at known vector
  Point(Vector v){
    connections=new Line[0];
    vec=v;
  }

  void show(){
    if (vec!=null) vec.show();
  }
  
  //  see if point is at a place of vector v
  boolean isAt(Vector v){    //returns true if the point is at position of vector v
    return vec.equal(v);
  }
  
    //  giving the points an ordering ...
  boolean above(Point p){
    if (abs(vec.y-p.vec.y) < small){
      return (vec.x > p.vec.x);
    }
    return (vec.y > p.vec.y);
  }


  void addConnection(Line newConnection){
    connections=(Line[])(append(connections,newConnection));
  }
  
  void setDualPolygon(Polygon p){
    dualPolygon=p;
  }
  
  void eliminateUndefinedConnections(){
        connections=eliminateUndefinedLines(connections);
  }
  
  void setToCenterOfPolygon(){  // sets coordinates of point to center of its dual polygon
    if(dualPolygon!=null) {
      vec=dualPolygon.center();
    }
    else vec=null;
  }

//----------------------------------------------------------------------
  
   void sortConnections(){
    float[] angles=new float[connections.length];
    Point otherEndPoint;
    int bottom,lookAt,iMin;
    float angleMin;
    for (lookAt=0;lookAt<connections.length;lookAt++){
      otherEndPoint=connections[lookAt].getOtherEndPoint(this);
      angles[lookAt]=atan2(otherEndPoint.vec.y-vec.y,otherEndPoint.vec.x-vec.x);
    }
    Line 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;
    }    
  }
  //----------------------------------------------------------------------
  
  Line findNextConnection(Line lastConnection){
    int i;
      // examine Point: find next connection to the left == next side of polygon
      for(i=0;i<connections.length;i++){
        if(connections[i]==lastConnection) break; // finding the connection going back
      }
      //  next Connection is closest connection with lower angle
      if(i==0) i=connections.length;    //  attention wraparound
      i=i-1;
      return connections[i];
  }

  //  makes a convex polygon going always to the left
//  puts polygon into list if it exists

  Polygon makePolygonFrom(Line returnConnection,Line connection, Point endPoint){
   Polygon nothing=null; 
    Line[] sides=new Line[1];
    sides[0]=connection; 
    Line lastConnection,newConnection=connection;
    Point here,newEndPoint=endPoint;
    Vector lastDirection,newDirection;
    newDirection=sub(endPoint.vec,this.vec);
    while (newEndPoint!=this){
      here=newEndPoint;
      if(here.connections.length < 2) return nothing;                    // no polygon possible
      lastConnection=newConnection;
      lastDirection=newDirection;
      newConnection=here.findNextConnection(lastConnection);
      newEndPoint=newConnection.getOtherEndPoint(here);
      newDirection=sub(newEndPoint.vec,here.vec);  
      if(lastDirection.cross(newDirection)<0) return nothing;  // test convex corner
      sides=(Line[])(append(sides,newConnection));
    }
    if(newConnection!=returnConnection) return nothing;
    return new Polygon(sides);
  }

  void makePolygons(Polygons polygons){
    if(connections.length < 2) return;                    // no polygon possible
    Line returnConnection,outConnection;
    Point returnEndPoint,outEndPoint;
    Vector returnDirection,outDirection;
    Polygon newPolygon;
    outConnection=connections[0];
    outEndPoint=outConnection.getOtherEndPoint(this);
    outDirection=sub(outEndPoint.vec,vec);
    for (int i=connections.length-1;i>=0;i--){
      returnConnection=outConnection;
      returnEndPoint=outEndPoint;
      returnDirection=outDirection;
      outConnection=connections[i];
      outEndPoint=outConnection.getOtherEndPoint(this);  
      outDirection=sub(outEndPoint.vec,vec);
                                        // each polygon only once, convex corner
      if(above(returnEndPoint)&&above(outEndPoint)
          &&(outDirection.cross(returnDirection)>0)) {
        newPolygon=makePolygonFrom(returnConnection,outConnection,outEndPoint);
        if(newPolygon!=null) polygons.addPolygon(newPolygon);
      }
    }    
  }
}
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