- /
-
Big Ladybug in Fireflies Wonderland
on 10 Oct 2024
- 23
- 179
- 1
- 5
- 1879
Cite your audio source here (if applicable):
% Nature’s Calm with Ambient Nature Sounds by Tim Janis
% https://youtu.be/vm5CqMpFU6Y?si=c_ugOH7GkpErSr5Z
drawframe(1);
Write your drawframe function below
function drawframe(f)
figure;
clf;
% Setting up axes properties
limX = [-7, 7];
limY = [-7, 7];
hold on;
axis off ;
axis equal ;
xlim(limX)
ylim(limY)
% Setting Gradient Background
gX = [limX(1) limX(2) limX(2) limX(1)];
gY = [limY(1) limY(1) limY(2) limY(2)];
cdata = [0 1 0;0 0 0; 0 0 0.4; 0 0.4470 0.7410];
patch(gX, gY, [1 2 3 4], 'FaceVertexCData', cdata, 'FaceColor', 'interp');
drawLadybugbody ; % Drawing the body of Ladybug
mf = mapNumber(f-1) ; % Mapping frame no to pattern value to create elytra movements
%Parameters to draw elytra
r = 3 ;
thR = linspace(-pi/2, pi/2, 100);
thL = linspace(pi/2, 3*pi/2, 100);
% Changing coordinates with changing frame to create elytra movements
xR = r * cos(thR+mf/10);
yR = r * sin(thR+mf/10);
xL = r * cos(thL-mf/10);
yL = r * sin(thL-mf/10);
% Drawing Elytra
fill(xR, yR, 'red',xL, yL, 'red');
scatter([1.7 1.8 -1.7 -1.8],[1.4 -1.4 1.4 -1.4],300,'k','filled')
scatter([1 -1],[0 0],400,'k','filled')
% Drawing Fireflies
n = 40; %no of fireflies
xf = 14 .* rand(n,1) + -7; %fireflies initial random coordinates
yf = 14 .* rand(n,1) + -7 ;
vx = 5 * randn(n,1); %fireflies initial random velocities
vy = 5 * randn(n,1);
h = scatter(xf, yf, 50, 'y', 'filled'); %fireflies plot
glow = 200 * ones(n,1); % Setting Glow effect
hg = scatter(xf, yf, glow, [1 1 0], 'filled', 'MarkerFaceAlpha', 0.15);
xf = xf + vx; %Changing Coordinates
yf = yf + vy;
vx(xf > 7 | xf <-7) = -0.9 * vx(xf > 7 | xf < -7); %Changing Velocities
vy(yf > 7 | yf < -7) = -0.9 * vy(yf > 7 | yf < -7);
glow = 200 + 150 * abs(sin(f*0.2 + (1:n)'));
set(h, 'XData', xf, 'YData', yf); %Updating plots
set(hg, 'XData', xf, 'YData', yf, 'SizeData', glow); %Updating Glow effects
end
function mapVal = mapNumber(n)
% Function used for creating elytra movements
pattern = [0, 1, 2, 3, 4, 5, 4, 3, 2, 1]; %mapping pattern
index = mod(n, 10) + 1;
mapVal = pattern(index);
end
function drawLadybugbody()
% Ladybug Drawing
rs = 2;
r = 3;
%head
th = linspace(0, 2*pi, 100);
x = rs*cos(th) ;
y = rs*sin(th) + r ;
fill(x, y,'k', 'LineWidth', 2);
%body ellipse parameter
a=1.5 ; b = 3 ;
xe = a * cos(th);
ye = b * sin(th);
fill(xe, ye,'k', 'LineWidth', 2);
%antenna
c = 0;
d = 5.1 ;
xt = @(t) cos(t) - c;
yt = @(t) sin(t) + d;
fplot(xt,yt,[-3.85,0.7],'k','linewidth',2)
scatter([cos(-3.85)-c,cos(0.7)-c],[sin(-3.85)+d,sin(0.7)+d],100,'k','filled')
%legs
x1 = [1.9 3.5 4.5];
y1 = [2.3 3 4.5];
x2 = [2.95 4 5.9];
y2 = [0.45 -0.5 1];
x3 = [2.55 4 2.5];
y3 = [-1.5 -2.5 -5];
plot(x1,y1,'k-',x2,y2,'k-',x3,y3,'k-',-x1,y1,'k-',-x2,y2,'k-',-x3,y3,'k-','LineWidth', 2);
%eyes
p = linspace(-3,-1.2,100);
c = -1.2;
d = 5.1 ;
xt = cos(p) - c;
yt = sin(p) + d;
fill(xt,yt,'w',-xt,yt,'w')
end