Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Simple Plotter, by Chris M. Thomasson
- # For the Wizz over on sci.crypt...
- import math;
- import random;
- import copy; # for non-mutable aspects of classes
- #import os; # open the output.bmp in your assigned program...
- from PIL import Image;
- from PIL import ImageDraw;
- # 2d Axes
- class ct_axes_2d:
- def __init__(self, xmin, xmax, ymin, ymax):
- self.xmin = xmin;
- self.xmax = xmax;
- self.ymin = ymin;
- self.ymax = ymax;
- def __repr__(self):
- return "(%s, %s, %s, %s)" % (self.xmin, self.xmax, self.ymin, self.ymax)
- def __str__(self): return self.__repr__();
- def width(self): return self.xmax - self.xmin;
- def height(self): return self.ymax - self.ymin;
- def ct_axes_2d_from_point(p, radius):
- return ct_axes_2d(
- p.real - radius,
- p.real + radius,
- p.imag - radius,
- p.imag + radius
- );
- # 2d Plane
- class ct_plane_2d:
- def __init__(self, axes, width, height):
- self.axes = copy.copy(axes);
- self.width = width;
- self.height = height;
- daspect = math.fabs(height / width);
- waspect = math.fabs(axes.height() / axes.width());
- if (daspect > waspect):
- excess = self.axes.height() * (daspect / waspect - 1);
- self.axes.ymax += excess / 2;
- self.axes.ymin -= excess / 2;
- elif (daspect < waspect):
- excess = self.axes.width() * (waspect / daspect - 1);
- self.axes.xmax += excess / 2;
- self.axes.xmin -= excess / 2;
- self.xstep = self.axes.width() / (width - 1);
- self.ystep = self.axes.height() / (height - 1);
- def __repr__(self):
- return "(%s, %s, %s, %s, %s)" % (self.axes, self.width, self.height, self.xstep, self.ystep)
- def __str__(self): return self.__repr__();
- def float_project(self, p):
- proj = math.floor(p / self.ystep);
- return proj;
- def complex_project(self, p):
- proj = complex(math.floor((p.real - self.axes.xmin) / self.xstep),
- math.floor((self.axes.ymax - p.imag) / self.ystep));
- return proj;
- # 2d Plot
- class ct_plot_2d:
- def __init__(self, plane):
- self.plane = copy.copy(plane);
- self.img = Image.new("RGB", (plane.width, plane.height));
- self.canvas = ImageDraw.Draw(self.img);
- def __repr__(self):
- return "(%s)" % (self.plane)
- def __str__(self): return self.__repr__();
- def save(self, ff):
- self.img.save("output.bmp");
- def circle(self, p, r):
- pproj = self.plane.complex_project(p);
- rproj = self.plane.float_project(r);
- ul = (pproj.real - rproj, pproj.imag - rproj);
- ur = (pproj.real + rproj, pproj.imag + rproj);
- self.canvas.arc([ul, ur], 0, 360);
- def rect(self, p, r):
- pproj = self.plane.complex_project(p);
- rproj = self.plane.float_project(r);
- ul = (pproj.real - rproj, pproj.imag - rproj);
- ur = (pproj.real + rproj, pproj.imag + rproj);
- self.canvas.rectangle([ul, ur]);
- def line(self, p0, p1):
- pproj_0 = self.plane.complex_project(p0);
- pproj_1 = self.plane.complex_project(p1);
- self.canvas.line((pproj_0.real, pproj_0.imag, pproj_1.real, pproj_1.imag));
- # Main Program
- def ct_gap(a, r):
- p0 = complex(math.cos(0), math.sin(0));
- p1 = complex(math.cos(a), math.sin(a));
- dif = p1 - p0;
- dis = abs(dif);
- dis_radius = dis / 2;
- print("dis:(%s)" % (dis));
- return dis_radius;
- # Define the Plane...
- plot_radius = 1.7;
- axes = ct_axes_2d_from_point(0+0j, plot_radius);
- plane = ct_plane_2d(axes, 1920, 1080);
- plot = ct_plot_2d(plane);
- # Actually plot our experiment!
- n = 23;
- center = 0+0j;
- abase = (math.pi * 2) / n;
- plot.circle(center, 1.0);
- plot.rect(center, 1.0);
- gap = ct_gap(abase, 1);
- for i in range(n):
- angle = i * abase;
- print("angle:(%s)" % angle);
- point = complex(math.cos(angle) + center.real, math.sin(angle) + center.imag);
- plot.circle(point, gap);
- plot.line(point, center);
- #a_prv = 0;
- #a_prv_angle = a_prv * abase;
- #plot.circle(complex(math.cos(a_prv_angle), math.sin(a_prv_angle)), .5);
- plot.circle(complex(1, 1), .5);
- plot.save("output.bmp");
- # open the output...
- #os.system("output.bmp");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement