using keyboard to control
Show older comments
figure;
bottom = [-2 2 2 -2 -2; 4 4 -4 -4 4];
blade1 = [-0.5 0.5 0.5 -0.5 -0.5; 5.5 5.5 2.5 2.5 5.5];
drawshape(bottom, 'g')
hold on
drawshape(blade1, 'r')
hold on
stopRotation = false;
for i = 1:1000
if stopRotation == true
break; % 如果 stopRotation 为 true,停止旋转
end
clf; % 清除当前图形窗口
drawshape(bottom, 'g')
fill(bottom(1, :), bottom(2, :), 'g');
hold on
% rotate and draw the blade
for j = 1:4
bladei = rotateabout(blade1, 0, 6, (j - 1) * pi/2 + i * pi/180);
drawshape(bladei, 'r')
fill(bladei(1, :), bladei(2, :), 'r');
hold on
end
% set axis
axis([-10 10 -10 10])
axis square
set(gca, 'Color', [0.7 0.85 1]);
% 控制每一帧显示的时间
pause(0.001)
end
set(gcf, 'KeyPressFcn', @keyPressCallback); % 设置当前图形窗口的按键回调函数
% 按键回调函数
function keyPressCallback(~, event)
if strcmp(event.Key, 's') % 如果按下回车键
stopRotation = true; % 设置 stopRotation 为 true
end
end
function drawshape(matrix, colour)
x = matrix(1,:);
y = matrix(2,:);
plot(x, y, colour)
end
function newShape = translateShape(shape, xShift, yShift)
x = shape(1,:) + xShift;
y = shape(2,:) + yShift;
newShape = [x; y];
end
function newShape = rotateShape(shape, a)
reflectMatrix = [cos(a) -sin(a); sin(a) cos(a)];
newShape = reflectMatrix * shape;
end
function newShape = rotateabout(shape, p, q, a)
shape1 = translateShape(shape, -p, -q);
shape2 = rotateShape(shape1, a);
newShape = translateShape(shape2, p, q);
end
Accepted Answer
More Answers (0)
Categories
Find more on MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!