-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchaos_cubes.py
153 lines (139 loc) · 6.3 KB
/
chaos_cubes.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import math
from random import randint
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.animation import PillowWriter
def rotMatr(ang):
mtr = np.array([[np.cos(ang), -np.sin(ang)], [np.sin(ang), np.cos(ang)]])
return mtr
def draw_line(x0, y0, x1, y1, image, color):
# Bresenham's algorithm
sign_x = 0
sign_y = 0
if x1 - x0 > 0:
sign_x = 1
elif x1 - x0 < 0:
sign_x = -1
if y1 - y0 > 0:
sign_y = 1
elif y1 - y0 < 0:
sign_y = -1
delta_x = abs(x1 - x0)
delta_y = abs(y1 - y0)
if delta_x > delta_y:
d = delta_x
dd = delta_y
else:
d = delta_y
dd = delta_x
x_cur, y_cur = x0, y0
error = d / 2
image[y_cur, x_cur] = color
for i in range(d):
error -= dd
if error < 0:
error += d
x_cur += sign_x
y_cur += sign_y
else:
if delta_x > delta_y:
x_cur += sign_x
else:
y_cur += sign_y
image[y_cur, x_cur] = color
cubes_amount=4
first_cube_center=[300,400]
second_cube_center=[500,500]
third_cube_center=[600,600]
fourth_cube_center=[700,700]
#fifth_cube_center=[400,200]
cubes_angle=[1,1,1,1]
width, height = 1000, 1000
count_foursquare = 4
rotation_speed = np.pi
img = np.zeros((width, height, 3), dtype=np.uint8)
animation.ffmpeg_path = 'ffmpeg'
fig = plt.figure(frameon=False)
plt.axis('off')
cubes_speed = [[10, 4], [-10, 14], [5, -10],[-4,-4]]
# ---------------------------------------------------------------
frames = []
for i in range(height):
for j in range(width):
img[i][j] = [255, 255, 255]
for frame in range(100):
first_cube_points = [[first_cube_center[0]-30, first_cube_center[1]+30],
[first_cube_center[0]+30, first_cube_center[1]+30],
[first_cube_center[0]+30, first_cube_center[1]-30],
[first_cube_center[0]-30, first_cube_center[1]-30]]
second_cube_points = [[second_cube_center[0]-30, second_cube_center[1]+30],
[second_cube_center[0]+30, second_cube_center[1]+30],
[second_cube_center[0]+30, second_cube_center[1]-30],
[second_cube_center[0]-30, second_cube_center[1]-30]]
third_cube_points = [[third_cube_center[0]-30, third_cube_center[1]+30],
[third_cube_center[0]+30, third_cube_center[1]+30],
[third_cube_center[0]+30, third_cube_center[1]-30],
[third_cube_center[0]-30, third_cube_center[1]-30]]
fourth_cube_points = [[fourth_cube_center[0] - 30, fourth_cube_center[1] + 30],
[fourth_cube_center[0] + 30, fourth_cube_center[1] + 30],
[fourth_cube_center[0] + 30, fourth_cube_center[1] - 30],
[fourth_cube_center[0] - 30, fourth_cube_center[1] - 30]]
cubes_vertices = [first_cube_points, second_cube_points, third_cube_points,fourth_cube_points]
cubes_centers=[first_cube_center,second_cube_center,third_cube_center,fourth_cube_center]
# scene refreshing
for i in range(height):
for j in range(width):
img[i][j] = [255, 255, 255]
# out of canvas cases
for i in range(cubes_amount):
if cubes_centers[i][0] < math.sqrt(2) * 70 or cubes_centers[i][0] > width - math.sqrt(2) * 40:
cubes_speed[i][0] *= -1
cubes_angle[i] *= -1
if cubes_centers[i][1] < math.sqrt(2) * 70 or cubes_centers[i][1] > width - math.sqrt(2) * 40:
cubes_speed[i][1] *= -1
cubes_angle[i] *= -1
# collision cases
for i in range(cubes_amount):
for j in range(i+1,cubes_amount):
if np.sqrt(((cubes_centers[i][0] - cubes_centers[j][0]) ** 2) + ((cubes_centers[i][1] - cubes_centers[j][1]) ** 2))<math.sqrt(2)*60:
cubes_speed[i][0] *= -1
cubes_speed[i][1] *= -1
cubes_angle[i] *= -1
cubes_speed[j][0] *= -1
cubes_speed[j][1] *= -1
cubes_angle[j] *= -1
# cubes shifting
for i in range(cubes_amount):
cubes_centers[i][0]+=cubes_speed[i][0]
cubes_centers[i][1]+=cubes_speed[i][1]
for i in range(cubes_amount):
cubes_vertices[i][0][0]-=cubes_centers[i][0]
cubes_vertices[i][0][1]-=cubes_centers[i][1]
cubes_vertices[i][1][0] -= cubes_centers[i][0]
cubes_vertices[i][1][1] -= cubes_centers[i][1]
cubes_vertices[i][2][0] -= cubes_centers[i][0]
cubes_vertices[i][2][1] -= cubes_centers[i][1]
cubes_vertices[i][3][0] -= cubes_centers[i][0]
cubes_vertices[i][3][1] -= cubes_centers[i][1]
cubes_vertices[i]=cubes_vertices[i] @ rotMatr(rotation_speed * cubes_angle[i])
cubes_vertices[i][0][0] += cubes_centers[i][0]
cubes_vertices[i][0][1] += cubes_centers[i][1]
cubes_vertices[i][1][0] += cubes_centers[i][0]
cubes_vertices[i][1][1] += cubes_centers[i][1]
cubes_vertices[i][2][0] += cubes_centers[i][0]
cubes_vertices[i][2][1] += cubes_centers[i][1]
cubes_vertices[i][3][0] += cubes_centers[i][0]
cubes_vertices[i][3][1] += cubes_centers[i][1]
rotation_speed+=0.07
# cubes drawing
for i in range(cubes_amount):
draw_line(int(cubes_vertices[i][0][0]),int(cubes_vertices[i][0][1]),int(cubes_vertices[i][1][0]),int(cubes_vertices[i][1][1]),img,[0,0,0])
draw_line(int(cubes_vertices[i][1][0]),int(cubes_vertices[i][1][1]),int(cubes_vertices[i][2][0]),int(cubes_vertices[i][2][1]),img,[0,0,0])
draw_line(int(cubes_vertices[i][2][0]),int(cubes_vertices[i][2][1]),int(cubes_vertices[i][3][0]),int(cubes_vertices[i][3][1]),img,[0,0,0])
draw_line(int(cubes_vertices[i][3][0]),int(cubes_vertices[i][3][1]),int(cubes_vertices[i][0][0]),int(cubes_vertices[i][0][1]),img,[0,0,0])
im = plt.imshow(img.swapaxes(0, 1), origin='lower')
frames.append([im])
# ---------------------------------------------------------------
ani = animation.ArtistAnimation(fig, frames, interval=200, blit=True)
ani.save(filename="cubes.gif", writer=PillowWriter(fps=24))