- /
-
Ladybug on the Race!
on 11 Oct 2024
- 12
- 129
- 0
- 0
- 1888
Cite your audio source here (if applicable):
drawframe(1);
Write your drawframe function below
drawframe(1);
function drawframe(f)
figure;
clf;
limX=[-7,7];
limY=[-7,7];
hold on;
axis off;
axis equal;
xlim(limX);
ylim(limY);
tileSize=2;
for x=-7:tileSize:7
for y=-7:tileSize:7
if mod(floor(x/tileSize)+floor(y/tileSize),2)==0
fill([x,x+tileSize,x+tileSize,x],[y,y,y+tileSize,y+tileSize],[0.8,0.8,0.8]);
else
fill([x,x+tileSize,x+tileSize,x],[y,y,y+tileSize,y+tileSize],[0.6,0.6,0.6]);
end
end
end
yPos=mod(f*0.1,14)-7; % Vertical movement (forward)
xPos=0; % No horizontal movement
f2(xPos,yPos,f); % Drawing the ladybug
end
function f2(xPos,yPos,f)
rHead=2;
rBody=3;
th=linspace(0,2*pi,100);
x=rHead*cos(th)+xPos;
y=rHead*sin(th)+rBody+yPos;
fill(x,y,'k','LineWidth',2);
a=1.5;b=3;
xe=a*cos(th)+xPos;
ye=b*sin(th)+yPos;
fill(xe,ye,'k','LineWidth',2);
f3(xPos,yPos); % Drawing elytra
f4(xPos,yPos); % Drawing antennae
f5(xPos,yPos); % Drawing eyes
f6(xPos,yPos,f); % Drawing legs
% Adding black dots to the ladybug body
dotX=[-0.5,0,0.5]; % X positions for dots
dotY=[0,1,0]; % Y positions for dots
scatter(dotX+xPos,dotY+yPos,100,'k','filled'); % Dots
end
function f3(xPos,yPos)
rElytra=3;
thR=linspace(-pi/2,pi/2,100);
thL=linspace(pi/2,3*pi/2,100);
xR=rElytra*cos(thR)+xPos;
yR=rElytra*sin(thR)+yPos;
xL=rElytra*cos(thL)+xPos;
yL=rElytra*sin(thL)+yPos;
fill(xR,yR,'red',xL,yL,'red');
end
function f4(xPos,yPos)
line([-2,-1]+xPos,[3,5]+yPos,'Color','k','LineWidth',2);
line([2,1]+xPos,[3,5]+yPos,'Color','k','LineWidth',2);
end
function f5(xPos,yPos)
scatter([1.5 -1.5]+xPos,[3 3]+yPos,200,'w','filled');
scatter([1.8 -1.8]+xPos,[3 3]+yPos,100,'k','filled');
end
function f6(xPos,yPos,f)
legOffset=0.3*sin(f*0.5);
legVertOffset=0.1*sin(f*2);
% Right-side legs
x1=[1.9 3.5 4.5]+xPos;
y1=[2.3+legVertOffset 3+legVertOffset 4.5+legVertOffset]+yPos;
x2=[2.95 4 5.9]+xPos;
y2=[0.45+legVertOffset -0.5+legVertOffset 1+legVertOffset]+yPos;
x3=[2.55 4 2.5]+xPos;
y3=[-1.5+legVertOffset -2.5+legVertOffset -5+legVertOffset]+yPos;
% Left-side legs
x1_l=[-1.9 -3.5 -4.5]+xPos;
y1_l=[2.3+legVertOffset 3+legVertOffset 4.5+legVertOffset]+yPos;
x2_l=[-2.95 -4 -5.9]+xPos;
y2_l=[0.45+legVertOffset -0.5+legVertOffset 1+legVertOffset]+yPos;
x3_l=[-2.55 -4 -2.5]+xPos;
y3_l=[-1.5+legVertOffset -2.5+legVertOffset -5+legVertOffset]+yPos;
% Plot legs
plot(x1,y1,'k-',x2,y2,'k-',x3,y3,'k-', ...
x1_l,y1_l,'k-',x2_l,y2_l,'k-',x3_l,y3_l,'k-','LineWidth',2);
end