- /
-
Acoustic Colorful Particle Animation
on 20 Oct 2024
- 12
- 77
- 0
- 0
- 1733
Cite your audio source here (if applicable):
I composed 30 sound loops for use in the Mini Hack.
If you like them, please feel free to use them for free.
drawframe(1);
Write your drawframe function below
function drawframe(f)
persistent p p1 p2 swarm particles_x0 particles_y0
if f == 1
set(gcf,'Color','k')
c.darkgoldenrod = "#b8860b";
c.darkorange = "#ff8c00";
c.col1 = c.darkgoldenrod;
c.col2 = c.darkorange;
fc = @(x,y) x.^5 + y.^6 - 1;
fp = fimplicit(fc);
fp.Color = c.col1;
fp.LineWidth = 5;
axis equal
axis off
hold on
xlim([-10 1.2])
ylim([-4 4])
% get edge points
p1 = [fp.XData(1), fp.YData(1)];
p2 = [fp.XData(end), fp.YData(end)];
[x, y, lambda] = bezier_curve(f, p1, p2);
p = plot(x, y, 'Color', c.col2, 'LineWidth', 3);
plot(p1(1), p1(2), 'bo', 'MarkerSize', 10, 'MarkerFaceColor', 'b');
plot(p2(1), p2(2), 'bo', 'MarkerSize', 10, 'MarkerFaceColor', 'b');
% particles
num_particles = 200;
% cols = randi(256,num_particles,1);
cols = lines(num_particles);
particles_x0 = -7 - rand(1, num_particles) * 3;
particles_y0 = -4 + rand(1, num_particles) * 8;
swarm = swarmchart(particles_x0, particles_y0, 30, cols, 'filled', ...
'MarkerFaceAlpha',0.75);
compress(lambda, particles_x0, y, p, swarm)
hold off
view(90,90)
drawnow
end
if f > 1
[x, y, lambda] = bezier_curve(f, p1, p2);
p.XData = x;
p.YData = y;
compress(lambda, particles_x0, y, p, swarm)
drawnow
end
end
function [x, y, lambda] = bezier_curve(f, p1, p2)
lambda = sin((f-1)/24*2*pi) * 2;
% control point
c_point = [(p1(1) + p2(1))/2 + lambda, (p1(2) + p2(2))/2];
% diaphragm & edge
t = 0:0.05:1;
x = (1-t).^2 * p1(1) + 2*(1-t).*t * c_point(1) + t.^2 * p2(1);
y = (1-t).^2 * p1(2) + 2*(1-t).*t * c_point(2) + t.^2 * p2(2);
end
function compress(lambda, particles_x0, y, p, swarm)
compression_ratio = 1 - 0.5 * lambda / 2;
particles_x = particles_x0 * compression_ratio;
n = length(particles_x);
swarm.YData = -4 + rand(1, n) * 8;
for i = 1:length(particles_x)
% idx = find(y <= particles_y0(i), 1);
idx = find(y <= swarm.YData(i), 1);
if isempty(idx)
idx = length(y);
end
left_boundary_x = p.XData(idx);
particles_x(i) = min(particles_x(i),left_boundary_x);
end
swarm.XData = particles_x;
alpha = rand(1,n);
swarm.AlphaData = alpha;
end