• C++ Programming for Financial Engineering
    Highly recommended by thousands of MFE students. Covers essential C++ topics with applications to financial engineering. Learn more Join!
    Python for Finance with Intro to Data Science
    Gain practical understanding of Python to read, understand, and write professional Python code for your first day on the job. Learn more Join!
    An Intuition-Based Options Primer for FE
    Ideal for entry level positions interviews and graduate studies, specializing in options trading arbitrage and options valuation models. Learn more Join!

FRACTALS

From the Attached Zip File extract program FRACTAL to your desktop and execute,
It will display the Koch Curve Fractal

Actual Code
C++:
/***********START***************/
// elementary fractal 
void fractal_Koch_curve(void){
	
	char file_name [60] = "c:\\exsan\\Fractal_The_Koch_curve_______0000.bmp";
	bool sign_this = 1;
	bool display = 1;
	bool save_image = 1;			
	bool sign_bg = 1;
	bool negative = 1;
	bool do_grey_scale = 0;

	print("http://classes.yale.edu/fractals/");
	char * string = "PixSan FRACTAL";

	unsigned long int side  = 401;
	enum {temp,  page_alphabet, page_signature, page};
	long int yo, xo, yf, xf;

	NETPTR net = NULL;
	
	net = net->create_exsan_net(page, side, side );
	net->set_work_sheet(page, side, side);

	if(save_image){	
		net->set_file_image(net, page_alphabet, file_name);		
		print("Master File: ", file_name);		
	}

	if(sign_this){	
		net->set_work_sheet(page_signature, side, side);		
	}

	unsigned long int param;

	CELLPTR ptr_a, ptr_b, ptr_c, ptr_center;

	ptr_center = net->go_to(net, page, side/2 + 1, side/2 + 1);

	do{		
		printing("\n    [0...", side/2,"] EXIT - Parameter");			
		cin >> param;
		if(!param) exit;

		ptr_a = net->Move_to(net, ptr_center, page,  0, (long)param, 1);
		ptr_b = net->Move_to(net, ptr_center, page,  (long)param*sqrt(3.)/2., -(long)param/2., 1);
		ptr_c = net->Move_to(net, ptr_center, page, -(long)param*sqrt(3.)/2., -(long)param/2., 1);

		basic_fracatal(net, page, ptr_a->get_row(),  ptr_a->get_col(), ptr_b->get_row(), ptr_b->get_col(), param+70);
		basic_fracatal(net, page, ptr_b->get_row(),  ptr_b->get_col(), ptr_c->get_row(), ptr_c->get_col(), param+70);
		basic_fracatal(net, page, ptr_c->get_row(),  ptr_c->get_col(), ptr_a->get_row(), ptr_a->get_col(), param+70);

		net->display_image(net, page, page_signature, string, file_name, sign_this, display, sign_bg, save_image, negative, do_grey_scale);

	}while(1);

	net->delete_exsan_net(net);  // freeing memory
	return;
}

void basic_fracatal(NETPTR net, unsigned long page, long yo, long xo, long yf, long xf, unsigned short tint){
	
	unsigned long int param = tint % 255;  // 255 white	
	long int xo_t, yo_t, xf_t, yf_t ;
	double angle, delta_y = yf - yo;
	double m, delta_x = xo - xf;

	bool fractal = 1;

	double side = sqrt(delta_x * delta_x + delta_y * delta_y);  //(long)

	if(side < param) return; //  exit recursive exit   10
	
	m = delta_y/delta_x;

	if(fractal)basic_fracatal(net, page, yo,  xo,  yo + (yf - yo)/3.,  xo + (xf - xo)/3, tint);	
	net->draw_line(net, page, xo , yo, xo + (xf - xo)/3., yo + (yf - yo)/3., tint);  // 1st
	xo_t = xo,                 yo_t =  yo;  
	xf_t = xo + (xf - xo)/3.,  yf_t = yo + (yf - yo)/3.; 	
	net->draw_line(net, page,  xo_t + (xf_t - xo_t)/3.,  yo_t + (yf_t - yo_t)/3., xf_t - (xf_t - xo_t)/3., yf_t - (yf_t - yo_t)/3. , 0);  // 0 - black 

	if(fractal)basic_fracatal(net, page, yf - (yf - yo)/3., xf - (xf - xo)/3., yf, xf, tint);	
	net->draw_line(net, page, xf - (xf - xo)/3., yf - (yf - yo)/3., xf, yf, tint);  // last
	xo_t = xf - (xf - xo)/3.,  yo_t = yf - (yf - yo)/3. ;  
	xf_t = xf,                 yf_t = yf ; 	
	net->draw_line(net, page,  xo_t + (xf_t - xo_t)/3.,  yo_t + (yf_t - yo_t)/3., xf_t - (xf_t - xo_t)/3., yf_t - (yf_t - yo_t)/3. , 0);  // 0 - black 

	angle = atan(m);

	if(!delta_x){ 
		if(delta_y > 0) angle -= pi/2.;  
		else   angle += pi/2.;        
	
	}
	else{
		if(!delta_y){ 
			if(delta_x < 0) { }
			else    angle -= pi;  
			           
		}
		else{
			if(delta_x > 0 && delta_y > 0 ){ angle += pi;  }
			if(delta_x > 0 && delta_y < 0 ){ angle += pi;  }
			if(delta_x < 0 && delta_y > 0 ){ angle += pipi;}
			if(delta_x < 0 && delta_y < 0 ){ /*    */      }
		}
	}
		
	angle += pi/3.;  

	delta_x =  side/3.*cos(angle);  
	delta_y = -side/3.*sin(angle);

	if(fractal)basic_fracatal(net, page, yo + (yf - yo)/3., xo + (xf - xo)/3., delta_y + yo + (yf - yo)/3., delta_x + xo + (xf - xo)/3., tint);	
	net->draw_line(net, page, xo + (xf - xo)/3., yo + (yf - yo)/3., delta_x + xo + (xf - xo)/3.,  delta_y + yo + (yf - yo)/3., tint);  
	xo_t = xo + (xf - xo)/3.,             yo_t =  yo + (yf - yo)/3.;  
	xf_t = delta_x + xo + (xf - xo)/3.,   yf_t =  delta_y + yo + (yf - yo)/3.; 
	net->draw_line(net, page,  xo_t + (xf_t - xo_t)/3.,  yo_t + (yf_t - yo_t)/3., xf_t - (xf_t - xo_t)/3., yf_t - (yf_t - yo_t)/3. , 0);  


	if(fractal)basic_fracatal(net, page, delta_y + yo + (yf - yo)/3., delta_x + xo + (xf - xo)/3.,  yf - (yf - yo)/3., xf - (xf - xo)/3., tint);	
	net->draw_line(net, page,   delta_x + xo + (xf - xo)/3., delta_y + yo + (yf - yo)/3., xf - (xf - xo)/3., yf - (yf - yo)/3. , tint);  
	xo_t = delta_x + xo + (xf - xo)/3.,  yo_t = delta_y + yo + (yf - yo)/3.;  
	xf_t = xf - (xf - xo)/3.,            yf_t = yf - (yf - yo)/3.; 
	net->draw_line(net, page,  xo_t + (xf_t - xo_t)/3.,  yo_t + (yf_t - yo_t)/3., xf_t - (xf_t - xo_t)/3., yf_t - (yf_t - yo_t)/3. , 0);  // 0 black 

	return;
}
/***********END*****************/
 

Attachments

  • Fractal_The_Koch_curve_nznq_00002.JPG
    Fractal_The_Koch_curve_nznq_00002.JPG
    28.5 KB · Views: 63
  • Fractal_The_Koch_curve_nznq_00007.JPG
    Fractal_The_Koch_curve_nznq_00007.JPG
    37.1 KB · Views: 50
  • ExSan4.00.D FRACTAL.zip
    2.1 MB · Views: 32
More Fractals, or the beauty of recursivity

From the Attached Zip File extract program FRACTAL_Basics to your desktop and execute.
It will display the Elemental Basic Fractals

