using keyboard to control

2 views (last 30 days)
JIayun
JIayun on 31 Dec 2023
Edited: Hassaan on 1 Jan 2024
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

Hassaan
Hassaan on 31 Dec 2023
Edited: Hassaan on 31 Dec 2023
% Execute the animation function
% fan_animation();
function fan_animation()
global stopRotation; % Declare as global variable
stopRotation = false; % Initialize the variable
% Initialize the figure
figure;
% Set up key press callback function for the current figure
set(gcf, 'KeyPressFcn', @keyPressCallback);
% Define the shapes using matrices
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];
% Draw the initial shapes
drawshape(bottom, 'g');
hold on;
drawshape(blade1, 'r');
hold on;
% Initialize stopRotation variable
stopRotation = false;
% Main loop for animation
for i = 1:1000
if stopRotation
break; % If stopRotation is true, stop the rotation
end
clf; % Clear the current figure
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 properties
axis([-10 10 -10 10]);
axis square;
set(gca, 'Color', [0.7 0.85 1]);
% Control the display time of each frame
pause(0.001);
end
end
% Key press callback function
function keyPressCallback(~, event)
global stopRotation;
disp("keyPressCallback() --> Key Pressed:")
disp(event.Key)
if strcmp(event.Key, 's') % If the 's' key is pressed
stopRotation = true; % Set stopRotation to true
disp("s key-pressed simulation will stop...")
end
end
% Function to draw the shape
function drawshape(matrix, colour)
x = matrix(1,:);
y = matrix(2,:);
plot(x, y, colour, 'LineWidth', 2);
end
% Function to rotate the shape about a point (p,q) by angle a
function newShape = rotateabout(shape, p, q, a)
% Translate shape to origin
shape1 = translateShape(shape, -p, -q);
% Rotate shape
shape2 = rotateShape(shape1, a);
% Translate shape back
newShape = translateShape(shape2, p, q);
end
% Function to translate the shape
function newShape = translateShape(shape, xShift, yShift)
x = shape(1,:) + xShift;
y = shape(2,:) + yShift;
newShape = [x; y];
end
% Function to rotate the shape
function newShape = rotateShape(shape, a)
% Rotation matrix
reflectMatrix = [cos(a) -sin(a); sin(a) cos(a)];
% Apply rotation
newShape = reflectMatrix * shape;
end
This code will create an animation of a fan with blades rotating. Pressing the 's' key during the animation will stop it. Note that the global stopRotation is used to allow the keyPressCallback function to modify the stopRotation variable. Make sure to execute the fan_animation() function to start the animation.
Note
Pressing the 's' key well stop the animation.
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems
  • Electrical and Electronics Engineering
  9 Comments
JIayun
JIayun on 1 Jan 2024
Edited: JIayun on 1 Jan 2024
thank you very much. Another question is why do we need to set a fan_animation function
Hassaan
Hassaan on 1 Jan 2024
Edited: Hassaan on 1 Jan 2024
@JIayun fan_animation() is what actually implements the animation code loop. For your case think of it like a main() function having all the required function calling.
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems
  • Electrical and Electronics Engineering

Sign in to comment.

More Answers (0)

Categories

Find more on Language Fundamentals 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!