• Remix
  • Share
  • New Entry

on 9 Nov 2023
  • 3
  • 22
  • 2
  • 1
  • 899
Remix with other bouncing graphics :)
drawframe(5);
function drawframe(frame)
persistent canol theta cyflymder lliw
H = 400; % set length of field
LL = 300; % set height of field
h = 90; % set length of rectangle
ll = 60; % set height of rectangle
% Initialize position and direction
if isempty(canol) && isempty(cyflymder)
canol = [rand*(H-h)+h/2 , rand*(LL-ll)+ll/2]; % initialises a random central position within the field and not beyond walls (x and y co-ord)
theta = pi/4; % you could also use a random direction angle 2*pi*rand(1)
cyflymder = 20*[cos(theta) sin(theta)]; % the velocity vector is defined with a magnitude per frame
lliw = rand(1,3);
end
clf ; axis equal; axis off; axis equal; hold on;
plot(polyshape([0,H,H,0], [LL,LL,0,0]),'FaceColor', 'k', 'FaceAlpha', 1); % plots the 'screen'
canol = canol + cyflymder;
x = [canol(1)-h/2 , canol(1)+h/2, canol(1)+h/2, canol(1)-h/2]; % x coord of rectangle corners
y = [canol(2)+ll/2 , canol(2)+ll/2, canol(2)-ll/2, canol(2)-ll/2]; % y coord of rectangle corners
if any(x < 0) | any(x > H)
cyflymder(1) = -cyflymder(1);
end
if any(y < 0) | any(y > LL)
cyflymder(2) = -cyflymder(2);
end
for i = 1:10
if any(x < 0) | any(x > H) | any(y < 0) | any(y > LL)
canol = canol + 0.5*cyflymder;
x = [canol(1)-h/2 , canol(1)+h/2, canol(1)+h/2, canol(1)-h/2]; % x coord of rectangle corners
y = [canol(2)+ll/2 , canol(2)+ll/2, canol(2)-ll/2, canol(2)-ll/2]; % y coord of rectangle corners
lliw = rand(1,3);
end
end
plot(polyshape(x,y), 'FaceColor', lliw, 'FaceAlpha', 1);
end
Animation
Remix Tree