class Polygon – the code

class Polygon{
  Line[] sides;                           //   attention: we do not know their orientation
  Point dualPoint;
  Polygon(Line[] theseSides){            //  dual point does not exist yet
  //  drawing the polygon as a shape requires special care
  void show(){
    Point corner=sides[sides.length-1].commonEndPoint(sides[0]);
    for(int i=0;i<sides.length;i++){
  // drawing special polygons
  //  check abs(cos(angle)) for squares and rhombs
  float absCosAngle(){
    Vector one=sub(sides[0].endPointA.vec,sides[0].endPointB.vec);
    Vector two=sub(sides[1].endPointA.vec,sides[1].endPointB.vec);
    return abs(one.scalar(two)/one.theLength()/two.theLength());
 //  show only regular polygons with given number of corners
  void showRegular(int nCorners){
    if(nCorners!=sides.length) return;
    if((nCorners!=4)||(absCosAngle()<small)) show();      // 4 corners: check if it really is a square
  //  show only rhombs with given absolute value of cos(angle)
  void showRhomb(float cosAngleMust){
     if((sides.length==4)&&(abs(absCosAngle()-cosAngleMust)<small)) show();      // 4 corners: check angle
  //  calculate center of the polygon
  Vector center(){
    Vector c=new Vector(0,0);
    Point corner=sides[sides.length-1].commonEndPoint(sides[0]);
    for(int i=0;i<sides.length;i++){
    return c;
  //  set endpoint of dual lines - they are the duals of the sides
  //   one of their end points is the dual point of the polygon
  //  and make up connections
  void createDualPoint(Points dualPoints,Lines dualLines){
    dualPoint=new Point();              // create dual point
    dualPoints.add(dualPoint);           //  put it into list
    dualPoint.setDualPolygon(this);     // this polygon is the dual of its dual point
    Line side,dualToSide;
    for(int i=0;i<sides.length;i++){                  // the dual point is an end point of the dual lines of the sides
      if(dualToSide==null) {                         // dualline needs to be created
        dualToSide=new Line();                        //  add to list of lines
      dualToSide.setAnEndPoint(dualPoint);            // set one endPoint of dual line
      dualPoint.addConnection(dualToSide);            // add dual line to list of connections of dual point
  //  setting dual points using a grid polygon around a dual point with known position, at the ends of dual lines
  //  attention of orientattion
  void setDualPoints(){ 
    Point lastCorner,corner=sides[sides.length-1].commonEndPoint(sides[0]);
    Point dualPointAcrossSide;
    Vector direction;
    for(int i=0;i<sides.length;i++){                   
      if(dualPointAcrossSide!=null){                           //  set only position of dual points if still unknown
                                                                //  this also teerminates recursion
          direction=sub(corner.vec,lastCorner.vec);            //  for each side get its direction
          direction.rotate90();                                //  rotate by 90 degrees -> direction of dual line
          dualPointAcrossSide.vec=sub(dualPoint.vec,direction);   //  set the position of the dual point
          //  and go to the grid polygon around this dual point to set other dual points
          //  note that the dual polygon of a dual point is a grid polygon, the dual polygon of this grid polygon 
          // is again this dual point
          //  attention: near border - grid polygon might not exist
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: Logo

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s