tpolygen.py - polygen - generative drawing of polygonal patterns
(HTM) git clone git://src.adamsgaard.dk/polygen
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) LICENSE
---
tpolygen.py (2689B)
---
1 #!/usr/bin/env python
2 import numpy
3 import matplotlib.pyplot as plt
4
5 class polyplot:
6 def __init__(self, figure_size_inches=(8,8), dpi=None):
7 self.points = []
8 self.fig = plt.figure(figsize=figure_size_inches, dpi=dpi)
9 self.ax = self.fig.add_subplot(1, 1, 1)
10
11 def add_point(self, xy_point):
12 self.points.append(numpy.asarray(xy_point))
13
14 def set_points(self, points):
15 self.points = points
16
17 def add_points(self, points):
18 self.points = numpy.concatenate((self.points, points), axis=0)
19
20 def plot_all_to_all(self, line_color='black', line_width=2,
21 line_style='solid',
22 plot_points=False, points_style='wo',
23 show_axes=False, exceptions=[]):
24 self.points = numpy.asarray(self.points)
25 for i in range(self.points.shape[0]):
26 for j in range(self.points.shape[0]):
27 if (i != j and
28 ([i,j] in exceptions) == False and
29 ([j,i] in exceptions) == False):
30 plt.plot([self.points[i,0], self.points[j,0]],
31 [self.points[i,1], self.points[j,1]],
32 color=line_color, linestyle=line_style,
33 linewidth=line_width)
34 if plot_points == True:
35 plt.plot(self.points[:,0], self.points[:,1], points_style)
36 plt.axis('equal')
37 if show_axes == False:
38 plt.axis('off')
39
40 def plot_circle(self, coord, radius, color='black'):
41 circ = plt.Circle(coord, radius=radius, color=color)
42 self.ax.add_patch(circ)
43
44 def save_plot(self, image_name='all_to_all', image_format='png',
45 background_color='white',
46 transparent_background=False):
47 if transparent_background == True:
48 self.fig.savefig(image_name + '.' + image_format,
49 transparent=True)
50 else:
51 self.fig.savefig(image_name + '.' + image_format,
52 facecolor=background_color, transparent=False)
53 self.fig.clf()
54
55
56 class regular_star_polygon:
57 def __init__(self, n=5, radius=1.0):
58
59 if n < 3:
60 raise Exception('regular_star_polygon: Error, this function ' +
61 'doesn\'t work with n < 3.')
62
63 self.n = n
64 self.generate_regular_star_polygon(radius=radius)
65
66 def generate_regular_star_polygon(self, radius=1.0):
67 self.points = numpy.empty((self.n, 2))
68
69 for i in range(self.n):
70 theta = i*(2.0*numpy.pi/self.n)
71 x = radius*numpy.cos(theta)
72 y = radius*numpy.sin(theta)
73 self.points[i,0] = x
74 self.points[i,1] = y