## 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
sides=theseSides;
}

//  drawing the polygon as a shape requires special care

void show(){
beginShape();
Point corner=sides[sides.length-1].commonEndPoint(sides[0]);
for(int i=0;i<sides.length;i++){
corner=sides[i].getOtherEndPoint(corner);
vertex(corner.vec.x,corner.vec.y);
}
endShape(CLOSE);
}

// 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++){
corner=sides[i].getOtherEndPoint(corner);
}
c.mult(1./float(sides.length));
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
side=sides[i];
dualToSide=side.dualLine;
if(dualToSide==null) {                         // dualline needs to be created
dualToSide=new Line();                        //  add to list of lines
side.setDual(dualToSide);
dualToSide.setDual(side);
}
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++){
lastCorner=corner;
corner=sides[i].getOtherEndPoint(corner);
dualPointAcrossSide=sides[i].dualLine.getOtherEndPoint(dualPoint);
if(dualPointAcrossSide!=null){                           //  set only position of dual points if still unknown
//  this also teerminates recursion
if(dualPointAcrossSide.vec==null){
direction=sub(corner.vec,lastCorner.vec);            //  for each side get its direction
direction.normalize();
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
if(dualPointAcrossSide.dualPolygon!=null)dualPointAcrossSide.dualPolygon.setDualPoints();
}
}
}
}
}```
This entry was posted in programming and tagged , , . Bookmark the permalink.