Chris_M_Thomasson

Visual Crack on the RIFC...

Jul 29th, 2016
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.71 KB | None | 0 0
  1. # Chris M. Thomasson 7/29/2016
  2. # N-Ary Reverse Julia Set Renderer
  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.  
  14.  
  15. # Complex Absolute Value
  16. def cabs(z):
  17.     return math.sqrt(z.real**2 + z.imag**2);
  18.  
  19.  
  20. # Complex Argument
  21. def carg(z):
  22.     return math.atan2(z.imag, z.real);
  23.  
  24.  
  25. # Complex Argument Range [0...PI2]
  26. def cargr(z):
  27.     a = math.atan2(z.imag, z.real);
  28.     if (a < 0): a += math.pi * 2;
  29.     return a;
  30.  
  31.  
  32. # Complex Roots
  33. def croots(z, p):
  34.     l = cabs(z);
  35.     s = l**(1.0 / p);
  36.     a = carg(z) / p;
  37.     n = int(math.ceil(math.fabs(p)));
  38.     astep = (math.pi * 2.0) / p;
  39.     result = [];
  40.  
  41.     for i in range(n):
  42.         r = complex(math.cos(a + astep * i) * s,
  43.                     math.sin(a + astep * i) * s);
  44.         result.append(r);
  45.  
  46.     return result;
  47.  
  48.  
  49. # 2d Axes
  50. class ct_axes_2d:
  51.     def __init__(self, xmin, xmax, ymin, ymax):
  52.         self.xmin = xmin;
  53.         self.xmax = xmax;
  54.         self.ymin = ymin;
  55.         self.ymax = ymax;
  56.  
  57.     def __repr__(self):
  58.         return "(%s, %s, %s, %s)" % (self.xmin, self.xmax, self.ymin, self.ymax)
  59.  
  60.     def __str__(self): return self.__repr__();
  61.  
  62.     def width(self): return  self.xmax - self.xmin;
  63.     def height(self): return  self.ymax - self.ymin;
  64.  
  65.  
  66. # 2d Plane
  67. class ct_plane_2d:
  68.     def __init__(self, width, height, axes):
  69.         self.axes = copy.copy(axes);
  70.         self.width = width;
  71.         self.height = height;
  72.         self.xstep = self.axes.width() / width;
  73.         self.ystep = self.axes.height() / height;
  74.         self.aratio = height / width;
  75.  
  76.     def __repr__(self):
  77.         return "(%s, %s, %s, %s, %s, %s)" % (self.axes,
  78.                                              self.width, self.height,
  79.                                              self.xstep, self.ystep,
  80.                                              self.aratio);
  81.  
  82.     def __str__(self): return self.__repr__();
  83.  
  84.     def project_to(self, p):
  85.         x = math.floor((p.real - self.axes.xmin) / self.xstep * self.aratio);
  86.         y = math.floor((self.axes.ymax - p.imag) / self.ystep);
  87.         return complex(x, y);
  88.  
  89.  
  90. # Secret Key
  91. class ct_skey:
  92.     def __init__(self, c, p):
  93.         self.c = c;
  94.         self.p = p;
  95.  
  96.     def __repr__(self):
  97.         return "(%s, %s)" % (self.c, self.p);
  98.  
  99.     def __str__(self): return self.__repr__();
  100.  
  101.  
  102. # Reverse Julia Fractal
  103. class ct_rjulia:
  104.     def __init__(self, plane):
  105.         self.plane = copy.copy(plane);
  106.         self.image = Image.new("RGB", (plane.width, plane.height));
  107.         self.pixels = self.image.load();
  108.  
  109.     def __repr__(self):
  110.         return "(%s)" % (self.plane);
  111.  
  112.     def __str__(self): return self.__repr__();
  113.  
  114.     def save(self, fname):
  115.         self.image.save(fname, "BMP");
  116.  
  117.     def set_pixel(self, p, c):
  118.         if (p.real < 0 or p.real >= self.plane.width or
  119.             p.imag < 0 or p.imag >= self.plane.height):
  120.             return False;
  121.         self.pixels[p.real, p.imag] = c;
  122.         return True;
  123.  
  124.     def set_point(self, p, c):
  125.         pp = self.plane.project_to(p);
  126.         return self.set_pixel(pp, c);
  127.  
  128.     def plot(self, skey, z, n):
  129.         for i in range(n):
  130.             r = croots(z - skey.c, skey.p);
  131.             for p in r: self.set_point(p, (255, 255, 255));
  132.             print("ct_rjulia::plot(%s of %s)\r" % (i, n), end="");
  133.             z = r[random.randint(0, len(r) - 1)];
  134.         print("");
  135.  
  136.  
  137.  
  138.  
  139. # Main Program
  140. rfrac = ct_rjulia(ct_plane_2d(640, 640, ct_axes_2d(-2, 2, -2, 2)));
  141. skey = ct_skey((-.75+.09j), 2.0);
  142.  
  143. print("rfrac:%s" % (rfrac));
  144. print("skey:%s" % (skey));
  145.  
  146. rfrac.plot(skey, (0+0j), 12048);
  147. rfrac.save("output.bmp");
  148.  
  149.  
  150. #os.system("mspaint output.bmp");
Add Comment
Please, Sign In to add comment