using keyboard to control

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

 Respuesta aceptada

Hassaan
Hassaan el 31 de Dic. de 2023
Editada: Hassaan el 31 de Dic. de 2023

0 votos

% 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 comentarios

JIayun
JIayun el 1 de En. de 2024
what is the use of global variables?thx
Walter Roberson
Walter Roberson el 1 de En. de 2024
In your original code, stoprotation is local to the script, and local to the function keyPressCallback .
In order for the variable to be shared, you would have had to wrap a function around everything -- it is a limitation of MATLAB.
JIayun
JIayun el 1 de En. de 2024
thank you for your reply. what do you mean by shared?
JIayun
JIayun el 1 de En. de 2024
Can I take it that if there is no global varaible, if i pressed 's', stopRotation is assigned for true. but initially, stopRotation is assigned for false, the value for stopRotation is still unchange? Please correct me if my understanding is wrong.
Hassaan
Hassaan el 1 de En. de 2024
@JIayun global keyword is used so that the same variable can be accessed or modified from different functions else the variable will be local
JIayun
JIayun el 1 de En. de 2024
we first declear global varaible in the main function, why do we need to declear again in keyPressCallback function
Hassaan
Hassaan el 1 de En. de 2024
@JIayun In MATLAB, the global keyword is used to declare variables that are accessible in multiple function workspaces. If you declare a variable as global in one function, you must also declare it as global in any other function that intends to access or modify that variable. This is because each function in MATLAB has its own local workspace, and variables in one function are not accessible in another function unless they are explicitly declared as global.
JIayun
JIayun el 1 de En. de 2024
Editada: JIayun el 1 de En. de 2024
thank you very much. Another question is why do we need to set a fan_animation function
Hassaan
Hassaan el 1 de En. de 2024
Editada: Hassaan el 1 de En. de 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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre MATLAB en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 31 de Dic. de 2023

Editada:

el 1 de En. de 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by