首页 > 其他 > 详细

PTBtutorials—Animating basic shapes and dots(1)

时间:2020-04-02 18:04:02      阅读:68      评论:0      收藏:0      [点我收藏+]

导读:

 

 

1.Moving Dot Grid Demo: 让点状网格在屏幕上呈正弦式向左或向右移动

注:关键在于点坐标的设置,可以将时间看做变量,使用sin函数,然后每帧都刷新,即可获得动态效果

% Use the meshgrid command to create our base dot coordinates     获得网格点坐标
dim = 10;
[x, y] = meshgrid(-dim:1:dim, -dim:1:dim);

% Scale this by the distance in pixels we want between each dot       调整坐标尺度
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 into two vectors   获取点的坐标向量
xPosVector = reshape(x, 1, numDots);
yPosVector = 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 = [screenXpixels / 2 screenYpixels / 2];

% Set the color of our dot to be random  设置颜色
dotColors = rand(3, numDots);

% Set the size of the dots randomly between 10 and 30 pixels   设置点的大小为10-30之间
dotSizes = rand(1, numDots) .* 20 + 10;

% Our grid will oscilate with a sine wave function to the left and right  设置正弦函数的参数
% of the screen. These are the parameters for the sine wave
% See: http://en.wikipedia.org/wiki/Sine_wave
amplitude = screenYpixels * 0.25;  波幅
frequency = 0.2;  频率
angFreq = 2 * pi * frequency;  角频率
startPhase = 0;  初始相位
time = 0;  时间

% Sync us and get a time stamp  获取时间戳
vbl = Screen(Flip, window);
waitframes = 1;

% Maximum priority level  设置优先级
topPriorityLevel = MaxPriority(window);
Priority(topPriorityLevel);

% Loop the animation until a key is pressed  循环
while ~KbCheck

    % Position of the square on this frame  某一帧上的位置
    gridPos = amplitude * sin(angFreq * time + startPhase);

    % Draw all of our dots to the screen in a single line of code adding  绘制
    % the sine oscilation to the X coordinates of the dots
    Screen(DrawDots, window, [xPosVector + gridPos; yPosVector],...
        dotSizes, dotColors, dotCenter, 2);

    % Flip to the screen
    vbl  = Screen(Flip, window, vbl + (waitframes - 0.5) * ifi);

    % Increment the time
    time = time + ifi;

end

 

2.Scaled Dot Grid Demo:如何扩展和收缩绘制在屏幕上的点网格。点的大小也随之调节

注:大体思路与第一个示例一致。主要是将点的坐标和点的大小设置为与时间相关的函数

% Use the meshgrid command to create our base dot coordinates
dim = 10;
[x, y] = meshgrid(-dim:1:dim, -dim:1:dim);

% Scale this by the distance in pixels we want between each dot
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 into two vectors
xPosVector = reshape(x, 1, numDots);
yPosVector = 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 = [screenXpixels / 2 screenYpixels / 2];

% Set the color of our dot to be random
dotColors = rand(3, numDots);

% Set the maximum size of the dots to 25 pixels   设置点最大为25pixel
maxDotSize = 25;

% Our grid will pulse by taking the absolute value of a sine wave, we
% therefore set the amplitude of the sine wave to 1 as this will be a
% multiplicative scale factor ranging between 0 and 1.
% With 0 the dots will all be on top of one another. With 1 the gird will
% have its maximum size.
% These are the parameters for the sine wave
% See: http://en.wikipedia.org/wiki/Sine_wave
amplitude = 1;
frequency = 0.05;
angFreq = 2 * pi * frequency;
startPhase = 0;
time = 0;

% Sync us and get a time stamp
vbl = Screen(Flip, window);
waitframes = 1;

% Maximum priority level
topPriorityLevel = MaxPriority(window);
Priority(topPriorityLevel);

