How can I calculate the angle of rotation to draw a circle with rectangles ?

edited February 2017 in Questions about Code

Hi guys, I am having a trouble to setup the correct rotation to some rectangle to make them draw a perfect circle.

I want them to be oriented in the center of the circle. I think it's just about the rotation, I tried many ways. But couldn't figure out how to do it. Here is how looks like my code.

import hype.*;
HDrawablePool pool;
float radius = 250;
float lineWithSpace = 20.0;
int numLines = round(TWO_PI * radius / lineWithSpace);
float angle = TWO_PI / numLines;
float radiusAdjust = (numLines * lineWithSpace)/ TWO_PI;

void setup() {
  size(800,800);
  H.init(this).background(255);
  smooth();

  pool = new HDrawablePool(numLines);
  pool.autoAddToStage();

  for (float p = 0 ; p < numLines; ++p) {
    HRect d = new HRect(1, 70);
    pool.add(d);
  }
  pool.onCreate(
    new HCallback() {
      public void run(Object obj) {
        int i = pool.currentIndex();
        for (float p = 0 ; p < pool.count(); p++) {
           pushMatrix();
           HDrawable d = (HDrawable) obj;
           float myAngle = p*angle;
           float x = radiusAdjust*cos(myAngle);
           float y = radiusAdjust*sin(myAngle);
              d.loc(x,y);
           d.rotate(radians(45 + i));
           popMatrix();
        }
      }
    }
  )
  .requestAll()
  ;
}

void draw() {
  translate(width/2, height/2);
  H.drawStage();
}
Sign In or Register to comment.