File:Fractal canopy.svg
Summary
Description |
English: Example of Fractal Canopy with angle=PI/11 and reduction factor=0.75 |
Date | |
Source | Own work |
Author | Claudio Rocchini |
Licensing
I, the copyright holder of this work, hereby publish it under the following license:
This file is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported license.
- You are free:
- to share – to copy, distribute and transmit the work
- to remix – to adapt the work
- Under the following conditions:
- attribution – You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
- share alike – If you remix, transform, or build upon the material, you must distribute your contributions under the same or compatible license as the original.
Source Code
/* (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;
}