BoJack_Horseman's blog

By BoJack_Horseman, 7 years ago, In English
#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. And I decided to create one. There are many approaches to creating fractals I was based on 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, there is a quite simple math. And my question is: what algorithm would you use? what other approaches do you know?

P.S: I know my algorithm could be slightly optimised but I am too lazy to do this. P.S.S There is my tool based on this algorithm: Fractal Maker

Full text and comments »

  • Vote: I like it
  • +9
  • Vote: I do not like it