PTBtutorials—Drawing basic shapes and dots(1)
导读:(1)画单个点 (2)画多个有不同属性的点 (3)画单个矩形 (4)画多个相同大小矩形 (5)画多个不同大小矩形 (6)画多个相同大小的空心矩形(7)画圆
注:(1)在使用绘图函数如drawdots、drawlines等时,要求反锯齿效果可以采用此语句 Screen(\’BlendFunction\’,w,GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)
(2)DrawDots(坐标,属性)与FillRect(属性,坐标)命令中参数的位置是有差异的
1.Single Dot Demo:在屏幕随机位置画一个点
Screen(\’DrawDots\’,w,xy,[,size],[,color],[,center],[,dot-type]) 在指定窗口。用某种颜色和尺寸于某位置画点
xy为2行向量,包含各点中心的x(1行)与y(2行)坐标;
size是点的大小,是一维向量,若为1个值则各点大小一致(默认为1);
color同上(默认白色,多色时列数需与xy列数一致);center,线条对应的中心店,默认为[0 0],即屏幕左上角
dot-type,取0时为矩形,1为反锯齿圆,2为高质量圆
% Set the color of our dot to full red. dotColor = [1 0 0]; % Determine a random X and Y position for our dot. dotXpos = rand * screenXpixels; dotYpos = rand * screenYpixels; % Dot size in pixels dotSizePix = 20; % Draw the dot to the screen. Screen(\'DrawDots\', window, [dotXpos dotYpos], dotSizePix, dotColor, [], 2); % Flip to the screen. Screen(\'Flip\', window);
效果图
2.Dot Grid Demo:在一行代码中绘制具有不同属性的多个点。在这个例子中,一个由不同位置、大小和颜色的圆点组成的统一网格
(1) [X,Y]=meshgrid(x,y) 生成二维网格 其中参数x,y的形式多为-dim:dim (拓展:[X,Y]=meshgrid(x) 二维网格 [X,Y,Z]=meshgrid(x,y,z) 三维网格 )
% 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 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 计算点的数量 numDots = numel(x); % Make the matrix of positions for the dots. This need to be a two row 制作点的坐标矩阵。是一个2*n的形式,第一行为x坐标,第二行为y坐标 % 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. 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 设置颜色 dotColors = rand(3, numDots) .* white; % Set the size of the dots randomly between 10 and 30 pixels 设置点的大小在10-30之间 dotSizes = rand(1, numDots) .* 20 + 10; % Draw all of our dots to the screen in a single line of code 绘制点 Screen(\'DrawDots\', window, dotPositionMatrix,... dotSizes, dotColors, dotCenter, 2); % Flip to the screen. Screen(\'Flip\', window);
效果图:
3.Square Demo:画一个矩形并将其放置在屏幕上
(1)[NewRect,dh,dv]=CenterRect(rect,fixedrect) 矩形居中至某矩形
rect 待居中的矩形 fixedrect 基准矩形 NewRect 居中后矩形的新坐标值 dh,dv 操作完成后水平/垂直偏移量
(2)newrect=CenterRectOnPoint(rext,x,y)/newrect=CenterRectOnPointd(rext,x,y) 矩形居中至某点
rect 待操作的矩形 x,y 基准点 newrect 新矩形d 坐标值
(3)Screen(\’FillRect\’,w,color,rect) 绘制实心矩形 若一次性绘制多个矩形,则将其坐标值变为列的形式(4行*n列),若各矩形颜色不同,同理(3行*n列)
w 窗口指针 color 颜色,默认黑色,0~255的灰度值或[r g b] rect 矩形,默认为整个窗口,形式[a b c d]
Screen(\’FrameRect\’,w,color,rect,penwidth) 绘制空心矩形,其中penwidth为画笔宽度,默认为1个像素
% Make a base Rect of 200 by 200 pixels. 制作一个200*200像素的矩形 baseRect = [0 0 200 200]; % Center the rectangle on the centre of the screen using fractional pixel values. 将此矩形调整到以某个点为中心,返回值为新矩形的坐标 centeredRect = CenterRectOnPointd(baseRect, xCenter, yCenter); % Set the color of our square to full red. rectColor = [1 0 0]; % Draw the square to the screen. Screen(\'FillRect\', window, rectColor, centeredRect); % Flip to the screen. Screen(\'Flip\', window);
效果图
4.Multiple Squares Demo:同时用不同颜色绘制多个矩形
(1) nan 是not a number的意思,nan(4,3)会生成一个4*3的矩阵,其中的元素都是nan
% Make a base Rect of 200 by 200 pixels 基准矩形 baseRect = [0 0 200 200]; % Screen X positions of our three rectangles 这三个矩形的x坐标 squareXpos = [screenXpixels * 0.25 screenXpixels * 0.5 screenXpixels * 0.75]; numSqaures = length(squareXpos); % Set the colors to Red, Green and Blue 颜色依次为红绿蓝 allColors = [1 0 0; 0 1 0; 0 0 1]; % Make our rectangle coordinates 制作坐标 allRects = nan(4, 3); for i = 1:numSqaures allRects(:, i) = CenterRectOnPointd(baseRect, squareXpos(i), yCenter); end % Draw the rect to the screen Screen(\'FillRect\', window, allColors, allRects); % Flip to the screen Screen(\'Flip\', window);
效果图
5.Different Squares Demo: 绘制不同颜色的大小不等的多个矩形
大体思路和示例4一致,只是对矩形的大小进行了调整。主要在执行矩阵居中命令时体现
% Make a base Rect of 200 by 200 pixels
baseRect = [0 0 200 200];
% Screen X positions of our three rectangles
squareXpos = [screenXpixels * 0.25...
screenXpixels * 0.5 screenXpixels * 0.75];
numSqaures = length(squareXpos);
% Set the colors to Red, Green and Blue
allColors = [1 0 0; 0 1 0; 0 0 1];
% Make a multiplier to modulate the size of our squares
sizeChanger = [0.7 1 1.3];
% Make our rectangle coordinates
allRects = nan(4, 3);
for i = 1:numSqaures
allRects(:, i) = CenterRectOnPointd(baseRect .* sizeChanger(i),...
squareXpos(i), yCenter);
end
% Draw the rect to the screen
Screen(\'FillRect\', window, allColors, allRects);
% Flip to the screen
Screen(\'Flip\', window);
效果图
6.Framed Squares Demo:同时绘制多个空心矩形
(1) Screen(\’FrameRect\’,w,color,rect,penwidth) 参数基本和Screen(\’FillRect\’)命令一致,但是多了penwidth这个参数(画笔宽度,以像素为单位,默认为1)
% Make a base Rect of 200 by 200 pixels
baseRect = [0 0 200 200];
% Screen X positions of our three rectangles
squareXpos = [screenXpixels * 0.25 screenXpixels * 0.5 screenXpixels * 0.75];
numSquares = length(squareXpos);
% Set the colors to Red, Green and Blue
allColors = [1 0 0; 0 1 0; 0 0 1];
% Make our rectangle coordinates
allRects = nan(4, 3);
for i = 1:numSquares
allRects(:, i) = CenterRectOnPointd(baseRect, squareXpos(i), yCenter);
end
% Pen width for the frames
penWidthPixels = 6;
% Draw the rect to the screen
Screen(\'FrameRect\', window, allColors, allRects, penWidthPixels);
% Flip to the screen
Screen(\'Flip\', window);
效果图
7.Oval Demo: 绘制实心圆 /椭圆
(1) Screen(\’FillOval\’,w,color,rect,perfectUoToMaxDiameter) 参数与FillRect命令基本一致,区别在于最后的参数perfectUoToMaxDiameter(用于设置椭圆看上去比较完美的最大直径,默认为整个显示区域的大小,若确知圆大小不超出某个范围,设置此参数可提高运行速度。但是实际中0实心圆,1实心三角形,2实心六边形,3实行八边形…)
% Make a base Rect of 200 by 250 pixels
baseRect = [0 0 200 250];
% For Ovals we set a miximum diameter up to which it is perfect for
maxDiameter = max(baseRect) * 1.01;
% Center the rectangle on the centre of the screen
centeredRect = CenterRectOnPointd(baseRect, xCenter, yCenter);
% Set the color of the rect to red
rectColor = [1 0 0];
% Draw the rect to the screen
Screen(\'FillOval\', window, rectColor, centeredRect, maxDiameter);
% Flip to the screen
Screen(\'Flip\', window);
效果图