File:Square tile fractal.png

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search

Original file(1,000 × 1,000 pixels, file size: 6 KB, MIME type: image/png)

Captions

Captions

Square tile fractal

Summary[edit]

Description
English: Square tile fractal. Based upon work by Santiago Zubieta. See Square tile fractal. WWW page written by Paul Bourke January 2013[1]
Date
Source Own work
Author Adam majewski
Other versions

Licensing[edit]

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.

c++ source code[edit]

// based on the code by Vort
// https://commons.wikimedia.org/wiki/File:XOR_texture.png
// 

#include <fstream>

int main()
{
	std::ofstream file;
	file.open("x1.ppm");
	int jMax = 1000;
	int iMax = 1000;
	file << "P2 " << jMax << ' ' << iMax <<" 255\n";
	for (int j = 0; j < jMax; j++)
		for (int i = 0; i < iMax; i++){
			int g = ( i & (j - 2*(i^j) + j) & i) % 255;
			file << g << ' ';
			}
	file.close();
	return 0;
}


 g++ d.cpp -Wall
 ./a.out

c source code[edit]

/*




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

  http://paulbourke.net/fractals/squaretile/
   
   
   
  ==========================================

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



  c console progam 
  
  


  gcc d.c -Wall -march=native 


  time ./a.out

  time ./a.out >i.txt
  time ./a.out >e.txt
  
  
  
  
  
  
  convert -limit memory 1000mb -limit disk 1gb dd30010000_20_3_0.90.pgm -resize 2000x2000 10.png

  
  
  
*/

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



// https://sourceforge.net/p/predef/wiki/Standards/

#if defined(__STDC__)
#define PREDEF_STANDARD_C_1989
#if defined(__STDC_VERSION__)
#if (__STDC_VERSION__ >= 199409L)
#define PREDEF_STANDARD_C_1994
#endif
#if (__STDC_VERSION__ >= 199901L)
#define PREDEF_STANDARD_C_1999
#endif
#endif
#endif




/* --------------------------------- 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 long long int iSize;	// = iWidth*iHeight; 

// memory 1D array 
unsigned char *data;


// 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






double  DisplayAspectRatio  = 1.0; // https://en.wikipedia.org/wiki/Aspect_ratio_(image)


















// ****************** ****************************


/* -----------  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;
}



// 
unsigned char
ComputeColor (int i , int j)
{

  /*
    Square tile fractal
    Written by Paul Bourke
    January 2013
    Based upon work by Santiago Zubieta

    The tiles below are based upon the value of the following expression modulo some integer (M) which turns out to be the repeating width of the tile.

    i & (j - 2 * (i^j) + j) & i
    Where (i,j) is the index of the pixel in question, and "&" and "^" are bit-wise AND and XOR respectively. If M is a power of 2 then the tile is complete.


	
    http://paulbourke.net/fractals/squaretile/
    Original code snippet from Santiago Zubieta follows:	
	
    int x = i & (j - 2*(i^j) + j) & i;
    x %= 256;
    x = Math.abs(x);
    g.setColor(new Color(x,x,x));
         
         
	
  
  */

  int x = i & (j - 2*(i^j) + j) & i;
  unsigned char g  = x % 256;
	
	

  return g ;


}





// plots raster point (ix,iy) 
int
DrawPoint (unsigned char A[], int ix, int iy)
{
  int i;			/* index of 1D array */
  unsigned char iColor = 0;
  


  
  
  iColor = ComputeColor (ix, iy);
  i = Give_i (ix, iy);		/* compute index of 1D array from indices of 2D array */
  A[i] = iColor;		//

  return 0;
}




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

  fprintf (stdout, "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)
    {
      fprintf (stderr, " %d from %d \r", iy, iyMax);	//info 
      for (ix = ixMin; ix <= ixMax; ++ix)
	DrawPoint (A, ix, iy);	//  
    }

  return 0;
}


//=========




























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

int
SaveArray2PGMFile (unsigned char A[], int a, int b , 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, "%d_%d", a, b );	/*  */
  char *filename = strcat (name, ".pgm");
  char long_comment[200];
  sprintf (long_comment, "aa  %s", comment);





  // save image array 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", long_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 (long_comment == NULL || strlen (long_comment) == 0)
    printf ("\n");
  else
    printf (". Comment = %s \n", long_comment);

  return 0;
}




int
PrintCInfo ()
{

  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 displayed in the console : export  OMP_DISPLAY_ENV="TRUE"

  printf ("__STDC__ = %d\n", __STDC__);
  printf ("__STDC_VERSION__ = %ld\n", __STDC_VERSION__);
  printf ("c dialect = ");
  switch (__STDC_VERSION__)
    {				// the format YYYYMM 
    case 199409L:
      printf ("C94\n");
      break;
    case 199901L:
      printf ("C99\n");
      break;
    case 201112L:
      printf ("C11\n");
      break;
    case 201710L:
      printf ("C18\n");
      break;
      //default : /* Optional */

    }

  return 0;
}


int
PrintProgramInfo ()
{


  // display info messages
  printf (" \n");
  //printf ("iPeriodParent = %d \n", iPeriodParent);
  //printf ("iPeriodOfChild  = %d \n", iPeriodChild);
  
 

  printf("pixel counters\n");

  printf ("all pixels of the array = iSize = %llu\n", iSize);


  // image corners in world coordinate
  // center and radius
  // center and zoom
  // GradientRepetition
  
  printf ("ratio of image  = %f \n", DisplayAspectRatio);
  //




  return 0;
}




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

int
setup ()
{

  fprintf (stderr, "setup start\n");






  /* 2D array ranges */

  iWidth = iHeight* DisplayAspectRatio ;
  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].


  
  
  
  	
  
  



  /* create dynamic 1D arrays for colors ( shades of gray ) */
  data = malloc (iSize * sizeof (unsigned char));

  
  if (data == NULL )
    {
      fprintf (stderr, " Could not allocate memory");
      return 1;
    }





 


  fprintf (stderr, " end of setup \n");

  return 0;

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




int
end ()
{


  fprintf (stderr, " allways free memory (deallocate )  to avoid memory leaks \n");	// https://en.wikipedia.org/wiki/C_dynamic_memory_allocation
  free (data);
  


  PrintProgramInfo ();
  PrintCInfo ();
  return 0;

}

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

int
main ()
{
  setup ();


  DrawImage (data);	// first find Fatou
  SaveArray2PGMFile (data, iWidth,  0, "");
  
  
  
  
  	

  end ();

  return 0;
}

references[edit]

  1. Square tile fractal. Written by Paul Bourke January 2013

File history

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

Date/TimeThumbnailDimensionsUserComment
current17:53, 3 August 2023Thumbnail for version as of 17:53, 3 August 20231,000 × 1,000 (6 KB)Obscure2020 (talk | contribs)Optimized with OxiPNG and ZopfliPNG.
13:36, 14 October 2020Thumbnail for version as of 13:36, 14 October 20201,000 × 1,000 (7 KB)Soul windsurfer (talk | contribs)Uploaded own work with UploadWizard

The following 2 pages use this file: