• Remix
  • Share
  • New Entry

on 9 Nov 2023
  • 8
  • 106
  • 1
  • 3
  • 1282
Remix with other bouncing graphics :)
drawframe(5);
ans =
Ellipse with properties: Center: [82.3252 121.0856] SemiAxes: [45 15] RotationAngle: 0 AspectRatio: 1.6180 Label: '' Color: [0.0938 0.4394 0.4879] Parent: [1×1 Axes] Visible: on Selected: 0 Use GET to show all properties
ans =
Ellipse with properties: Center: [82.3252 121.0856] SemiAxes: [9 3] RotationAngle: 0 AspectRatio: 1.6180 Label: '' Color: [0 0 0] Parent: [1×1 Axes] Visible: on Selected: 0 Use GET to show all properties
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
images.roi.Ellipse(gca, 'Center', [canol(1),canol(2)-ll/4], 'Semiaxes', [h, ll/2]/2, 'InteractionsAllowed', 'none', 'Color', lliw, 'FaceAlpha',1)
images.roi.Ellipse(gca, 'Center', [canol(1),canol(2)-ll/4], 'Semiaxes', [h, ll/2]/10, 'InteractionsAllowed', 'none', 'Color', 'k', 'FaceAlpha',1)
text(canol(1),canol(2)+ll/4,'DVD', 'Color', lliw, 'HorizontalAlignment', 'center', 'VerticalAlignment', 'middle','FontWeight', 'bold','FontSize',13)
end
Animation
Remix Tree