Drawing matlab PTB (1)

Using drawing functions such as when drawdots, drawlines, require anti-aliasing can use this statement  Screen ( 'BlendFunction', w, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) 

 

Painting point function

Screen('DrawDots',w,xy,[,size],[,color],[,center],[,dot-type])

xy 2 row vector, contains the center point x (1 row) and y (2 rows) coordinates;

size is the size of the dots, is a row vector, the respective points of the same size if a value;

color Ibid; center by default [00], namely the upper left corner of the screen

dot-type, is rectangular when taken 0, 1 antialiasing circle, circle 2 for high-quality

% The Clear at The Workspace and at The Screen 
SCA; 
use Close All; 
clearvars;

 % Here WE Call some default Settings for Setting up Psychtoolbox 
PsychDefaultSetup ( 2 );

 % . Seed at The Random Number The Generator Here WE use at The AN older Way to BE
 % compatible with older Systems. Newer syntax Would bE RNG ( ' shuffle ' ). Look
 % of AT the Help function rand " Help rand "  for More Information set random seed, such that each time a different random seed experiments, may be employed rng shuffle statement 
rand ( ' SEED ' , SUM ( 100 * clock));

% Get the screen numbers. This gives us a number for each of the screens
% attached to our computer. For help see: Screen Screens?
screens = Screen('Screens');

% Draw we select the maximum of these numbers. So in a situation where we
% have two screens attached to our monitor we will draw to the external
% screen. When only one screen is attached to the monitor we will draw to
% this. For help see: help max
screenNumber = max(screens);

% Define black and white (white will be 1 and black 0). This is because
% luminace values are (in general) defined between 0 and 1.
% For help see: help WhiteIndex and help BlackIndex
white = WhiteIndex(screenNumber);
black = BlackIndex(screenNumber);

% Open an on screen window and color it black
% For help see: Screen OpenWindow?
[window, windowRect] = PsychImaging('OpenWindow', screenNumber, black);

% Get the size of the on screen window in pixels
% For help see: Screen WindowSize?
[screenXpixels, screenYpixels] = Screen('WindowSize', window);

% Get the centre coordinate of the window in pixels
% For help see: help RectCenter
[xCenter, yCenter] = RectCenter(windowRect);

% Enable alpha blending for anti-aliasing
% For help see: Screen BlendFunction?
% Also see: Chapter 6 of the OpenGL programming guide
Screen('BlendFunction', window, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

% Use the meshgrid command to create our base dot coordinates. This will
% simply be a grid of equally spaced coordinates in the X and Y dimensions,
% centered on 0,0
% For help see: help meshgrid   此函数用来生成(二维或三维)网格
dim = 10;
[x, y] = meshgrid(-dim:1:dim, -dim:1:dim);

% Here we scale the grid so that it is in pixel coordinates. We just scale
% it by the screen size so that it will fit. This is simply a
% multiplication. Notive the "." before the multiplicaiton sign. This
% allows us to multiply each number in the matrix by the scaling value.
pixelScale = screenYpixels / (dim * 2 + 2);  按比例对点的坐标进行控制
x = x .* pixelScale;
y = y .* pixelScale;

% Calculate the number of dots
% For help see: help numel
numDots = numel(x);

% Make the matrix of positions for the dots. This need to be a two row
% vector. The top row will be the X coordinate of the dot and the bottom
% row the Y coordinate of the dot. Each column represents a single dot. For
% help see: help reshape  将矩形重新编辑为m*n的格式
dotPositionMatrix = [reshape(x, 1, numDots); reshape(y, 1, numDots)];

% We can define a center for the dot coordinates to be relaitive to. Here
% we set the centre to be the centre of the screen
dotCenter = [xCenter yCenter];

% Set the color of our dot to be random i.e. a random number between 0 and
% 1
dotColors = rand(3, numDots) .* white;

% Set the size of the dots randomly between 10 and 30 pixels
dotSizes = rand(1, numDots) .* 20 + 10;

% Draw all of our dots to the screen in a single line of code
% For help see: Screen DrawDots
Screen('DrawDots', window, dotPositionMatrix,...
    dotSizes, dotColors, dotCenter, 2);

% Flip to the screen. This command basically draws all of our previous
% commands onto the screen. See later demos in the animation section on more
% timing details. And how to demos in this section on how to draw multiple
% rects at once.
% For help see: Screen Flip?
Screen('Flip', window);

% Now we have drawn to the screen we wait for a keyboard button press (any
% key) to terminate the demo. For help see: help KbStrokeWait
KbStrokeWait;

% Clear the screen. "sca" is short hand for "Screen CloseAll". This clears
% all features related to PTB. Note: we leave the variables in the
% workspace so you can have a look at them if you want.
% For help see: help sca
sca;

The effect is as follows:

 

 

Reference: Draw a single point: http://peterscarfe.com/singledotdemo.html

           Meshed point: http://peterscarfe.com/dotgriddemo.html

Guess you like

Origin www.cnblogs.com/zxpsyneuroscience/p/12565017.html
Recommended