user348173 - 1 year ago 99

Javascript Question

I have a circle and I need to fill this circle by rectangles. Something like this:

Is there some algorithm for this or advice where to start?

I use fabric.js for drawing. This is playground. But, the question is more about math probably. I think there should be some formula, I hope.

Answer Source

I'll go with "close enough" for now: subtract the rectangle length from the radius, then multiply the result by 2π. That's the inner circumference. Now divide that by the rectangle width (and round down) to get the number of rectangles. Divide 360° by that to get the angles at which to draw them.

Here's a demo: http://jsbin.com/napecagado/edit?js,output