% mfile GetSharkDirection % compute force on sharks % % start with random direction force = randn(d,d) + II * randn(d,d); % % add in electrostatic attraction to fish % sharkpnt points to sharks sharkpnt = find(shark(2:d+1,2:d+1)); % sharkwhere contains shark locations (as complex numbers) sharkwhere = ZZ(sharkpnt); nsharks = length(sharkwhere); % fishpnt points to fish fishpnt = find(fish(2:d+1,2:d+1)); % fishwhere contains fish locations (as complex numbers) fishwhere = ZZ(fishpnt); nfish = length(fishwhere); % distances(i,j) = position of fish i - position of shark j if (nsharks > 0 & nfish > 0), distances = fishwhere*ones(1,nsharks) - ones(nfish,1)*conj(sharkwhere'); else distance =[]; end % compute force = sum( distance./abs(distance).^2) distantsharkhunger = SHARKATTRACT*(sum(1 ./ distances))'; force(sharkpnt) = force(sharkpnt) + distantsharkhunger; % % add in global current force = force + Current(ZZ); % % locate fish who are nearest neighbors fishU = fish(1:d,2:d+1); fishD = fish(3:d+2,2:d+1); fishL = fish(2:d+1,1:d); fishR = fish(2:d+1,3:d+2); % decide if shark will eat eatnow = ( fishU.* II .*rand(d,d) + fishD.*(-II).*rand(d,d) + ... fishL.*( -1).*rand(d,d) + fishR.*( 1).*rand(d,d)); eatnow = EATNOW * eatnow ./ max(1e-5,abs(eatnow)); force = force + eatnow .* shark(2:d+1,2:d+1); % % discretize force into (1=R, 2=U, 3=L, 4=D, later 0 = no move) intforce = round(angle(force)*(2/pi)); intforce = intforce + 4*(intforce < 0); sharkdir(2:d+1,2:d+1)=(intforce+1) .* shark(2:d+1,2:d+1); % if (dbug == 1), disp('got shark direction'), keyboard, end