Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- /* Generate koch fractal in SVG format (Scalable vector graphics)
- * http://ecademy.agnesscott.edu/~lriddle/ifs/kcurve/kcurve.htm
- * Run ./a.out > kochfract.svg and look at the file in a browser
- * SVG drawing commands: l=line, m=move, h=horiz. line, v=vert. line
- * uppercase: absolute coordinates, lowercase: relative coordinates
- * e.g. M x y = move to (x, y)
- * l dx dy = line from current point, moving by dx, dy */
- void kochfract(double len, double ang) {
- if (len < 30) {
- printf("l %.1f %.1f ", len * cos(ang), -len * sin(ang));
- }
- else {
- double l3 = len/3;
- kochfract(l3, ang);
- kochfract(l3, ang+acos(.5));
- kochfract(l3, ang-acos(.5));
- kochfract(l3, ang);
- }
- }
- int main(void)
- {
- printf("%s", "<?xml version=\"1.0\"?>\n" // .svg file header
- "<svg version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">\n"
- "<path fill=\"none\" stroke=\"blue\" d=\"M 0 200 ");
- kochfract(300, 0);
- puts("\"/>\n</svg>"); // end of .svg file
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment