#include <graphics.h>
#include <fstream>
using namespace std;
struct point{
int x,y;
} P[100],p1,p2;
int p=0;
double angle(point a,point b){
return atan2(b.y-a.y,b.x-a.x);
}
double dist(point a,point b){
return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
}
void fractal(point a,point b,int k){
double alpha = angle(a,b)-angle(P[1],P[p]);
point A[p+2];
A[1] = a;
for(int i = 2;i<=p;i++){
A[i].y = A[1].y + sin(alpha+angle(P[1],P[i]))*dist(P[1],P[i])*dist(a,b)/dist(P[1],P[p]);
A[i].x = A[1].x + cos(alpha+angle(P[1],P[i]))*dist(P[1],P[i])*dist(a,b)/dist(P[1],P[p]);
}
for(int i = 1;i<p;i++)
if(k) fractal(A[i],A[i+1],k-1);
else line(A[i].x, A[i].y,A[i+1].x, A[i+1].y);
}
void init(){
int gd = DETECT,gm;
initgraph(&gd,&gm,NULL);
}
void read(){
ifstream cin("fractal.in");
cin >> p;
for(int i = 1;i<=p;i++) cin >> P[i].x >> P[i].y;
}
int main(){
read();
init();
cleardevice();
fractal(P[1],P[p],5);
delay(60*1000);
closegraph();
return 0;
}
Once I wanted to play with fractals, but I couldn't find a lightweight and easy tool for doing this. So I decided to create one. There are many approaches to creating fractals I started from the idea that we can create a fractal if we have a set of segments that are connected to each other and every segment we replace with the entire shape: https://www.youtube.com/watch?v=YiGBNNDDgH0&feature=youtu.be&t=155;
I don't think this algorithm needs explanation, the math is quite simple.
P.S Here is my tool based on this algorithm: Fractal Maker
What algorithm would you use? What other approaches do you know?