/* (C)2013 Claudio Rocchini, CC-BY 3.0 */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
const double PI = 3.1415926535897932384626433832795;
class edge {
public:
double l,t,a,x,y;
edge() {}
edge( double nl, double nt, double na, double nx, double ny ) :
l(nl),t(nt),a(na),x(nx),y(ny) {}
};
int main() {
const double SX = 800; const double SY = 800;
const double a = PI/11; const double r = 0.75; const double minl = 10;
std::vector<edge> st;
st.push_back( edge(200,25,-PI/2,SX/2,SY-10) );
FILE * fo = fopen("fractal_canopy.svg","w");
fprintf(fo,
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"
"<svg xmlns:svg=\"http://www.w3.org/2000/svg\" xmlns=\"http://www.w3.org/2000/svg\"\n"
"version=\"1.0\" width=\"%g\" height=\"%g\" id=\"fractal_canopy\">\n"
,SX,SY
);
double lastt = -1.0;
for(size_t i=0;i<st.size();++i) {
double nx = st[i].x+st[i].l*cos(st[i].a);
double ny = st[i].y+st[i].l*sin(st[i].a);
if(lastt!=st[i].t) {
if(lastt!=-1.0) fprintf(fo,"</g>\n");
lastt = st[i].t;
fprintf(fo,"<g style=\"stroke:#000000;stroke-width:%g;fill:none\">\n"
,lastt);
}
fprintf(fo,"<line x1=\"%6.2f\" y1=\"%6.2f\" x2=\"%6.2f\" y2=\"%6.2f\"/>\n"
,st[i].x,st[i].y,nx,ny);
if(st[i].l<=minl) continue;
st.push_back( edge(st[i].l*r,st[i].t*r,st[i].a+a,nx,ny) );
st.push_back( edge(st[i].l*r,st[i].t*r,st[i].a-a,nx,ny) );
}
fprintf(fo,"</g>\n");
fprintf(fo,"</svg>\n");
fclose(fo);
return 0;
}