function mirror(src,mic,room,radius)
%% PRIKLAD VOLANI - mirror([2 3],[4 1],[6 4],20)
% src = souradnice zdroje [x y]
% mic = souradnice mikrofonu [xm ym]
% room = rozmery mistnosti [a b]
% c = rychlost zvuku m/s
% radius = zadana vzdalenost, do ktere se bude merit
clc
c = 343;
nx = 10;
ny = 15;

fs = 96000;
ts = 1/fs;

xPlus = zeros(1,nx);
xMinus = zeros(1,nx);
yPlus = zeros(1,ny);
yMinus = zeros(1,ny);

% doplnkove souradnice zdroje
src_ = [abs(room(1)-src(1)) abs(room(2)-src(2))];

% vytvoreni souradnic X
for i = 1:nx
	if mod(i,2) == 1
		xPlus(i) = (i-1)*room(1)+src(1);
	else
		xPlus(i) = (i-1)*room(1)+src_(1);
	end
	xMinus(nx+1-i) = -xPlus(i);
end

% vytvoreni souradnic Y
for i = 1:ny
	if mod(i,2) == 1
		yPlus(i) = (i-1)*room(2)+src(2);
	else
		yPlus(i) = (i-1)*room(2)+src_(2);
	end
	yMinus(ny+1-i) = -yPlus(i);
end

x = [xMinus, xPlus];
y = [yMinus, yPlus];

% vytvoreni moznych kombinaci souradnic zdroju
l_x = length(x);
l_y = length(y);
image_source = zeros(2,l_x*l_y);
for i = 1:l_x
	for j = 1:l_y
		image_source(:,(i-1)*l_y+j) = [x(i);y(j)];
	end
end
image_source;

count = length(image_source);

% vypocet vzdalenosti pro zdroje, ktere jsou v zadanem okruhu
for i = 1:2:2*count
		tmp = sqrt((abs(image_source(i)-mic(1)))^2 + (abs(image_source((i+1))-mic(2)))^2);
		if (tmp < radius)
			distance_image(round(i/2)) = tmp;
			% ulozeni indexu pouzitelneho zdroje
			indexes(round(i/2)) = i;
		end
end

% vymaze nulove hodnoty
distance_image(:, find(abs(distance_image) == 0)) = [];

indexes(:, find(abs(indexes) == 0)) = [];
l_i = length(indexes);

% sestaveni pole pouzitelnych zdroju
useful_source = [image_source(indexes); image_source(indexes+1)];

distance = sqrt((abs(src(1)-mic(1)))^2 + (abs( src(2)-mic(2)))^2);
distance_image;

sound_level = 100*ones(1,l_i);

%% UBYTEK SE VZDALENOSTI
decrease = zeros(1,l_i);
%% PRO 2D
sound_level = 100 ./(distance_image);

%% PRO 3D 
%sound_level = 100 ./(distance_image.*distance_image);

%e = exp(-distance_image);
%sound_level = sound_level.*e;

%% VYPOCET UTLUMU
walls = zeros(2,l_i);

for i = 1:2:2*l_i
	walls([i;i+1]) = [abs(floor(useful_source(i)/room(1))); abs(floor(useful_source(i+1)/room(2)))]; % pocet zdi ve smeru X a Y
	sum_walls(round(i/2)) = walls(i) + walls(i+1);
	sound_level(round(i/2)) = power(0.9,sum_walls(round(i/2)))*sound_level(round(i/2)); % utlumeni na zaklade pruchodu zdi
	if (mod(sum_walls(round(i/2)),2) == 1)
		sound_level(round(i/2)) = -1*sound_level(round(i/2));
	end
end
sum_walls;

%% UPRAVA VYSLEDKU
sound_level(find(sound_level > 100)) = 100; 
%sound_level(find(sound_level < 10e-6)) = 0; % hodnoty nizsi nez 10^-6 se neuvazuji

time = distance / c;
time_image = distance_image ./ c;

% navzorkovani se vzorkovaci periodou ts
samples = floor(time_image ./ ts);
time_image = samples.*ts;

%% SOUCET ZVUKU, KTERE DOPADNOU VE STEJNY CAS
% v tmp jsou ulozeny indexy prvku se stejnymi hodnotami
% sound_sum je soucet stejnych hodnot
for i = 1:2:2*l_i 
	tmp = find(time_image == time_image(round(i/2)));
	sound_sum(round(i/2)) = sum(sound_level(tmp));
	%sound_level(tmp) = 0; % vynuluje stejné hodnoty a ponechá pouze jednu //v grafu zobrazuje 0
end
sound_sum;

% zobrazeni vysledku
results = [useful_source; distance_image; sound_level; walls; time_image]


h = stem(time_image, sound_sum);
set(h,'marker','.')
title('')
xlabel('cas [s]')
ylabel('intenzita zvuku [%]')
grid
print('fig.eps','-deps')
%set(gca,'XTick',0:2:1000)