#include <fractal.h> #include <gfx.h> #include <math.h> #include <debug.h> #define PI 3.141592654 void Koch(unsigned int h, int ax, int ay, int bx, int by, int c) { int cx, cy, dx, dy, ex, ey; double l, alpha; if(((bx-ax)*(bx-ax)+(by-ay)*(by-ay))<c) { gfx_lineto(h,ax, ay, bx, by); } else { cx = ax + (bx - ax) / 3; cy = ay + (by - ay) / 3; ex = bx - (bx - ax) / 3; ey = by-(by-ay)/3; l = sqrt(((double)ex-cx)*(ex-cx)+(ey-cy)*(ey-cy)); alpha = atan(((double)ey-cy)/(ex-cx)); if((ex - cx) < 0) { alpha += PI; } dy = cy + sin(alpha + PI / 3) * l; dx = cx + cos(alpha + PI / 3) * l; Koch(h, ax, ay, cx, cy, c); Koch(h, ex, ey, bx, by, c); Koch(h, cx, cy, dx, dy, c); Koch(h, dx, dy, ex, ey, c); } } void Sierpinski(unsigned int h, int x, int y, int L, int n) { int x1, y1, x2, y2, x3, y3, x01, y01, x02, y02, x03, y03; if(n == 1) { x1=x-L/2; y1=y+L*(tan(PI/6))/2; x2=x+L/2; y2=y+L*(tan(PI/6))/2; x3=x; y3=y-L*(tan(PI/6)); gfx_lineto(h, x1, y1, x2, y2); gfx_lineto(h, x2, y2, x3, y3); gfx_lineto(h, x3, y3, x1, y1); } else { x01=x-L/4; y01=y+L*(tan(PI/6))/4; x02=x+L/4; y02=y+L*(tan(PI/6))/4; x03=x; y03=y- L*(tan(PI/6))/2; Sierpinski(h, x01,y01,L/2,n-1); Sierpinski(h, x02,y02,L/2,n-1); Sierpinski(h, x03,y03,L/2,n-1); } } void Ramus(unsigned int h, int x, int y, float alpha, int L, int n) { int x1, y1; float alpha_L, alpha_R; if(n > 0) { x1=x+cos(alpha)*L; y1=y-sin(alpha)*L; gfx_lineto(h, x, y, x1, y1); alpha_L=alpha+PI/8; alpha_R=alpha-PI/8; L=2*L/3; Ramus(h, x1,y1,alpha_L,L,n-1); Ramus(h, x1,y1,alpha_R,L,n-1); } } #define B (PI/6) #define s1 2 #define s2 3 #define s3 1.4 void tree(unsigned int h, int x, int y, int L, float A, float C) { int x1, y1, x2, y2, x1L, y1L, x2L, y2L, x1R, y1R, x2R, y2R; if(L>s1) { x2 = x + cos(A) * L; y2 = y - sin(A) * L; x2L = x2 + cos(A+B) * L / s2; y2L = y2 - sin(A+B) * L / s2; x2R = x2 + cos(A-B) * L / s2; y2R = y2 - sin(A-B) * L / s2; x1 = x + cos(A) * L / s2; y1 = y - sin(A) * L / s2; x1L = x1 + cos(A+B) * L / s2; y1L = y1 - sin(A+B) * L / s2; x1R = x1 + cos(A-B) * L / s2; y1R = y1 - sin(A-B) * L / s2; /*!~30%*/ gfx_lineto(h, x, y, x2, y2); gfx_lineto(h, x2, y2, x2L, y2L); gfx_lineto(h, x2, y2, x2R, y2R); gfx_lineto(h, x1, y1, x1R, y1R); gfx_lineto(h, x1, y1, x1L, y1L); tree(h, x2, y2, L/s3, A-C, C); tree(h, x2L, y2L, L/s2, A+B, C); tree(h, x2R, y2R, L/s2, A-B, C); tree(h, x1R, y1R, L/s2, A-B, C); tree(h, x1L, y1L, L/s2, A+B, C); } }