Chris_M_Thomasson

Python Test Plotter For Hybrid Fractal (PIL for graphics)

Mar 30th, 2017
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.65 KB | None | 0 0
  1. # Python Test Plotter For Hybrid Fractal
  2. # https://plus.google.com/101799841244447089430/posts/Grb2inEG79b
  3.  
  4.  
  5. import math;
  6. import random;
  7. import copy; # for non-mutable aspects of classes
  8. #import os;
  9.  
  10. from PIL import Image;
  11.  
  12.  
  13. # 2d Axes
  14. class ct_axes_2d:
  15.     def __init__(self, xmin, xmax, ymin, ymax):
  16.         self.xmin = xmin;
  17.         self.xmax = xmax;
  18.         self.ymin = ymin;
  19.         self.ymax = ymax;
  20.  
  21.     def __repr__(self):
  22.         return "(%s, %s, %s, %s)" % (self.xmin, self.xmax, self.ymin, self.ymax)
  23.  
  24.     def __str__(self): return self.__repr__();
  25.  
  26.     def width(self): return  self.xmax - self.xmin;
  27.     def height(self): return  self.ymax - self.ymin;
  28.  
  29.  
  30. # 2d Plane
  31. # no aspect ratio support for this crude test... ;^o
  32. class ct_plane_2d:
  33.     def __init__(self, width, height, axes):
  34.         self.axes = copy.copy(axes);
  35.         self.width = width;
  36.         self.height = height;
  37.         self.xstep = self.axes.width() / width;
  38.         self.ystep = self.axes.height() / height;
  39.         self.aratio = height / width;
  40.  
  41.     def __repr__(self):
  42.         return "(%s, %s, %s, %s, %s, %s)" % (self.axes,
  43.                                              self.width, self.height,
  44.                                              self.xstep, self.ystep,
  45.                                              self.aratio);
  46.  
  47.     def __str__(self): return self.__repr__();
  48.  
  49.     def project_to(self, p):
  50.         x = math.floor((p.real - self.axes.xmin) / self.xstep * self.aratio);
  51.         y = math.floor((self.axes.ymax - p.imag) / self.ystep);
  52.         return complex(x, y);
  53.  
  54.     def project_xy(self, x, y):
  55.         x = self.axes.xmin + x * self.xstep;
  56.         y = self.axes.ymax - y * self.ystep;
  57.         return complex(x, y);
  58.  
  59.  
  60. # CT Hybrid Fractal Test Plotter
  61. class ct_hybrid:
  62.     def __init__(self, plane):
  63.         self.plane = copy.copy(plane);
  64.         self.image = Image.new("RGB", (plane.width, plane.height));
  65.         self.pixels = self.image.load();
  66.  
  67.     def __repr__(self):
  68.         return "(%s)" % (self.plane);
  69.  
  70.     def __str__(self): return self.__repr__();
  71.  
  72.     def save(self, fname):
  73.         self.image.save(fname, "BMP");
  74.  
  75.     def iterate_pixel(self, z, c, x, y, imax):
  76.         for i in range(0, imax):
  77.             #z = z**2 + c;
  78.  
  79.             if ((int(i + 1) % 4) == 0):
  80.                 z = z**2 + c;
  81.  
  82.             mul = z.real * z.imag;
  83.             if (mul == 0.0): mul = .000000001;
  84.  
  85.             z = z**2 + (z.real + z.imag) / mul * .001;
  86.  
  87.             if (z.real**2 + z.imag**2 > 52):
  88.                 self.pixels[x, y] = (255, 255, 255);
  89.                 return;
  90.  
  91.             self.pixels[x, y] = (0, 0, 0);
  92.  
  93.     def iterate_plane(self, imax):
  94.         for y in range(0, self.plane.height - 1):
  95.             for x in range(0, self.plane.width - 1):
  96.                 z = self.plane.project_xy(x, y);
  97.                 self.iterate_pixel(z, z, x, y, imax);
  98.                 print("iterate_plane(%s of %s)\r" % (y, self.plane.height), end="");
  99.         print("");
  100.  
  101.  
  102.  
  103. # Secret Key
  104. class ct_rifc_skey:
  105.     def __init__(self, z, c_0, c_1, p_0, p_1, n, hmkey):
  106.         self.z = z;
  107.         self.c_0 = c_0;
  108.         self.c_1 = c_1;
  109.         self.p_0 = p_0;
  110.         self.p_1 = p_1;
  111.         self.n = n;
  112.         self.hmack = copy.copy(hmkey);
  113.  
  114.     def __repr__(self):
  115.         return "(%s, %s, %s, %s, %s, %s, %s)" % (self.z, self.c_0, self.c_1, self.p_0, self.p_1, self.n, self.hmack);
  116.  
  117.     def __str__(self): return self.__repr__();
  118.  
  119.  
  120.  
  121.  
  122. # Main Program
  123. axes = ct_axes_2d(-2, 2, -2, 2);
  124. plane = ct_plane_2d(640, 640, axes);
  125. plot = ct_hybrid(plane);
  126. plot.iterate_plane(100);
  127.  
  128. plot.save("output.bmp");
  129.  
  130. #os.system("mspaint output.bmp");
Advertisement
Add Comment
Please, Sign In to add comment