understanding rects/coordinates in psychtoolbox

6 ビュー (過去 30 日間)
Jaymie Sway
Jaymie Sway 2023 年 1 月 31 日
回答済み: Abhinav Aravindan 2024 年 11 月 15 日
Hello,
I am trying to write a program that seperates my screen into four quadrants. A circle will randomly appear at the center of each quadrant and the participant must press the spacebar once it appears.
However, I am unsure how to create four rects that are centered at each quadrant. Could anyone provide some knowledge on how I could calculate coordinates/rects. Thank you so much. Much appreciated.

回答 (1 件)

Abhinav Aravindan
Abhinav Aravindan 2024 年 11 月 15 日
Hi Jaymie,
To create a program where circles randomly appear in each quadrant and move to a different quadrant when the "Spacebar" is pressed, you can utilize Figure Callbacks in MATLAB. Below is a sample program to help you get started:
function continuous_random_quadrant_circle_on_space()
figWidth = 800;
figHeight = 600;
% Calculate centers of each quadrant within the fixed size
centers = [
figWidth/4, figHeight/4; % Quadrant 1
3*figWidth/4, figHeight/4; % Quadrant 2
figWidth/4, 3*figHeight/4; % Quadrant 3
3*figWidth/4, 3*figHeight/4 % Quadrant 4
];
radius = 50; % Adjust as needed
% Create a figure window
fig = figure('Position', [100, 100, figWidth, figHeight]); % Set figure size
set(fig, 'Color', [1 1 1]); % Set background color to white
axis equal;
axis([0 figWidth 0 figHeight]);
set(gca, 'YDir', 'reverse'); % Reverse y-axis to match screen coordinates
set(gca, 'XTick', [], 'YTick', []); % Remove axis ticks
% Set the callback
set(fig, 'WindowKeyPressFcn', @keyPressCallback);
disp('Press the spacebar to move the circle. Press "E" to exit.');
% Initial display
updateCircle();
% Function to update circle position
function updateCircle()
if ishandle(fig)
% Randomly select one of the quadrant centers
idx = randi(4);
centerX = centers(idx, 1);
centerY = centers(idx, 2);
clf;
hold on;
axis equal;
axis([0 figWidth 0 figHeight]);
set(gca, 'YDir', 'reverse');
set(gca, 'XTick', [], 'YTick', []);
set(fig, 'Color', [1 1 1]);
% Display the circle at the randomly selected quadrant center
rectangle('Position', [centerX-radius, centerY-radius, 2*radius, 2*radius], ...
'Curvature', [1, 1], 'FaceColor', 'r');
disp(['Circle moved to Quadrant ', num2str(idx)]);
end
end
% Callback function to handle key presses
function keyPressCallback(~, event)
if strcmp(event.Key, 'space')
updateCircle();
elseif strcmp(event.Key, 'e')
disp('Exiting program...');
if ishandle(fig)
close(fig);
end
end
end
end
Output:
Please find the related documentation below for more detail:

カテゴリ

Help Center および File ExchangeInstallation and Operational Settings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by