Actual Code
C++:
/***********START***************/
void basic_fractal(void){
	
	double theta, angle, shift, delta, x, y, xx, yy;
	
	unsigned long int side  = 501; 
	
	enum {temp, page_alphabet, page_signature, page_fractal};
	long int yo, xo, yf, xf;

	GRID myGrid;

	CELL center_ptr;
	
	myGrid = myGrid->create_exsan_net(page_fractal, side, side);
	myGrid->set_work_sheet(page_fractal, side, side);

	unsigned long int  radius = side/4;
	unsigned short radii_limit,  tint = 255;  // white id --> 255

	center_ptr = myGrid->go_to(myGrid, page_fractal, myGrid->get_rows_in_page(page_fractal)/2., myGrid->get_cols_in_page(page_fractal)/2.);

	do{
		myGrid->set_page_to_data(myGrid, page_fractal, 0);  // reset frame to black ---> 0 

		cin >> side; 
		cin >> radii_limit;
		cin >> angle;
	
		shift = double (angle)*pi/180.; 
		delta = pipi/(double)side;  // pipi = 2*pi

		for(theta = shift; theta < pipi + shift; theta += delta){
		
			xx = radius*cos(theta). 			 yy = radius*sin(theta); 
			x  = center_ptr->get_col() + xx,      y = center_ptr->get_row() + yy;

			myGrid->draw_line(myGrid, page_fractal, center_ptr->get_col(), center_ptr->get_row(), center_ptr->get_col() + xx, center_ptr->get_row() + yy, tint);

			basic_fractal_recursive(myGrid,  page_fractal, center_ptr->get_col() + xx, center_ptr->get_row() + yy, shift, delta, radius/2.,  tint , radii_limit );
		}

		myGrid->display_image(myGrid, page_fractal); 

		print("One more time");
	}while(get_yes_no());

	myGrid->delete_exsan_net(myGrid);  // freeing memory

	return;
}


void basic_fractal_recursive(GRID net, unsigned short page, unsigned long int x, unsigned long int y, 
							 double shift, double delta, unsigned long int radius,  
							 unsigned short tint, unsigned short radii_limit){

	double theta, xx, yy;	
	unsigned long int center_x = x;
	unsigned long int center_y = y;

	if(radius <=  radii_limit)  return; // exit recursive
	
	for(theta = shift; theta < pipi + shift; theta += delta){
		
		xx = radius*cos(theta), yy = radius*sin(theta); 
		xx +=  center_x,        yy +=  center_y;

		basic_fractal_recursive(net, page, xx , yy , shift, delta, radius/2., tint - 3 , radii_limit ); // fading

		net->draw_line(net, page, center_x, center_y, xx , yy , 255);
	}

	return;
}
/****ENDS  basic_fractal(void) ***************/
 

Attachments

  • basic_fractal_ivwx_00005.JPG
    basic_fractal_ivwx_00005.JPG
    39 KB · Views: 11
  • basic_fractal_ivwx_00004.JPG
    basic_fractal_ivwx_00004.JPG
    56.6 KB · Views: 14
  • basic_fractal_ivwx_00003.JPG
    basic_fractal_ivwx_00003.JPG
    50.3 KB · Views: 14
  • basic_fractal_ivwx_00002.JPG
    basic_fractal_ivwx_00002.JPG
    38.1 KB · Views: 15
  • basic_fractal_ivwx_00001.JPG
    basic_fractal_ivwx_00001.JPG
    26.1 KB · Views: 16
  • basic_fractal_ivwx_00006.JPG
    basic_fractal_ivwx_00006.JPG
    78.5 KB · Views: 16
  • basic_fractal_ivwx_00007.JPG
    basic_fractal_ivwx_00007.JPG
    57.5 KB · Views: 13
  • basic_fractal_ivwx_00008.JPG
    basic_fractal_ivwx_00008.JPG
    72.3 KB · Views: 19
  • ExSan4.00.E FRACTAL_Basics.zip
    2 MB · Views: 6
Hi Alain,
Sorry it took me a while to get back to this.

The ACTUAL code is attached, it uses ExSan as platform in the background which significantly simplifies the coding.

It may look not exactly as the actual code or maybe as a pseudo-code, but it is the compiled code in fact.

I will try to depict the algorithm a little bit.

The calling function: void basic_fractal(void), prepares the scenario,

at the beginning it sets the dimension of the frame/window-image 501*501

myGrid = myGrid->create_exsan_net(page_fractal, side, side);
creates an object myGrid -think of it as an excel structure -
it has n worksheets--> page_fractal, in this case 3, all pages have 501 rows*501 cols