% Loop the animation until a key is pressed
while ~KbCheck

    % Scale the grid coordinates         调整网格坐标,这里使用了绝对值函数
    scaleFactor = abs(amplitude * sin(angFreq * time + startPhase));

    % Scale the dot size. Here we limit the minimum dot size to one pixel   调整点的大小(1~25),这里用最大值函数
    % by using the max function as PTB wont allow the dot size to scale to
    % zero (sensibly, as youd be drawing no dots at all)
    thisDotSize = max(1, maxDotSize .* scaleFactor);

    % Draw all of our dots to the screen in a single line of code adding
    % the sine oscilation to the X coordinates of the dots
    Screen(DrawDots, window, [xPosVector; yPosVector] .* scaleFactor,...
        thisDotSize, dotColors, dotCenter, 2);

    % Flip to the screen
    vbl  = Screen(Flip, window, vbl + (waitframes - 0.5) * ifi);

    % Increment the time
    time = time + ifi;

end

 

3.Moving Square Demo:使一个正方形在屏幕上呈正弦向左或向右移动

注:大体思路和第一个示例一致,只是将目标换成了矩形

% Make a base Rect of 200 by 200 pixels
baseRect = [0 0 200 200];

% Set the color of the rect to red
rectColor = [1 0 0];

% Our square will oscilate with a sine wave function to the left and right
% of the screen. These are the parameters for the sine wave
% See: http://en.wikipedia.org/wiki/Sine_wave
amplitude = screenXpixels * 0.25;
frequency = 0.2;
angFreq = 2 * pi * frequency;
startPhase = 0;
time = 0;

% Sync us and get a time stamp
vbl = Screen(Flip, window);
waitframes = 1;

% Maximum priority level
topPriorityLevel = MaxPriority(window);
Priority(topPriorityLevel);

% Loop the animation until a key is pressed
while ~KbCheck

    % Position of the square on this frame    每一帧上方形各点的x坐标变化量
    xpos = amplitude * sin(angFreq * time + startPhase);

    % Add this position to the screen center coordinate. This is the point  调整到以屏幕中心点为中心
    % we want our square to oscillate around
    squareXpos = xCenter + xpos;

    % Center the rectangle on the centre of the screen
    centeredRect = CenterRectOnPointd(baseRect, squareXpos, yCenter);

    % Draw the rect to the screen
    Screen(FillRect, window, rectColor, centeredRect);

    % Flip to the screen
    vbl  = Screen(Flip, window, vbl + (waitframes - 0.5) * ifi);

    % Increment the time
    time = time + ifi;

end

 

4.Multiple Moving Squares Demo: 使两个正方形在屏幕上以正弦方式移动,具有不同的颜色和不同的横向偏移

注:和示例三的思路一致,只是多加了一个矩形。

% Make a base Rect of 200 by 200 pixels
baseRect = [0 0 200 200];

% Set the color of the top rect to red and the bottom blue
topColor = [1 0 0];
bottomColor = [0 0 1];

% Our square will oscilate with a sine wave function to the left and right
% of the screen. These are the parameters for the sine wave
% See: http://en.wikipedia.org/wiki/Sine_wave
amplitude = screenXpixels * 0.25;
frequency = 0.2;
angFreq = 2 * pi * frequency;
time = 0;

% Our two squares will be pi out of phase   初始位置反相
startPhaseOne = 0;
startPhaseTwo = pi;

% Sync us and get a time stamp
vbl = Screen(Flip, window);
waitframes = 1;

% Maximum priority level
topPriorityLevel = MaxPriority(window);
Priority(topPriorityLevel);

% Loop the animation until a key is pressed
while ~KbCheck

    % Position of the two squares on this frame           每一帧中心点位置变化量
    xposOne = amplitude * sin(angFreq * time + startPhaseOne);
    xposTwo = amplitude * sin(angFreq * time + startPhaseTwo);

    % Add this position to the screen center coordinate. This is the point  矩形中心点的横坐标
    % we want our squares to oscillate around
    squareXposOne = xCenter + xposOne;
    squareXposTwo = xCenter + xposTwo;

    % Center the rectangle on the centre of the screen     获取矩形每帧的坐标
    centeredRectOne = CenterRectOnPointd(baseRect, squareXposOne,...
        screenYpixels * 0.25);
    centeredRectTwo = CenterRectOnPointd(baseRect, squareXposTwo,...
        screenYpixels * 0.75);

    % Draw the rect to the screen
    Screen(FillRect, window, [topColor bottomColor],...
        [centeredRectOne centeredRectTwo]);

    % Flip to the screen
    vbl  = Screen(Flip, window, vbl + (waitframes - 0.5) * ifi);

    % Increment the time
    time = time + ifi;

