-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriangular.py
97 lines (73 loc) · 2.78 KB
/
triangular.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
import random
import time
import pmma
import pygame
import math
pmma.init()
canvas = pmma.Display()
canvas.create(1280, 720)
events = pmma.Events(canvas)
UNIFORM = False
registry = pmma.Registry
color_perlin = pmma.Perlin(random.randint(0, 9999))
rotation_perlin = pmma.Perlin(random.randint(0, 9999))
SWITCH = True
def draw_ngon(Surface, color, n, radius, position, rotation=0, width=0, wire_frame=False, cache=None):
if cache is not None:
return pygame.draw.polygon(Surface, color, cache, width=width), cache
pi2 = 2 * math.pi
if wire_frame:
for i in range(0, n):
pygame.draw.line(Surface, color, position, (math.cos(i / n * pi2) * radius + position[0], math.sin(i / n * pi2) * radius + position[1]))
points = [(math.cos(i / n * pi2 + rotation) * radius + position[0], math.sin(i / n * pi2 + rotation) * radius + position[1]) for i in range(0, n)]
if wire_frame:
width = 1
return pygame.draw.polygon(Surface, color, points, width=width), points
class Hexagon:
def __init__(self, n, d):
self.n = n/10
self.size = n
self.iter = n
self.points = None
self.k = n
self.d = d
def render(self, now_time):
color = [
color_perlin.generate_2D_perlin_noise((now_time+self.n)/75, 0, [0, 255]),
color_perlin.generate_2D_perlin_noise(0, (now_time+self.n)/75, [0, 255]),
color_perlin.generate_2D_perlin_noise(-(now_time+self.n)/75, 0, [0, 255])
]
if clear_cache:
self.points = None
_, self.points = draw_ngon(canvas.surface, color, 3, self.size, center, rotation=self.k/(2*math.pi))
draw_ngon(canvas.surface, (0, 0, 0), 3, self.size-5, center, rotation=self.k/(2*math.pi))
if SWITCH:
self.k = rotation_perlin.generate_2D_perlin_noise(-(now_time+self.iter)/1000, 0, [0, 360]) # 500
elif UNIFORM:
self.k += 0.1
else:
self.k += (1-self.d)/2
squares = []
diag = int(math.sqrt(canvas.get_width()**2 + canvas.get_width()**2))
for i in range(0, diag, 5):
squares.append(Hexagon(diag-i, i/diag))
clear_cache = False
start = time.perf_counter()
now_time = 0
center = (canvas.get_width()/2, canvas.get_height()/2)
while registry.running:
print(canvas.get_fps())
#k = time.perf_counter()
canvas.clear(pygame.transform.average_color(canvas.surface))
event = events.handle()
for e in event:
if e.type == pygame.KEYDOWN:
if e.key == pygame.K_F11:
clear_cache = True
center = (canvas.get_width()/2, canvas.get_height()/2)
for square in squares:
square.render(now_time)
clear_cache = False
canvas.refresh(refresh_rate=60)
now_time = (time.perf_counter() - start)*5
#s = time.perf_counter()