Here's the demo CFDG
Code: Select all
startshape seed
include i_polygons.cfdg
rule seed {
CIRCLE {x -2 y 2 b 0.5}
CIRCLE {x -2 y 0 b 0.5}
polygon5sided {x 0 y 0}
polygon6sided {x 2 y 0}
polygon7sided {x 4 y 0}
polygon8sided {x 6 y 0}
polygon9sided {x 0 y 2}
polygon10sided {x 2 y 2}
polygon11sided {x 4 y 2}
polygon12sided {x 6 y 2}
CIRCLE {x 8 y 0 b 0.5}
CIRCLE {x 8 y 2 b 0.5}
}
Code: Select all
#include <iostream>
#include <sstream>
#include <cmath>
using namespace std;
const float PI = 3.14159265;
float cleanFloat(float f) {
if (f < 0.001 && f > -0.001)
return (0);
else
return (f);
}
int main () {
float R = 1.0;
for (int n = 5; n < 100; n++) {
cout << "\n\nrule polygon" << n << "sided {";
float angle = 360.0 / float(n) / 2.0;
float side = 2 * R * abs(sin(PI * angle / 180.0));
float inner_circle_radius = R * cos(angle * PI / 180.0);
float dist = inner_circle_radius - side/2;
for (int k = 0; k < n; k++) {
float theta = float(k)*360.0/float(n);
float x = dist * cos(PI * theta / 180.0);
float y = dist * sin(PI * theta / 180.0);
cout << "\n SQUARE {size "
<< cleanFloat(side)
<< " x "
<< cleanFloat(x)
<< " y "
<< cleanFloat(y)
<< " rotate "
<< cleanFloat (theta)
<< "}";
}
cout << "\n CIRCLE {s " << cleanFloat(inner_circle_radius) << "}";
cout << "\n}";
}
}
http://chriscoyne.com/cfdg/i_polygons.cfdg
Enjoy!
cc