Help with some fixing up (geometry)

OK The problem must be in the calculation of the intermediate points but the use of arrays is confusing. The following sketch shows how it might be done using basic geometry and rotating the graphics context.

I have modified the calculation of the alpha channel of your grey lines and dots to that polygons at all depths are visible.

void setup() {
  size(600, 600);
  background(0);
}

void draw() {
  translate(width/2, height/2);
  background(0);
  drawShape(20, 280, 30);
}

/**
Parameters
nbrSides = number of sides for each polygon (circle)
radius = radius of the outermost 

*/
void drawShape(int nbrSides, float radius, int depth) {
  pushMatrix();
  float deltaAlpha = 230.0 / depth;
  strokeWeight(1.2);
  float r = radius;
  float sideAngle = TWO_PI / nbrSides;
  float sinSideAngle = sin(sideAngle);
  float cosSideAngle = cos(sideAngle);
  float sinHalfSideAngle = sin(sideAngle / 2);

  for (int d = 0; d < depth; d++) {
    int alpha = round(255 - deltaAlpha * d);
    stroke(255, alpha);
    fill(255, alpha);
    float nx = r * cosSideAngle;
    float ny = r * sinSideAngle;
    for (int s = 0; s < nbrSides; s++) {
      line(r, 0, nx, ny);
      ellipse(r, 0, 3, 3);
      rotate(sideAngle);
    }
    // Calculate the radius for the next polygon using Pythagoras
    // Calculate half the chord length
    float halfChordLength = r * sinHalfSideAngle;
    // Calculate the next radius
    r = sqrt(r * r - halfChordLength * halfChordLength);
    // rotate by half side angle so inner touches outer polygon
    // at chord centres
    rotate(sideAngle / 2);
  }
  popMatrix();
}

The output

3 Likes