This is a file from the Wikimedia Commons

File:Julia set for f(z) = z^2+0.355534 -0.337292*i.png

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Original file(2,000 × 2,000 pixels, file size: 3.28 MB, MIME type: image/png)

Summary

Description
English: Julia set for f(z) = z^2+0.355534 -0.337292*i. it is inside the period 85 componnet with center c = 0.3555471250217805 -0.3372858052155804*i. Adress 1-(4/5)->5-(1/17)-> 85, Location by ‎Chris Thomasson‎.
Date
Source Own work
Author Adam majewski
Other versions

steps

How to find c value on the parameter plane ?

  • center ( nucleus) of period 1 component is c = 0.0
  • go along internal ray 4/5 to the bifurcation point between period 1 and 5 components
  • bifurcation point ( root point = bond) is c = 0.379513588015924 -0.334932305597498 i
  • go along internal ray 0 to the center of period 5 component c = 0.379513588015924 -0.334932305597498 i
  • go along internal ray 1/17 to bifurcation point
  • bifurcation pointis aprox c = 0.355630631309901 -0.337285118827790 i ( real: 3.5563081682876507e-01 imag: -3.3729232913917928e-01
  • go along internal ray 0 to the center
  • center of period 85 componnet c = 0.355534 -0.337292*i

Tips for making better images by Paul Bourke

  • do the fractal creation not in 8 bit, but use 16 bit or floating point for each pixel ( pfm file, kfb and exr ( OpenEXR) from Kalles Fraktaler or array with floating point values )
  • apply antialiasing by supersampling each final pixel ( render a 30,000 pixel version of the image )
  • do all colour external to the fractal creation using gradient maps...allows you to make appearance decisions independent to the creation

Summary

coefficients read from input file thomasson85.txt
	degree 2 coefficient = ( 1.0000000000000000 +0.0000000000000000*i) 
	degree 0 coefficient = ( 0.3555340000000000 -0.3372920000000000*i) 

Input polynomial p(z)=(1+0i)*z^2+(0.35553400000000001668-0.33729199999999998072i)

1 critical points found

	cp#0: 0,0 . It's critical orbit is bounded and enters cycle #0 length=85 and it's stability = |multiplier|=0.1738 =attractive 
cycle = {
0.085363751140101362669,-0.13328474304205845113 ; 0.34505614728092159904,-0.36004737127162922405 ; 0.34496363521674267005,-0.58576511753922388959 ; 0.13141313669620913185,-0.74142732865898641759 ; -0.17691107118606408388,-0.53215858178273711676 ; 0.10363877094318652006,-0.14900251051191856777 ; 0.34407324670375988296,-0.36817687411380894957 ; 0.33836618846505089886,-0.59065162487515943113 ; 0.12115633552870030964,-0.73700507803939352414 ; -0.17296362741710957778,-0.51587766904259346745 ; 0.11932064699246511252,-0.15883585411781978025 ; 0.3445425882453631572,-0.37519679375787812203 ; 0.33347096106862217368,-0.59583454884540199004 ; 0.11171807227822680431,-0.73467903928273026182 ; -0.171738363087832413,-0.50144585202377256472 ; 0.13358012284424097826,-0.16505702039250771951 ; 0.34613382923822982873,-0.38138867412067112284 ; 0.32988530699559659443,-0.60131504440295846514 ; 0.10277853314624713077,-0.73402199604788154197 ; -0.17269086380642195699,-0.48817540810176329735 ; 0.14704090536688504964,-0.16868513417170785673 ; 0.34870035336058619047,-0.38689922970108481248 ; 0.32743492249050487608,-0.60711579622341371731 ; 0.094158038442373359622,-0.73487382735834927416 ; -0.17563980593299344246,-0.47568055617340299079 ; 0.16011134990674161238,-0.17019511885521021455 ; 0.3522032658868199384,-0.39179234045489208516 ; 0.32607990246221990605,-0.61327308371530764308 ; 0.085758227580088108866,-0.73724405464118469844 ; -0.18064032250609596275,-0.46374148683997123976 ; 0.17310875949855913936,-0.1697511765155422514 ; 0.35668518068671922761,-0.3960628311800529211 ; 0.32589255187915844303,-0.61983148500550155902 ; 0.077548885566184666551,-0.74128892876698238013 ; -0.18796144625994304356,-0.45226426061686064717 ; 0.18632054384881382991,-0.16727551096554213461 ; 0.36226824849133454354,-0.39962572835137605276 ; 0.3270715611046115523,-0.62683542532385372326 ; 0.06958715564247108798,-0.74733208223269198545 ; -0.19812886890384162619,-0.44130142784587667037 ; 0.20004209847430617919,-0.16242289451049232718 ; 0.3691696445008375016,-0.40227483331629948493 ; 0.32999518490121820813,-0.63430731441402388171 ; 0.062085052938857854254,-0.7559287190085018926 ; -0.21203967442341375982,-0.43115574907529163884 ; 0.2145995435689114772,-0.15444775068058413936 ; 0.37773285640969328503,-0.40358083360259677796 ; 0.33533862156005905808,-0.64218348213782805445 ; 0.055586366379134388715,-0.76798984737747610918 ; -0.23118456154764366328,-0.42267153006355973055 ; 0.23032907917170558632,-0.14186173528716850001 ; 0.38846073277339915331,-0.40264176571678755856 ; 0.34431534940731378658,-0.65011303071103720974 ; 0.051440107137190604902,-0.78497979064703704566 ; -0.25801318710198040129,-0.41805088906282605876 ; 0.24733825887230226348,-0.12156671548416758677 ; 0.40193174798837350004,-0.3974281994893570924 ; 0.35913395629163719081,-0.65677002184125865547 ; 0.053164336972316261765,-0.80902883263519242973 ; -0.29616720530935625177,-0.42331496295707404798 ; 0.26405345563740539871,-0.086547980910739397142 ; 0.41776767443432955007,-0.38299858687584187322 ; 0.38337591225337619427,-0.65730085790150993841 ; 0.070466672298047483558,-0.84127863204583630541 ; -0.34725018483215319698,-0.45585621135144721583 ; 0.26831180543826926854,-0.020699692702649707954 ; 0.42709674765955951692,-0.34839994384213057721 ; 0.41656311099217374627,-0.6348929657994942799 ; 0.12596974741780025964,-0.86623797796097012913 ; -0.37896585719740571996,-0.55553155857489888714 ; 0.19053380834870822791,0.083762986591094767963 ; 0.38482089420120235701,-0.30537263833227384913 ; 0.41036867237179930346,-0.57231954349521196868 ; 0.19638678739762535641,-0.80701602247312875793 ; -0.25717309026398937899,-0.65426656806381511267 ; -0.0063927437300727585345,-0.00077248966922782713596 ; 0.35557427043210931839,-0.33728212334302098974 ; 0.36820783106654997319,-0.5771496898749748361 ; 0.15800924233595339818,-0.76231407101919290703 ; -0.20062182221027302642,-0.57819733756755764542 ; 0.061470954376758202287,-0.10529399308013645542 ; 0.34822585325322968908,-0.35023704448955150781 ; 0.35412925754116375554,-0.58121518731652688228 ; 0.14313043707927045056,-0.74894260551210012355 ; -0.18489470433275023709,-0.55168496494846896994 ; }


old c src code

/*

https://www.facebook.com/photo.php?fbid=111908386634835&set=a.110008616824812&type=3&theater

center of period 85 componnet. Adress 1->5 -> 85
4K antialiased version. Max iterations 5000, and it was reached!

Paul Bourke I rendered a 30,000 pixel version

  Adam Majewski
  adammaj1 aaattt o2 dot pl  // o like oxygen not 0 like zero 
  
  
  console program in c programing language 
  
  ==============================================
  
  
  Structure of a program or how to analyze the program 
  
  
  ============== Image X ========================
  
  DrawImageOfX -> DrawPointOfX -> ComputeColorOfX -> IsInTheYTrap
  
  first 2 functions are identical for every X
  check only last function =  ComputeColorOfX
  which computes color of one pixel !
  
  

   
  ==========================================

  -------------------------------
  cd existing_folder
  git init
  git remote add origin git@gitlab.com:adammajewski/SepalsOfCauliflower.git
  git add .
  git commit
  git push -u origin master
  ---------------------------------
  indent d.c 
  default is gnu style 
  -------------------



  c console progam 
  
	export  OMP_DISPLAY_ENV="TRUE"	
  	gcc d.c -lm -Wall -march=native -fopenmp
  	time ./a.out > b.txt


  gcc d.c -lm -Wall -march=native -fopenmp


  time ./a.out

  time ./a.out >a.txt

  ----------------------
  
  a@zelman:~/c/julia/demj/85$ gcc d.c -lm -Wall -march=native -fopenmp
a@zelman:~/c/julia/demj/85$ time ./a.out
setup start
 end of setup 
File 30001.0000000000.pgm saved . Comment = DEM/J 
 allways free memory (deallocate )  to avoid memory leaks 
Numerical approximation of Julia set for fc(z)= z^2 + c 
parameter c = ( 0.3555340000000000 ; -0.3372920000000000 ) 
Image Width = 2.400000 in world coordinate
PixelWidth = 0.000080 
distanceMax = 0.0000800026667556
Maximal number of iterations = iterMax = 50000 
ratio of image  = 1.000000 ; it should be 1.000 ...
gcc version: 7.3.0

real	2021m48,597s
user	16100m25,607s
sys	1m52,759s

  

*/

#include <stdio.h>
#include <stdlib.h>		// malloc
#include <string.h>		// strcat
#include <math.h>		// M_PI; needs -lm also
#include <complex.h>
#include <omp.h>	// OpenMP

/* --------------------------------- global variables and consts ------------------------------------------------------------ */



// virtual 2D array and integer ( screen) coordinate
// Indexes of array starts from 0 not 1 
//unsigned int ix, iy; // var
static unsigned int ixMin = 0;	// Indexes of array starts from 0 not 1
static unsigned int ixMax;	//
static unsigned int iWidth;	// horizontal dimension of array

static unsigned int iyMin = 0;	// Indexes of array starts from 0 not 1
static unsigned int iyMax;	//

static unsigned int iHeight = 30000;	//  
// The size of array has to be a positive constant integer 
static unsigned int iSize;	// = iWidth*iHeight; 

// memmory 1D array 
unsigned char *data;
unsigned char *edge;

// unsigned int i; // var = index of 1D array
//static unsigned int iMin = 0; // Indexes of array starts from 0 not 1
static unsigned int iMax;	// = i2Dsize-1  = 
// The size of array has to be a positive constant integer 
// unsigned int i1Dsize ; // = i2Dsize  = (iMax -iMin + 1) =  ;  1D array with the same size as 2D array


static const double ZxMin = -1.2;	//-0.05;
static const double ZxMax =  1.2;	//0.75;
static const double ZyMin = -1.2;	//-0.1;
static const double ZyMax =  1.2;	//0.7;
static double PixelWidth;	// =(ZxMax-ZxMin)/ixMax;
static double PixelHeight;	// =(ZyMax-ZyMin)/iyMax;
static double ratio;


// complex numbers of parametr plane 
double complex c;		// parameter of function fc(z)=z^2 + c


static unsigned long int iterMax = 50000;	//iHeight*100;

static double ER = 1e60;		// EscapeRadius for bailout test  , minimal =  2.0




double distanceMax;


//double D2MaxGlobal;	//= 0.0497920256372717 ;
//double DistanceMaxGlobal2  ;

/* colors = shades of gray from 0 to 255 */

static unsigned char iColorOfExterior = 250;
static unsigned char iColorOfInterior = 255;

unsigned char iColorOfBoundary = 0;


double BoundaryWidth = 1.0; //  distanceMax = BoundaryWidth*PixelWidth; // here boundary is changing with resolution, maybe % of ImageWidth would be better ? 



/* ------------------------------------------ functions -------------------------------------------------------------*/





//------------------complex numbers -----------------------------------------------------



// fast cabs
double cabs2(complex double z) {
  return (creal(z) * creal(z) + cimag(z) * cimag(z));
}



// from screen to world coordinate ; linear mapping
// uses global cons
double
GiveZx ( int ix)
{
  return (ZxMin + ix * PixelWidth);
}

// uses globaal cons
double GiveZy (int iy) {
  return (ZyMax - iy * PixelHeight);
}				// reverse y axis


complex double GiveZ( int ix, int iy){
  double Zx = GiveZx(ix);
  double Zy = GiveZy(iy);
	
  return Zx + Zy*I;
	
	


}




// ****************** DYNAMICS = trap tests ( target sets) ****************************



// bailout test
// z escapes when 
// abs(z)> ER or cabs2(z)> ER2 
// https://en.wikibooks.org/wiki/Fractals/Iterations_in_the_complex_plane/Julia_set#Boolean_Escape_time

int Escapes(complex double z){
 // here target set (trap) is the exterior  circle with radsius = ER ( EscapeRadius) 
  // with ceter = origin z= 0
  // on the Riemann sphere it is a circle with point at infinity as a center  
   
  if (cabs(z)>ER) return 1;
  return 0;
}








/* -----------  array functions = drawing -------------- */

/* gives position of 2D point (ix,iy) in 1D array  ; uses also global variable iWidth */
unsigned int Give_i (unsigned int ix, unsigned int iy)
{
  return ix + iy * iWidth;
}


// ***********************************************************************************************
// ********************** edge detection usung Sobel filter ***************************************
// ***************************************************************************************************

// from Source to Destination
int ComputeBoundaries(unsigned char S[], unsigned char D[])
{
 
  unsigned int iX,iY; /* indices of 2D virtual array (image) = integer coordinate */
  unsigned int i; /* index of 1D array  */
  /* sobel filter */
  unsigned char G, Gh, Gv; 
  // boundaries are in D  array ( global var )
 
  // clear D array
  memset(D, iColorOfExterior, iSize*sizeof(*D)); // for heap-allocated arrays, where N is the number of elements = FillArrayWithColor(D , iColorOfExterior);
 
  // printf(" find boundaries in S array using  Sobel filter\n");   
#pragma omp parallel for schedule(dynamic) private(i,iY,iX,Gv,Gh,G) shared(iyMax,ixMax)
  for(iY=1;iY<iyMax-1;++iY){ 
    for(iX=1;iX<ixMax-1;++iX){ 
      Gv= S[Give_i(iX-1,iY+1)] + 2*S[Give_i(iX,iY+1)] + S[Give_i(iX-1,iY+1)] - S[Give_i(iX-1,iY-1)] - 2*S[Give_i(iX-1,iY)] - S[Give_i(iX+1,iY-1)];
      Gh= S[Give_i(iX+1,iY+1)] + 2*S[Give_i(iX+1,iY)] + S[Give_i(iX-1,iY-1)] - S[Give_i(iX+1,iY-1)] - 2*S[Give_i(iX-1,iY)] - S[Give_i(iX-1,iY-1)];
      G = sqrt(Gh*Gh + Gv*Gv);
      i= Give_i(iX,iY); /* compute index of 1D array from indices of 2D array */
      if (G==0) {D[i]=255;} /* background */
      else {D[i]=0;}  /* boundary */
    }
  }
 
   
 
  return 0;
}



// copy from Source to Destination
int CopyBoundaries(unsigned char S[],  unsigned char D[])
{
 
  unsigned int iX,iY; /* indices of 2D virtual array (image) = integer coordinate */
  unsigned int i; /* index of 1D array  */
 
 
  //printf("copy boundaries from S array to D array \n");
  for(iY=1;iY<iyMax-1;++iY)
    for(iX=1;iX<ixMax-1;++iX)
      {i= Give_i(iX,iY); if (S[i]==0) D[i]=0;}
 
 
 
  return 0;
}





// ***************************************************************************************************************************
// ************************** DEM/J*****************************************
// ****************************************************************************************************************************

unsigned char ComputeColorOfDEMJ(complex double z){
// https://en.wikibooks.org/wiki/Fractals/Iterations_in_the_complex_plane/Julia_set#DEM.2FJ


  int nMax = iterMax;
  complex double dz = 1.0; //  is first derivative with respect to z.
  double distance;
  double cabsz;
	
  int n;

  for (n=0; n < nMax; n++){ //forward iteration

    if (cabs(z)> ER || cabs(dz)> 1e60) break; // big values
    
  			
    dz = 2.0*z * dz; 
    z = z*z +c ; /* forward iteration : complex quadratic polynomial */ 
  }
  
  if (n==nMax) return iColorOfInterior;
  cabsz = cabs(z);
  distance = 2.0 * cabsz* log(cabsz)/ cabs(dz);
  if (distance <distanceMax) {//printf(" distance = %f \n", distance); 
  		return iColorOfBoundary;}
  
  
  return iColorOfExterior;
}



// plots raster point (ix,iy) 
int DrawPointOfDEMJ (unsigned char A[], int ix, int iy)
{
  int i;			/* index of 1D array */
  unsigned char iColor;
  complex double z;


  i = Give_i (ix, iy);		/* compute index of 1D array from indices of 2D array */
  z = GiveZ(ix,iy);
  iColor = ComputeColorOfDEMJ(z);
  A[i] = iColor ;		// interior
  
  return 0;
}




// fill array 
// uses global var :  ...
// scanning complex plane 
int DrawImagerOfDEMJ (unsigned char A[])
{
  int ix, iy;		// pixel coordinate 

  	//printf("compute image \n");
 	// for all pixels of image 
	#pragma omp parallel for schedule(dynamic) private(ix,iy) shared(A, ixMax , iyMax)
  	for (iy = iyMin; iy <= iyMax; ++iy){
    		printf (" %d from %d \r", iy, iyMax);	//info 
    		for (ix = ixMin; ix <= ixMax; ++ix)
      			DrawPointOfDEMJ(A, ix, iy);	//  
  }

  return 0;
}









// *******************************************************************************************
// ********************************** save A array to pgm file ****************************
// *********************************************************************************************

int SaveArray2PGMFile( unsigned char A[], double k, char* comment )
{
  
  FILE * fp;
  const unsigned int MaxColorComponentValue=255; /* color component is coded from 0 to 255 ;  it is 8 bit color file */
  char name [100]; /* name of file */
  snprintf(name, sizeof name, "%.10f", k); /*  */
  char *filename =strncat(name,".pgm", 4);
  
  
  
  // save image to the pgm file 
  fp= fopen(filename,"wb"); // create new file,give it a name and open it in binary mode 
  fprintf(fp,"P5\n # %s\n %u %u\n %u\n", comment, iWidth, iHeight, MaxColorComponentValue);  // write header to the file
  fwrite(A,iSize,1,fp);  // write array with image data bytes to the file in one step 
  fclose(fp); 
  
  // info 
  printf("File %s saved ", filename);
  if (comment == NULL || strlen(comment) ==0)  
    printf("\n");
  else printf (". Comment = %s \n", comment); 

  return 0;
}




int info ()
{

  
  // display info messages
  printf ("Numerical approximation of Julia set for fc(z)= z^2 + c \n");
  //printf ("iPeriodParent = %d \n", iPeriodParent);
  //printf ("iPeriodOfChild  = %d \n", iPeriodChild);
  printf ("parameter c = ( %.16f ; %.16f ) \n", creal(c), cimag(c));
  
  printf ("Image Width = %f in world coordinate\n", ZxMax - ZxMin);
  printf ("PixelWidth = %f \n", PixelWidth);
  
  if ( distanceMax<0.0 || distanceMax > ER ) printf("bad distanceMax\n");
	printf("distanceMax = %.16f\n",  distanceMax); 
  
  // image corners in world coordinate
  // center and radius
  // center and zoom
  // GradientRepetition
  printf ("Maximal number of iterations = iterMax = %ld \n", iterMax);
  printf ("ratio of image  = %f ; it should be 1.000 ...\n", ratio);
  //
  printf("gcc version: %d.%d.%d\n",__GNUC__,__GNUC_MINOR__,__GNUC_PATCHLEVEL__); // https://stackoverflow.com/questions/20389193/how-do-i-check-my-gcc-c-compiler-version-for-my-eclipse
  // OpenMP version is diplayed in the console 
  return 0;
}


// *****************************************************************************
//;;;;;;;;;;;;;;;;;;;;;;  setup ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
// **************************************************************************************

int setup ()
{

  printf ("setup start\n");
  c = 0.355534-I*0.337292; //
  
  
  
  
  
	
  /* 2D array ranges */
  
  iWidth = iHeight;
  iSize = iWidth * iHeight;	// size = number of points in array 
  // iy
  iyMax = iHeight - 1;		// Indexes of array starts from 0 not 1 so the highest elements of an array is = array_name[size-1].
  //ix

  ixMax = iWidth - 1;

  /* 1D array ranges */
  // i1Dsize = i2Dsize; // 1D array with the same size as 2D array
  iMax = iSize - 1;		// Indexes of array starts from 0 not 1 so the highest elements of an array is = array_name[size-1].

  /* Pixel sizes */
  PixelWidth = (ZxMax - ZxMin) / ixMax;	//  ixMax = (iWidth-1)  step between pixels in world coordinate 
  PixelHeight = (ZyMax - ZyMin) / iyMax;
  ratio = ((ZxMax - ZxMin) / (ZyMax - ZyMin)) / ((float) iWidth / (float) iHeight);	// it should be 1.000 ...
	
   
	
  
  //ER2 = ER * ER; // for numerical optimisation in iteration
  
  
   	
  /* create dynamic 1D arrays for colors ( shades of gray ) */
  data = malloc (iSize * sizeof (unsigned char));
  edge = malloc (iSize * sizeof (unsigned char));
  	
  if (data == NULL || edge == NULL){
    fprintf (stderr, " Could not allocate memory");
    return 1;
  }

  
 	
  
  
  distanceMax = BoundaryWidth*PixelWidth; // here boundary is changing with resolution, maybe % of ImageWidth would be better ? 
  
  
  
  printf (" end of setup \n");
	
  return 0;

} // ;;;;;;;;;;;;;;;;;;;;;;;;; end of the setup ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;




int end(){


  printf (" allways free memory (deallocate )  to avoid memory leaks \n"); // https://en.wikipedia.org/wiki/C_dynamic_memory_allocation
  free (data);
  free(edge);
  info ();
  return 0;

}

// ********************************************************************************************************************
/* -----------------------------------------  main   -------------------------------------------------------------*/
// ********************************************************************************************************************

int main () {
  setup ();
  // ******************************** DEM/J **********************************************************
  DrawImagerOfDEMJ(data);
  SaveArray2PGMFile (data, iWidth+BoundaryWidth, "DEM/J");
  
  
  end();

  return 0;
}

new c src code

/*

https://www.facebook.com/photo.php?fbid=111908386634835&set=a.110008616824812&type=3&theater

center of period 85 componnet. Adress 1->5 -> 85
4K antialiased version. Max iterations 5000, and it was reached!

Paul Bourke I rendered a 30,000 pixel version

  Adam Majewski
  adammaj1 aaattt o2 dot pl  // o like oxygen not 0 like zero 
  
  
  console program in c programing language 
  
  ==============================================
  
  
  Structure of a program or how to analyze the program 
  
  
  ============== Image X ========================
  
  DrawImageOfX -> DrawPointOfX -> ComputeColorOfX -> IsInTheYTrap
  
  first 2 functions are identical for every X
  check only last function =  ComputeColorOfX
  which computes color of one pixel !
  
  
Vec3f(color[2], color[1], color[0]);
   
  ==========================================

  
  ---------------------------------
  indent d.c 
  default is gnu style 
  -------------------



  c console progam 
  
	export  OMP_DISPLAY_ENV="TRUE"	
  	gcc d.c -lm -Wall -march=native -fopenmp
  	time ./a.out > b.txt


  gcc d.c -lm -Wall -march=native -fopenmp


  time ./a.out

  time ./a.out >a.txt

  ----------------------
 ls -l *

=====


*/

#include <stdio.h>
#include <stdlib.h>		// malloc
#include <string.h>		// strcat
#include <math.h>		// M_PI; needs -lm also
#include <complex.h>
#include <omp.h>	// OpenMP

/* --------------------------------- global variables and consts ------------------------------------------------------------ */



// virtual 2D array and integer ( screen) coordinate
// Indexes of array starts from 0 not 1 
//unsigned int ix, iy; // var
static unsigned int ixMin = 0;	// Indexes of array starts from 0 not 1
static unsigned int ixMax;	//
static unsigned int iWidth;	// horizontal dimension of array

static unsigned int iyMin = 0;	// Indexes of array starts from 0 not 1
static unsigned int iyMax;	//

static unsigned int iHeight = 1000;	//  // The size of array has to be a positive constant integer 
static unsigned int iSize;	// = iWidth*iHeight; 
static unsigned int iSizeC;	// = iWidth*iHeight*iSizeC

int iColorSize = 3 ; // RGB = 3*(unsigned char)


// memmory 1D arrays 
double *dData; // double for dem ( double = 8 bytes per pixel)
unsigned char * ucColor; // 


// unsigned int i; // var = index of 1D array
//static unsigned int iMin = 0; // Indexes of array starts from 0 not 1
static unsigned int iMax;	// = i2Dsize-1  = 
// The size of array has to be a positive constant integer 
// unsigned int i1Dsize ; // = i2Dsize  = (iMax -iMin + 1) =  ;  1D array with the same size as 2D array


static const double ZxMin = -1.2;	//-0.05;
static const double ZxMax =  1.2;	//0.75;
static const double ZyMin = -1.2;	//-0.1;
static const double ZyMax =  1.2;	//0.7;
static double PixelWidth;	// =(ZxMax-ZxMin)/ixMax;
static double PixelHeight;	// =(ZyMax-ZyMin)/iyMax;
static double ratio;


// complex numbers of parametr plane 
double complex c;		// parameter of function fc(z)=z^2 + c


static unsigned long int iterMax = 50000;	//iHeight*100;

static double ER = 160;		// EscapeRadius for bailout test  , minimal =  2.0




//double distanceMax;
double distanceMaxImage = 0.0;
double BoundaryWidth = 0.0;

//double D2MaxGlobal;	//= 0.0497920256372717 ;
//double DistanceMaxGlobal2  ;


// sum of points = iSize;
int iExterior = 0;
int iInterior = 0;
int iBoundary = 0;
int iAll =0;
double PixelWidthRatio = 1.0; //  BoundaryWidth= PixelWidthRatio*PixelWidth; // here boundary is changing with resolution, maybe % of ImageWidth would be better ? 



/* ------------------------------------------ functions -------------------------------------------------------------*/





//------------------complex numbers -----------------------------------------------------



// fast cabs
double cabs2(complex double z) {
  return (creal(z) * creal(z) + cimag(z) * cimag(z));
}



// from screen to world coordinate ; linear mapping
// uses global cons
double
GiveZx ( int ix)
{
  return (ZxMin + ix * PixelWidth);
}

// uses global constants
double GiveZy (int iy) {
  //return (ZyMin + iy * PixelHeight);
  return (ZyMax - iy * PixelHeight);// reverse y axis
}				


complex double GiveZ( int ix, int iy){
  double Zx = GiveZx(ix);
  double Zy = GiveZy(iy);
	
  return Zx + Zy*I;
	
	


}




// ****************** DYNAMICS = trap tests ( target sets) ****************************



// bailout test
// z escapes when 
// abs(z)> ER or cabs2(z)> ER2 
// https://en.wikibooks.org/wiki/Fractals/Iterations_in_the_complex_plane/Julia_set#Boolean_Escape_time

int Escapes(complex double z){
 // here target set (trap) is the exterior  circle with radsius = ER ( EscapeRadius) 
  // with ceter = origin z= 0
  // on the Riemann sphere it is a circle with point at infinity as a center  
   
  if (cabs(z)>ER) return 1;
  return 0;
}








/* -----------  array functions = drawing -------------- */

/* gives position of 2D point (ix,iy) in 1D array  ; uses also global variable iWidth */
unsigned int Give_i (unsigned int ix, unsigned int iy)
{
  return ix + iy * iWidth;
}





// ***************************************************************************************************************************
// ************************** DEM/J*****************************************
// ****************************************************************************************************************************

double ComputeDistance(complex double z){
// https://en.wikibooks.org/wiki/Fractals/Iterations_in_the_complex_plane/Julia_set#DEM.2FJ


  int nMax = iterMax;
  complex double dz = 1.0; //  is first derivative with respect to z.
  double distance = 0.0;
  double cabsz;
	
  int n;

  // compute distance from point z to the boundary of Julia set
  for (n=0; n < nMax; n++){ //forward iteration

    if (cabs(z)> ER || cabs(dz)> 1e60) break; // big values
    
  			
    dz = 2.0*z * dz; 
    z = z*z +c ; /* forward iteration : complex quadratic polynomial */ 
  }
  
  if (n==nMax) 
  	{distance = -1.0;} // 
  	else {
  		cabsz = cabs(z);
  		distance = 2.0 * cabsz* log(cabsz)/ cabs(dz);
  		}
  	
  		
   
  return distance;
}



// compute and save  raster point (ix,iy) data
int ComputePointData (double A[], int ix, int iy)
{
  int i;			/* index of 1D array */
  double distance;
  complex double z;


  i = Give_i (ix, iy);		/* compute index of 1D array from indices of 2D array */
  z = GiveZ(ix,iy);
  distance = ComputeDistance(z);
  
  if (distance >distanceMaxImage ) distanceMaxImage = distance; // compute also max distance of the image
  A[i] = distance ;		// 
  
  return 0;
}




// fill array 
// uses global var :  ...
// scanning complex plane 
int FillDataArray (double A[])
{
  int ix, iy;		// pixel coordinate 

  	//printf("compute image \n");
 	// for all pixels of image 
	#pragma omp parallel for schedule(dynamic) private(ix,iy) shared(A, ixMax , iyMax)
  	for (iy = iyMin; iy <= iyMax; ++iy){ 
    		printf (" %d from %d \r", iy, iyMax);	//info 
    		for (ix = ixMin; ix <= ixMax; ++ix)
      			ComputePointData(A, ix, iy);	//  
  }

  return 0;
}






/* 
 normalize values in  array D 
 normalize = change range to [0,1.0] 
 using transformation : d = d/dMax 
 to do it one muust know dMax = max (D)
 uses global var :  ...
 
*/ 
int NormalizeDataArray (double D[])
{
  int i=0;		// array index 

  	//printf("compute image \n");
 	// for all pixels of image 
	#pragma omp parallel for schedule(dynamic) private(i) shared(iAll, iBoundary, iExterior, iInterior,  D, iSize)
  	for (i = 0; i < iSize; ++i){
    		D[i] = D[i]/distanceMaxImage;
  }
  
  //printf(" i = %d\n", i);
	
  return 0;
}







int SaveDataFile(double A[], double k, char* comment ){

	 FILE * fp;
  
  	char name [100]; /* name of file */
  	snprintf(name, sizeof name, "%.0f", k); /*  */
  	char *filename =strncat(name,".dat", 4);
  
  
  
  	// save image to the pgm file 
  	fp= fopen(filename,"wb"); // create new file,give it a name and open it in binary mode 
  	fwrite(A,iSize,1,fp);  // write array with image data bytes to the file in one step 
  	fclose(fp); 
  
  	// info 
  	printf("File %s saved ", filename);
  	if (comment == NULL || strlen(comment) ==0)  
    	printf("\n");
  	else printf (". Comment = %s \n", comment); 

  	return 0;

}






// *******************************************************************************************
// **********************************  pfm file ****************************
// *********************************************************************************************



/*

input :
* int i
* array D of double numbers ( distance)

output : array of rgb colors 

*/
int ComputePointColor(const int i, const double D[], unsigned char  C[] ){


	
	int iC = i*iColorSize; // compute index of F array
	// color channels from 0 to 255  
	unsigned char R;
	unsigned char G;
	unsigned char B;
	
	double Position = D[i]; // read distance form // distance/distanceMax;
	
	
	if (Position<0.0)// interior = white
		{
		  R = 255;
		  G = 255;
		  B = 255; 
		  iInterior +=1;
		} 
		else {
			if ( Position > BoundaryWidth) // exterior = color gradient
				{	Position = sqrt(Position); // Position*Position*Position;
					R = 0;  // red
					G = 255.0*Position; //x^3  = non linear gradient
					B = 0; // blue 
					iExterior +=1;
				} 
				else {	// boundary = black 
					 
					R = 0;
					G = 0;
					B = 0;
					iBoundary +=1;
					} // boundary
		} 
		
	//printf("R = %u \t G = %u \t B = %u \n", R, G, B); // info 
	// save color to the array
	C[iC] 	= R;
	C[iC+1]	= G;
	C[iC+2] = B;
	
	
	iAll +=1;
	
	
	
	
	
	
	
	return 0;

};







// fill array f using data from d array
// uses global var :  ...
int FillColorArray (double D[], unsigned char C[])
{
  int i=0;		// array index 

  	//printf("compute image \n");
 	// for all pixels of image 
	#pragma omp parallel for schedule(dynamic) private(i) shared(iAll, iBoundary, iExterior, iInterior,  D, C, iSize)
  	for (i = 0; i < iSize; ++i){
    		//printf (" %d from %d \n", i, iSize);	//info 
    		ComputePointColor(i, D, C);	//  
  }
  
  //printf(" i = %d\n", i);
	
  return 0;
}





int Z_IsInFirstQuadrant(const int ix, const int iy){

	double zx = GiveZx(ix);
	double zy = GiveZy(iy);
	int r = 0;
	
	if (zx >0.0 && zy > 0.0) { r = 1;}
	
	return r;
	
}




int InverseColor(const int i, unsigned char C[]){

	C[i]   = 255 - C[i];
	C[i+1] = 255 - C[i+1];
	C[i+2] = 255 - C[i+2];
	
	return 0;
}


// scanning complex plane 
int CheckArrayOrientation (unsigned char C[])
{
  int ix, iy;		// pixel coordinate 
  int i; 

  	//printf("compute image \n");
 	// for all pixels of image 
	//#pragma omp parallel for schedule(dynamic) private(ix,iy) shared(F, ixMax , iyMax)
  	for (iy = iyMin; iy <= iyMax; ++iy){ 
    		for (ix = ixMin; ix <= ixMax; ++ix){
      			
      			if ( Z_IsInFirstQuadrant(ix, iy)) {
      			
      				i = Give_i(ix,iy);
      				InverseColor(i*iColorSize, C);
      			
      				}
      			
      			
      			}
      				//  
  }

  return 0;
}












int Save_PPM( unsigned char Cl[], double k, char* comment )
{
  
  FILE * fp;
  
  char name [100]; /* name of file */
  snprintf(name, sizeof name, "%.0f", k); /*  */
  char *filename =strncat(name,".ppm", 4);
  
  
  
  // save image to the pgm file 
  fp= fopen(filename,"wb"); // create new file,give it a name and open it in binary mode 
  if (fp ) {
  	fprintf(fp,"P6\n%d %d\n255\n",  iWidth, iHeight);  // write header to the file
  	fwrite(Cl, iSizeC, 1,  fp);  // write array with image data bytes to the file in one step 
  	fclose(fp); 
  
  	// info 
  	printf("File %s saved ", filename);
  	if (comment == NULL || strlen(comment) ==0)  
    		printf("\n");
  		else printf ("Comment = %s \n", comment); 
	}
	else printf( "ERROR saving `%s'\n", filename);
  return 0;
}




int info ()
{

  	int iSum =  iExterior+iBoundary+iInterior;
  	
	// display info messages
  	printf ("Numerical approximation of Julia set for fc(z)= z^2 + c \n");
  	//printf ("iPeriodParent = %d \n", iPeriodParent);
  	//printf ("iPeriodOfChild  = %d \n", iPeriodChild);
  	printf ("parameter c = ( %.16f ; %.16f ) \n", creal(c), cimag(c));
  
  	printf ("Image Width = %f in world coordinate\n", ZxMax - ZxMin);
  	printf ("PixelWidth = %f \n", PixelWidth);
  
  	//if ( distanceMax<0.0 || distanceMax > ER ) printf("bad distanceMax\n");
	//printf("distanceMax = %.16f\n",  distanceMax); 
	
  	printf("distanceMaxImage = %.16f\n",  distanceMaxImage); 
  	printf("BoundaryWidth = %f = %f *distanceMaxImage = %f * PixelWidth\n" , BoundaryWidth , BoundaryWidth/((double) distanceMaxImage), PixelWidthRatio);	
	
		
  	printf("iSize = number of points in the  dData array = %d\n", iSize );
  	//
  	printf("iSizeC = number of points in the ucColor array = %d\n", iSizeC);
  	printf(" iExterior = %d \t iBoundary = %d \t iInterior = %d \t iSum = %d\n", iExterior, iBoundary,iInterior, iSum);
  	printf("iAll = %d \n", iAll);
  	if (iSum != iSize) { printf("Error : iSum != iSize \n");}
  
  	
  	printf("estimated ppm file size = %d B\n ", iSizeC*((int) sizeof(unsigned char)) +16); // header ( 16 bytes ) +  series of three 4-byte IEEE 754 single precision floating point numbers for each pixel
  
  // image corners in world coordinate
  // center and radius
  // center and zoom
  // GradientRepetition
  	printf ("Maximal number of iterations = iterMax = %ld \n", iterMax);
  	printf ("ratio of image  = %f ; it should be 1.000 ...\n", ratio);
  
  
  //
  	printf("gcc version: %d.%d.%d\n",__GNUC__,__GNUC_MINOR__,__GNUC_PATCHLEVEL__); // https://stackoverflow.com/questions/20389193/how-do-i-check-my-gcc-c-compiler-version-for-my-eclipse
  	// OpenMP version is diplayed in the console 
  	return 0;
}


// *****************************************************************************
//;;;;;;;;;;;;;;;;;;;;;;  setup ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
// **************************************************************************************

int setup ()
{

  printf ("setup start\n");
  c = 0.355534-I*0.337292; //
  
  
  
  
  
	
  /* 2D array ranges */
  
  iWidth = iHeight;// square image
  iSize = iWidth * iHeight;	// size = number of points in the dData array 
  iSizeC = iSize * iColorSize ; // size of the fRGB array
  
  
  // iy
  iyMax = iHeight - 1;		// Indexes of array starts from 0 not 1 so the highest elements of an array is = array_name[size-1].
  //ix

  ixMax = iWidth - 1;

  /* 1D array ranges */
  // i1Dsize = i2Dsize; // 1D array with the same size as 2D array
  iMax = iSize - 1;		// Indexes of array starts from 0 not 1 so the highest elements of an array is = array_name[size-1].

  /* Pixel sizes */
  PixelWidth = (ZxMax - ZxMin) / ixMax;	//  ixMax = (iWidth-1)  step between pixels in world coordinate 
  PixelHeight = (ZyMax - ZyMin) / iyMax;
  ratio = ((ZxMax - ZxMin) / (ZyMax - ZyMin)) / ((float) iWidth / (float) iHeight);	// it should be 1.000 ...
	
   
	
  
  //ER2 = ER * ER; // for numerical optimisation in iteration
  
  
   	
  /* create dynamic 1D arrays for data  ( dData) and  colors ( ucColor ) */
  dData = malloc (iSize * sizeof (double));
  ucColor = malloc (iSizeC * sizeof (unsigned char));
  	
  if (dData == NULL || ucColor == NULL){
    printf (" Could not allocate memory");
    return 1;
  }

  
 	
  
  
  BoundaryWidth =  PixelWidthRatio*PixelWidth; // here boundary is changing with resolution, maybe % of ImageWidth would be better ? 
  
  
  
  printf (" end of setup \n");
	
  return 0;

} // ;;;;;;;;;;;;;;;;;;;;;;;;; end of the setup ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;




int end(){


  printf (" allways free memory (deallocate )  to avoid memory leaks \n"); // https://en.wikipedia.org/wiki/C_dynamic_memory_allocation
  free (dData);
  free(ucColor);
  info ();
  return 0;

}

// ********************************************************************************************************************
/* -----------------------------------------  main   -------------------------------------------------------------*/
// ********************************************************************************************************************

int main () {
  setup ();
  
  //  DEM/J 
  FillDataArray(dData); // compute distasnce of each pixel and also find max distance of the image
  NormalizeDataArray(dData); // convert distance  normalized distance = distance/distanceMaxImage
  SaveDataFile(dData, iHeight+PixelWidthRatio, "") ; 
  
  // 
  FillColorArray(dData, ucColor);
  Save_PPM(ucColor, iHeight+PixelWidthRatio, "");
  //
  // CheckArrayOrientation(fRGB);
  // Save_fRGB_Array_2_PFM(fRGB, iHeight, "");
  
  
  
  end();

  return 0;
}

Licensing

I, the copyright holder of this work, hereby publish it under the following license:
w:en:Creative Commons
attribution share alike
This file is licensed under the Creative Commons Attribution-Share Alike 4.0 International license.
You are free:
  • to share – to copy, distribute and transmit the work
  • to remix – to adapt the work
Under the following conditions:
  • attribution – You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
  • share alike – If you remix, transform, or build upon the material, you must distribute your contributions under the same or compatible license as the original.

Captions

Julia set for f(z) = z^2+0.355534 -0.337292*i

Items portrayed in this file

depicts

16 March 2019

image/png

File history

Click on a date/time to view the file as it appeared at that time.

Date/TimeThumbnailDimensionsUserComment
current19:37, 24 December 2019Thumbnail for version as of 19:37, 24 December 20192,000 × 2,000 (3.28 MB)Soul windsurferbetter quality
14:47, 16 March 2019Thumbnail for version as of 14:47, 16 March 20192,000 × 2,000 (1.08 MB)Soul windsurferUser created page with UploadWizard

Metadata