#!/usr/bin/ruby

# Based on Euler's identity: e^(pi*x*i) = cos(pi*x) + i*sin(pi*x)

require('Imager');

var iter = 20;          # the number of polygons
var size = 200;         # the diameter of each polygon in pixels

var (width=(iter*size + size), height=700);
var img = %O<Imager>.new(xsize => width, ysize => height);
img.box(filled => 1, color => 'white');

iter.times { |k|
    var points = gather {
        k.times { |n|
            var x = (size*k   + 100*cos(n*Number.pi / k/2));
            var y = (height/2 + 100*sin(n*Number.pi / k/2));
            take([x.round(0), y.round(0)]);
        }
    }

    img.polygon(
        points => points,
        color => [0, k, 255/k],
    )
}

img.write(file => 'circle_from_polygons.png');
