Skip to content

Instantly share code, notes, and snippets.

@timon-schelling
Created December 12, 2024 23:34
Show Gist options
  • Save timon-schelling/b3318e29368e9f621c7690d390c3b2ae to your computer and use it in GitHub Desktop.
Save timon-schelling/b3318e29368e9f621c7690d390c3b2ae to your computer and use it in GitHub Desktop.
matlab analog clock
function analog_clock()
fig = figure('Name','Analog Clock (Imag) - Mirrored','NumberTitle','off','Color',[1 1 1]);
ax = axes('Parent',fig);
axis(ax,'equal');
axis(ax,[-1.2 1.2 -1.2 1.2]);
axis off
hold on
th = linspace(0,2*pi,1000);
z_circle = exp(1j*th);
plot(real(z_circle), imag(z_circle), 'k', 'LineWidth', 2);
% Hours
for hourMark = 1:12
baseAngle = (hourMark/12)*2*pi;
angle = -baseAngle + pi/2;
z_outer = exp(1j*angle);
z_inner = 0.85*exp(1j*angle);
plot([real(z_inner) real(z_outer)], [imag(z_inner) imag(z_outer)], 'k', 'LineWidth', 3);
z_text = 0.75*exp(1j*angle);
text(real(z_text), imag(z_text), num2str(hourMark), ...
'HorizontalAlignment','center','VerticalAlignment','middle','FontSize',12);
end
% Minutes
for minuteMark = 1:60
baseAngle = (minuteMark/60)*2*pi;
angle = -baseAngle + pi/2;
z_outer = exp(1j*angle);
if mod(minuteMark,5)~=0
z_inner = 0.95*exp(1j*angle);
plot([real(z_inner) real(z_outer)], [imag(z_inner) imag(z_outer)], 'k', 'LineWidth', 1);
end
end
hourHandLength = 0.5;
minuteHandLength = 0.75;
secondHandLength = 0.9;
hourHand = plot([0 0],[0 hourHandLength],'LineWidth',4,'Color','k');
minuteHand = plot([0 0],[0 minuteHandLength],'LineWidth',2,'Color','k');
secondHand = plot([0 0],[0 secondHandLength],'LineWidth',1,'Color','r');
while ishandle(fig)
c = clock; % [year month day hour min sec]
hourVal = c(4);
minuteVal = c(5);
secondVal = round(c(6));
hourAngle = -((hourVal + minuteVal/60)/12)*2*pi + pi/2;
minuteAngle = -(minuteVal/60)*2*pi + pi/2;
secondAngle = -(secondVal/60)*2*pi + pi/2;
z_hourHandTip = hourHandLength*exp(1j*hourAngle);
z_minuteHandTip = minuteHandLength*exp(1j*minuteAngle);
z_secondHandTip = secondHandLength*exp(1j*secondAngle);
set(hourHand,'XData',[0 real(z_hourHandTip)], 'YData',[0 imag(z_hourHandTip)]);
set(minuteHand,'XData',[0 real(z_minuteHandTip)], 'YData',[0 imag(z_minuteHandTip)]);
set(secondHand,'XData',[0 real(z_secondHandTip)], 'YData',[0 imag(z_secondHandTip)]);
drawnow;
pause(1);
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment