Harmonic Motion Art
Youtube:
Required library: raylib
pip install raylib
Source code:
from pyray import *
from raylib import *
from math import sin, cos, pi, sqrt
from random import random
from time import sleep
SW = 540
SH = 960
CX = SW / 2
CY = SH / 2
MAXR = 540/2-10
NSEG = 3
set_config_flags(FLAG_MSAA_4X_HINT)
init_window(SW, SH, "Anim")
lens = []
rots = []
angs = []
pts = []
dly =0.1
LC = (0,225,255)
def distance(point1, point2):
return sqrt((point1[0] - point2[0]) ** 2 + (point1[1] - point2[1]) ** 2)
def rndrot():
return round(100*random()-50)*5/100
def reset():
global lens,rots,angs,pts,dly
pts = []
lens = []
rots = []
angs = []
dly=0.05
mr = MAXR-5
for i in range(NSEG):
rots.append(rndrot())
angs.append(0)
lens.append(MAXR/NSEG)
print(lens)
print(rots)
reset()
dowait = False
waitcount=0
while not window_should_close():
begin_drawing()
if not dowait:
clear_background(BLACK)
x = CX
y = CY
a = 0
for i in range(NSEG):
x1 = x + cos((angs[i] + a) * pi / 180) * lens[i]
y1 = y + sin((angs[i] + a) * pi / 180) * lens[i]
draw_line_ex([x, y], [x1, y1], 2, RED)
draw_circle_v([x1, y1], 5, GREEN)
x = x1
y = y1
a += angs[i]
angs[i] += rots[i]
pts.append([x, y])
draw_line_strip(pts, len(pts), WHITE)
if len(pts) > 2:
start_point = Vector2(pts[0][0], pts[0][1])
end_point = Vector2(pts[len(pts) - 1][0], pts[len(pts) - 1][1])
dist = distance([start_point.x, start_point.y], [end_point.x, end_point.y])
if dist < 0.1:
dowait=True
waitcount=10000
else:
clear_background(BLACK)
draw_line_strip(pts, len(pts), WHITE)
waitcount-=10
if waitcount<=0:
reset()
dowait=False
end_drawing()
if IsKeyPressed(32):
dowait=False
reset()
sleep(dly)
if dly>0.00001:
dly/=1.025
close_window()
No comments:
Post a Comment