%% regular n-gon billiard table teps = 10^(-10); % set up wall parameters wW,wC where real(wW(i)'.z) = wC(i) N=5; I = sqrt(-1); wW = exp(2*pi*I*(0:N-1)/N) wC = ones(1,N) %% N = length(wW); tX =[]; tY =[]; for j = 1:N k = mod(j+N,N)+1; A = [real(wW(j)), imag(wW(j)); real(wW(k)), imag(wW(k))] B = [wC(j); wC(k)]; X = A\B; tX = [tX,X(1)]; tY = [tY,X(2)]; end tX = [tX,tX(1)]; tY = [tY,tY(1)]; %% graph pool table figure(1) hold off h = plot(tX,tY,'r-'); set(h,'LineWidth',3); axis equal hold on %% set up billiard shot % set up initial point z0 = 0.1; w0 = I; w0 = w0/abs(w0); ball = plot(real(z0),imag(z0),'bo'); set(ball,'LineWidth',3); %% iterate bounces numits = 600; pauseeps = 0.01; z = z0; w = w0; disp ('press a key to start'); pause; for k=1:numits times = (wC'-real(wW'*z))./real(wW'*w); mt = min(times(find(times > teps))); wallnum = min(find(times==mt)); nextz = z+mt*w; w1 = wW(wallnum); nextw = -(w1)^2/w; nextw = nextw/abs(nextw); pause(pauseeps); drawnow; plot([real(z) real(nextz)],[imag(z) imag(nextz)],'k-'); z = nextz; w = nextw; end; %% hold off