Storm motion over a catchment - fill matrix in a generic direction
1 view (last 30 days)
Show older comments
Hi everybody,
i'd like to simulate motion of a storm with a specific direction over a catchment, with the goal to individuate at any time-step the catchment's cells covered by the storm. (the storm is supposed to cover the entire catchment at the end of the process)
My idea was to start with a generic catchment surface, represented by a matrix C=zeros(m,n). then i assigned a ratio, rows(dm)/column(dn), that represent the storm direction (i want to control the direction and also the speed) i had the intended to expand this ration over the basin as show in the figure below, but i'm I am having some troubles.
i report the part of the code that i had written (is incomplete and does not work as i want, but maybe can help to understand how i had set up (maybe wrong) the problem ). I will appreciate any help or advice.
thanks
% %
C=zeros(100,100); %are of interest
%
% individuate direction by defining a ratio between rows and column dm/dn
dm = 1; %number of cells along rows
dn = 1; %number of cells along columns
%
v = 1 ;% scale factor for storm velocity
t = 20;% time step of storm passage simulation
%
ST= zeros(size(C,1),size(C,2),t) ; %storm matrix preallocation
%
for k=1:t
%
ST( 1 , 1:dn*k , k ) = 1;
ST( 1:dm*k , 1 , k ) = 1;
% ?????
% ?????
% ?????
end
0 Comments
Accepted Answer
Chad Greene
on 24 May 2015
How's this?
[X,Y] = meshgrid(1:200,1:200);
t = 1:20;
x0 = 0.2*rand(size(t));
y0 = 0.3*rand(size(t));
r = 20*t + 10*rand(size(t));
NOP=100; % number of points in circle
ItHasRained = zeros(size(X));
h1 = imagesc(ItHasRained);
caxis([0 1])
colormap([0 0 0; 0.5843 0.8157 0.9882])
hold on
axis([0 200 0 200])
cb = colorbar;
set(cb,'ytick',[0 1],'yticklabel',{'dry';'wet'})
for ti = t
x = x0(ti)+r(ti).*cos( 2*pi/NOP*(1:NOP));
y = y0(ti)+r(ti).*sin( 2*pi/NOP*(1:NOP));
ItHasRained(inpolygon(X,Y,x,y))=1;
set(h1,'cdata',ItHasRained)
h2 = plot(x,y,'r-','linewidth',2);
drawnow
pause(.05)
delete(h2)
end
More Answers (1)
Walter Roberson
on 24 May 2015
In order to travel through the matrix in a given direction, you should have a look at Bresenham's Line Algorithm. Chad's Answer is fine but it deals with plotting not with individual entries in a matrix; it is easy to get off of a straight line when you have to quantize down to individual locations.
I posted a formulation that does not require sin() and cos() a couple of hours ago here . It creates floating point outputs and so has some quantization difficulties; however, if you keep the values in floating point and round() them then you can get to indices. Be cautioned, though, that Bresenham's Algorithm produces straighter lines than working with floating point and rounding to integers.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!