-
Notifications
You must be signed in to change notification settings - Fork 0
/
pattern.py
83 lines (71 loc) · 2.94 KB
/
pattern.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import numpy as np
import matplotlib.pyplot as plt
class Checker:
def __init__(self, resolution, tile_size):
if resolution%(2*tile_size) != 0:
raise ValueError()
self.resolution = resolution
self.tile_size = tile_size
self.output = None
def draw(self):
self.output = np.zeros((self.resolution, self.resolution), dtype=int)
num_tiles = self.resolution//self.tile_size
for x in range(num_tiles):
for y in range(num_tiles):
if (x+y)%2 != 0:
current_x = x*self.tile_size
current_y = y*self.tile_size
for i in range(self.tile_size):
self.output[current_y+i, current_x:current_x+self.tile_size] = 1
return self.output.copy()
def show(self):
plt.imshow(self.output, cmap='gray')
plt.axis('off')
plt.show()
class Circle:
def __init__(self, resolution, radius, position):
self.resolution = resolution
self.radius = radius
self.position = position
self.output = None
def draw(self):
y, x = np.meshgrid(np.arange(self.resolution), np.arange(self.resolution), indexing='ij')
center_x, center_y = self.position
distance = np.sqrt((x-center_x)**2 + (y-center_y)**2)
self.output = np.zeros((self.resolution, self.resolution), dtype=int)
self.output[distance <= self.radius] = 1
return self.output.copy()
def show(self):
plt.imshow(self.output, cmap='gray')
plt.axis('off')
plt.show()
class Spectrum:
def __init__(self, resolution):
self.resolution = resolution
self.output = None
def draw(self):
"""
We need Matrix (array of arrays) of tuples:
[
[(0, 0, 255), (1, 0, 254), ... , (255, 0, 0)],
[(0, 1, 255), (1, 0, 254), ... , (255, 1, 0)],
...
[(0, 255, 255), (1, 255, 254), ... , (255, 255, 0)]
]
We can see that:
the red channel in the rows has always (0, 1, ..., 255) pattern
the green channel in the rows has always (0, 0, ..., 0), (1, 1, ..., 1), ... ,(255, 255, ..., 255) pattern
the blue channel in the rows has always (255, 254, ..., 0) pattern
We use linspace to obtain given resolution
"""
sequence = np.linspace(0, 1, self.resolution)
red = np.tile(sequence, (self.resolution, 1))
green = np.tile(sequence.reshape(-1, 1), (1, self.resolution))
rev_sequence = np.linspace(1, 0, self.resolution)
blue = np.tile(rev_sequence, (self.resolution, 1))
self.output = np.stack((red, green, blue), axis=-1)
return self.output.copy()
def show(self):
plt.imshow(self.output, cmap='gray')
plt.axis('off')
plt.show()