Advertisement
Chris_M_Thomasson

Plotter for the Wizz

Jan 21st, 2018
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.32 KB | None | 0 0
  1. # Simple Plotter, by Chris M. Thomasson
  2. # For the Wizz over on sci.crypt...
  3.  
  4. import math;
  5. import random;
  6. import copy; # for non-mutable aspects of classes
  7.  
  8. #import os; # open the output.bmp in your assigned program...
  9.  
  10. from PIL import Image;
  11. from PIL import ImageDraw;
  12.  
  13.  
  14. # 2d Axes
  15. class ct_axes_2d:
  16.     def __init__(self, xmin, xmax, ymin, ymax):
  17.         self.xmin = xmin;
  18.         self.xmax = xmax;
  19.         self.ymin = ymin;
  20.         self.ymax = ymax;
  21.  
  22.     def __repr__(self):
  23.         return "(%s, %s, %s, %s)" % (self.xmin, self.xmax, self.ymin, self.ymax)
  24.  
  25.     def __str__(self): return self.__repr__();
  26.  
  27.     def width(self): return  self.xmax - self.xmin;
  28.     def height(self): return  self.ymax - self.ymin;
  29.  
  30.  
  31. def ct_axes_2d_from_point(p, radius):
  32.     return ct_axes_2d(
  33.         p.real - radius,
  34.         p.real + radius,
  35.         p.imag - radius,
  36.         p.imag + radius
  37.     );
  38.  
  39.  
  40. # 2d Plane
  41. class ct_plane_2d:
  42.     def __init__(self, axes, width, height):
  43.         self.axes = copy.copy(axes);
  44.         self.width = width;
  45.         self.height = height;
  46.  
  47.         daspect = math.fabs(height / width);
  48.         waspect = math.fabs(axes.height() / axes.width());
  49.  
  50.         if (daspect > waspect):
  51.             excess = self.axes.height() * (daspect / waspect - 1);
  52.             self.axes.ymax += excess / 2;
  53.             self.axes.ymin -= excess / 2;
  54.         elif (daspect < waspect):
  55.             excess = self.axes.width() * (waspect / daspect - 1);
  56.             self.axes.xmax += excess / 2;
  57.             self.axes.xmin -= excess / 2;
  58.  
  59.         self.xstep = self.axes.width() / (width - 1);
  60.         self.ystep = self.axes.height() / (height - 1);
  61.  
  62.     def __repr__(self):
  63.         return "(%s, %s, %s, %s, %s)" % (self.axes, self.width, self.height, self.xstep, self.ystep)
  64.  
  65.     def __str__(self): return self.__repr__();
  66.  
  67.     def float_project(self, p):
  68.         proj = math.floor(p / self.ystep);
  69.         return proj;
  70.  
  71.     def complex_project(self, p):
  72.         proj = complex(math.floor((p.real - self.axes.xmin) / self.xstep),
  73.                        math.floor((self.axes.ymax - p.imag) / self.ystep));
  74.         return proj;
  75.  
  76.  
  77. # 2d Plot
  78. class ct_plot_2d:
  79.     def __init__(self, plane):
  80.         self.plane = copy.copy(plane);
  81.         self.img = Image.new("RGB", (plane.width, plane.height));
  82.         self.canvas = ImageDraw.Draw(self.img);
  83.  
  84.     def __repr__(self):
  85.         return "(%s)" % (self.plane)
  86.  
  87.     def __str__(self): return self.__repr__();
  88.  
  89.     def save(self, ff):
  90.         self.img.save("output.bmp");
  91.  
  92.     def circle(self, p, r):
  93.         pproj = self.plane.complex_project(p);
  94.         rproj = self.plane.float_project(r);
  95.         ul = (pproj.real - rproj, pproj.imag - rproj);
  96.         ur = (pproj.real + rproj, pproj.imag + rproj);
  97.         self.canvas.arc([ul, ur], 0, 360);
  98.  
  99.     def rect(self, p, r):
  100.         pproj = self.plane.complex_project(p);
  101.         rproj = self.plane.float_project(r);
  102.         ul = (pproj.real - rproj, pproj.imag - rproj);
  103.         ur = (pproj.real + rproj, pproj.imag + rproj);
  104.         self.canvas.rectangle([ul, ur]);
  105.  
  106.  
  107.     def line(self, p0, p1):
  108.         pproj_0 = self.plane.complex_project(p0);
  109.         pproj_1 = self.plane.complex_project(p1);
  110.         self.canvas.line((pproj_0.real, pproj_0.imag, pproj_1.real, pproj_1.imag));
  111.  
  112.  
  113.  
  114.  
  115. # Main Program
  116.  
  117.  
  118. def ct_gap(a, r):
  119.     p0 = complex(math.cos(0), math.sin(0));
  120.     p1 = complex(math.cos(a), math.sin(a));
  121.     dif = p1 - p0;
  122.     dis = abs(dif);
  123.     dis_radius = dis / 2;
  124.     print("dis:(%s)" % (dis));
  125.     return dis_radius;
  126.  
  127. # Define the Plane...
  128. plot_radius = 1.7;
  129. axes = ct_axes_2d_from_point(0+0j, plot_radius);
  130. plane = ct_plane_2d(axes, 1920, 1080);
  131. plot = ct_plot_2d(plane);
  132.  
  133. # Actually plot our experiment!
  134. n = 23;
  135. center = 0+0j;
  136. abase = (math.pi * 2) / n;
  137. plot.circle(center, 1.0);
  138. plot.rect(center, 1.0);
  139.  
  140. gap = ct_gap(abase, 1);
  141.  
  142. for i in range(n):
  143.     angle = i * abase;
  144.     print("angle:(%s)" % angle);
  145.     point = complex(math.cos(angle) + center.real, math.sin(angle) + center.imag);
  146.     plot.circle(point, gap);
  147.     plot.line(point, center);
  148.  
  149.  
  150. #a_prv = 0;
  151. #a_prv_angle = a_prv * abase;
  152. #plot.circle(complex(math.cos(a_prv_angle), math.sin(a_prv_angle)), .5);
  153. plot.circle(complex(1, 1), .5);  
  154.  
  155. plot.save("output.bmp");
  156.  
  157. # open the output...
  158. #os.system("output.bmp");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement