Skip to content

Commit e0b63b0

Browse files
committed
refactor update dots
1 parent c6d66f4 commit e0b63b0

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

reseedDots.m

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function dots = reseedDots(dots, cfg)
2+
3+
% Create a logical vector to detect any dot that has:
4+
% - an xy position inferior to 0
5+
% - an xy position superior to winWidth
6+
% - has exceeded its liftime
7+
% - has been been picked to be killed
8+
N = any([ ...
9+
dots.positions > cfg.screen.winWidth, ...
10+
dots.positions < 0, ...
11+
dots.time > dots.lifeTime, ...
12+
rand(cfg.dot.number, 1) < cfg.dot.proportionKilledPerFrame], 2) ;
13+
14+
% If there is any such dot we relocate it to a new random position
15+
% and change its lifetime to 1
16+
if any(N)
17+
dots.positions(N, :) = rand(sum(N), 2) * cfg.screen.winWidth;
18+
dots.time(N, 1) = 1;
19+
end
20+
21+
end

updateDots.m

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,13 @@
66
if all(isnan(dots.positions(:)))
77
errorStruct.message = 'All dots position have NaN values.';
88
errorStruct.identifier = 'updateDots:onlyNans';
9-
9+
1010
error(errorStruct);
1111
end
1212

13-
% Create a logical vector to detect any dot that has:
14-
% - an xy position inferior to 0
15-
% - an xy position superior to winWidth
16-
% - has exceeded its liftime
17-
% - has been been picked to be killed
18-
N = any([ ...
19-
dots.positions > cfg.screen.winWidth, ...
20-
dots.positions < 0, ...
21-
dots.time > dots.lifeTime, ...
22-
rand(cfg.dot.number, 1) < cfg.dot.proportionKilledPerFrame], 2) ;
23-
24-
% If there is any such dot we relocate it to a new random position
25-
% and change its lifetime to 1
26-
if any(N)
27-
dots.positions(N, :) = rand(sum(N), 2) * cfg.screen.winWidth;
28-
dots.time(N, 1) = 1;
29-
end
13+
dots = reseedDots(dots, cfg);
3014

3115
% Add one frame to the dot lifetime to each dot
3216
dots.time = dots.time + 1;
33-
17+
3418
end

0 commit comments

Comments
 (0)