overlapping polygons – the code

```// You need this code to draw overlapping polygons, such as in
//  the last post "overlapping polygons".
//  It first finds at each point for pairs of lines or connections
//  with the correct angle between.
//  Then, as the other code, it searches counterclockwise for the corners of polygons.
// Knowing the angle of the line or connection coming to a corner
//  and given the corner angle of the polygon, it calculates the
//   angle required for the outgoing line or connection. Then
//  it searches for this connection. If all connections are found
//  coming back to the starting point, then it draws the polygon.

//  regular polygons with given number of corners
void tilingShowOverlapRegularPolygons(int numberCorners){
float angle=PI*(1-2./numberCorners);
for (int i=0;i<tiling.points.length;i++){
pointShowOverlapRegularPolygons(tiling.points[i],angle);
}
}

void pointShowOverlapRegularPolygons(TPoint start,float angle){
if (start.connections.length<2) return;
// search for the polygons
int out,in;
for (in=0;in<start.connections.length;in++){
out=nextConnectionAtAngle(start.angles[in],start,angle);
if(out>=0) tryOverlapRegularPolygon(start,out,angle);
}
return;
}

//  test if we are at a regular polygon ... and draw it
void tryOverlapRegularPolygon(TPoint start,int out,float angle){
Vector[] corners=new Vector[1];
corners[0]=start.vec;
TPoint next,now;
int iNewNext;
now=start;
next=start.connections[out];
iNewNext=out;
while (next!=start){
iNewNext=nextConnectionAtAngle(PI+now.angles[iNewNext],next,angle);
if(iNewNext<0) return;
corners=(Vector[])append(corners,next.vec);
now=next;
next=next.connections[iNewNext];
}
showPolygon(corners);
}

//------------------------------------------------------------------------------------------------
// rhombs with given acute angle

void tilingShowOverlapRhombs(float acuteAngle){
for (int i=0;i<tiling.points.length;i++){
pointShowOverlapRhombs(tiling.points[i],acuteAngle);
}
}

void pointShowOverlapRhombs(TPoint start,float acuteAngle){
if (start.connections.length<2) return;
// search for the rhombs
int out,in;
for (in=0;in<start.connections.length;in++){
out=nextConnectionAtAngle(start.angles[in],start,acuteAngle);
if(out>=0) tryOverlapRhomb(start,out,acuteAngle);
}
return;
}

// test if we are at a rhomb, and draw it
void tryOverlapRhomb(TPoint start,int out,float deltaAngleMust){
Vector[] corners=new Vector[4];
corners[0]=start.vec;
TPoint next,now;
int iNewNext;
now=start;
next=start.connections[out];
corners[1]=next.vec;
// obtuse angle
iNewNext=nextConnectionAtAngle(PI+now.angles[out],next,PI-deltaAngleMust);
if(iNewNext <0) return;
now=next;
next=next.connections[iNewNext];
//  newNext is corner opposed to start, with acute angle
if (start.above(next)) return;
corners[2]=next.vec;
iNewNext=nextConnectionAtAngle(PI+now.angles[iNewNext],next,deltaAngleMust);
if(iNewNext<0) return;
now=next;
next=next.connections[iNewNext];
corners[3]=next.vec;
// test last angle
iNewNext=nextConnectionAtAngle(PI+now.angles[iNewNext],next,PI-deltaAngleMust);
if(iNewNext<0) return;
if(next.connections[iNewNext]!=start) return;
showPolygon(corners);
}
//----------------------------------------------------------------------------------------

//  returns index of the connection of TPoint now with correct angle
int nextConnectionAtAngle(float inAngle,TPoint now,float angleBetween){
float outAngle=mapAngle(inAngle-angleBetween);
for (int iNext=0;iNext<now.connections.length;iNext++){
if (equalAngles(outAngle,now.angles[iNext])) return iNext;  //got it, return index
}
return -100;    // does not exist, return negative number
}```
This entry was posted in programming and tagged , , . Bookmark the permalink.