USER basically provides the pivot number side,
then the for loop will draw n lines = side centered on the drawing frame/window

for(...){ // n lines with a common edge centered
.....
myGrid->draw_line(...);
basic_fractal_recursive( ... );
}

right away after a line is drawn the function void basic_fractal_recursive( ...) is called

this is a sort of replica of the calling function as you can see

there are only 2 basic differences:

- this function calls to itself, and the statement
- if(radius <= radii_limit) return; // exit recursive

sets the limit and forces it to return and not to overload the stack.

hope this helps.
 
What is the ExSan platform?

It began as a hobby, years ago +12 , at this point it is more mature, it is not comercially available but it may be in the future :tiphat:

ExSan is a spreadsheet tool built on a novel data structure, coded in C/C++, console based and capable of handling large matrices, for instance in Image Processing tasks. It is used for a variety of applications including Pattern Recognition, Artificial Intelligence, Financial Derivatives Pricing and more general Numerical Computing.
 
From the Attached Zip File extract program FIREWORK_FRACTAL to your desktop and execute. It will display the Fireworks Fractals

For the attached images, input parameters:
0 Exit - [3..12] Parameter ---> 7
0 Exit - [60...120] Radii Recursive Limit ---> 99

This version compiles all previus Fractals
Actual Code

C++:
/***********START***************/
void recursive_pixsan_set(GRID net, unsigned short page, double radius_param, unsigned long int yy, unsigned xx, short int radii_limit){

	unsigned long int side = (unsigned long int) radius_param / 2; 

	double delta = pi/18.;  
	double radii = side;
	double theta, x, y;

	CELL myCell = NULL;

	for(unsigned short int radius = radii/2; radius <= radii; radius += 1){
		for(theta = 0; theta < pipi; theta += delta){

			x = (0.5*cos(theta) - 0.25*cos(2*theta))*radius;		
			y = (0.5*sin(theta) - 0.25*sin(2*theta))*radius;

			if(y+yy < 1 || x+xx < 1 || y+yy > net->get_rows_in_page(page) || x+xx > net->get_cols_in_page(page)) break;

			myCell = net->go_to(net, page, y + yy, x + xx );

			recursive_pixsan_set(net, page, sqrt(x*x + y*y), (unsigned long int)y +xx, (unsigned long int)x + yy, radii_limit);
			
		}
	}
	return;
}
/***********END*****************/


/***********START***************/
void pixsan_set(bool random){
	
	GRID myGrid = NULL;
	CELL center_cell = NULL;
	CELL ptr = NULL; 

	unsigned long int side  = 651;
        unsigned long int page_fractal = 1;

	myGrid = myGrid->create_exsan_net(page_fractal, side, side);

	short int param, radii_limit;

	do{
		myGrid->set_page_to_data(myGrid, page_fractal, 0);  // reset frame to black

		cin >> param;
		cin >> radii_limit;

		double delta = pi/double(param);  
		double radii = side/2;
		double theta, x, y;

		for(unsigned short int radius = radii; radius <= radii; radius += 1){
			for(theta = 0; theta < pipi; theta += delta){

				x = (0.5*cos(theta) - 0.25*cos(2*theta))*radius + side/2 + 1;		
				y = (0.5*sin(theta) - 0.25*sin(2*theta))*radius + side/2 + 1;

				center_cell = myGrid->go_to(myGrid, page_fractal, y, x);

				recursive_pixsan_set(myGrid, page_fractal, sqrt(x*x + y*y), (unsigned long int)y, (unsigned long int)x, radii_limit);		
			}
		}

		myGrid->display_image(myGrid, page_fractal);  //
		
		print("One more time");
	}while(get_yes_no());

	myGrid->delete_exsan_net(myGrid);  //   memory release
	return;
}
/***********END*****************/
 

Attachments

  • ExSan4.00.J_Firework_Fractal.zip
    2.6 MB · Views: 2
  • PixSan_Fireworks_Fractals_00001a.JPG
    PixSan_Fireworks_Fractals_00001a.JPG
    89 KB · Views: 7
  • PixSan_Fireworks_Fractals_00002b.JPG
    PixSan_Fireworks_Fractals_00002b.JPG
    85.5 KB · Views: 7
Top