end

 

5.Moving Colour Change Square Demo: 使一个正方形在屏幕上呈正弦运动,其颜色根据其屏幕位置而动态变化

 注:在此demo中,矩形是在红蓝两个色调之间做正弦变换。在示例3的基础上,进一步做颜色的变化

% Make a base Rect of 200 by 200 pixels
baseRect = [0 0 200 200];

% Our square will oscilate with a sine wave function to the left and right
% of the screen. These are the parameters for the sine wave
% See: http://en.wikipedia.org/wiki/Sine_wave
amplitude = screenXpixels * 0.25;
frequency = 0.2;
angFreq = 2 * pi * frequency;
startPhase = 0;
time = 0;

% Sync us and get a time stamp
vbl = Screen(Flip, window);
waitframes = 1;

% Maximum priority level
topPriorityLevel = MaxPriority(window);
Priority(topPriorityLevel);

% Loop the animation until a key is pressed
while ~KbCheck

    % Position of the square on this frame
    xpos = amplitude * sin(angFreq * time + startPhase);

    % Use the x position of the square to determine its color  根据x坐标来确定颜色变化率
    colorMultiplier = abs(xpos) / amplitude;

    % Add this position to the screen center coordinate. This is the point
    % we want our square to oscillate around
    squareXpos = xCenter + xpos;

    % Center the rectangle on the centre of the screen
    centeredRect = CenterRectOnPointd(baseRect, squareXpos, yCenter);

    % Draw the rect to the screen
    Screen(FillRect, window, [1 - colorMultiplier 0 colorMultiplier],...
        centeredRect);

    % Flip to the screen
    vbl  = Screen(Flip, window, vbl + (waitframes - 0.5) * ifi);

    % Increment the time
    time = time + ifi;

end

 

6.Moving Size Change Square Demo: 使一个正方形在屏幕上呈正弦式横向运动,其大小取决于其屏幕上的位置

注:对矩形的中心点进行正弦式变化

% Make a base Rect of 200 by 200 pixels
baseRect = [0 0 200 200];

% Set the color of the rect to red
rectColor = [1 0 0];

% Our square will oscilate with a sine wave function to the left and right
% of the screen. These are the parameters for the sine wave
% See: http://en.wikipedia.org/wiki/Sine_wave
amplitude = screenXpixels * 0.25;
frequency = 0.2;
angFreq = 2 * pi * frequency;
startPhase = 0;
time = 0;

% Sync us and get a time stamp
vbl = Screen(Flip, window);
waitframes = 1;

% Maximum priority level
topPriorityLevel = MaxPriority(window);
Priority(topPriorityLevel);

% Loop the animation until a key is pressed
while ~KbCheck

    % Position of the square on this frame   确定每一帧的位移量
    xpos = amplitude * sin(angFreq * time + startPhase);

    % Use the x position of the square to determine its color  利用x坐标在确定矩形的颜色,但是实际上并没有用到
    sizeMultiplier = abs(xpos) / amplitude;

    % Add this position to the screen center coordinate. This is the point  设置中心坐标
    % we want our square to oscillate around
    squareXpos = xCenter + xpos;

    % Center the rectangle on the centre of the screen 将矩形调整到以屏幕中心点为中心的位置
    centeredRect = CenterRectOnPointd(baseRect * sizeMultiplier,...
        squareXpos, yCenter);

    % Draw the rect to the screen
    Screen(FillRect, window, rectColor, centeredRect);

    % Flip to the screen
    vbl  = Screen(Flip, window, vbl + (waitframes - 0.5) * ifi);

    % Increment the time
    time = time + ifi;

end

 

PTBtutorials—Animating basic shapes and dots(1)

原文:https://www.cnblogs.com/zxpsyneuroscience/p/12619660.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!