# display clock with minute, hour, and seconds hand
# Jerry Stratton hoboes.com/coco

%xcenter% = 128
%ycenter% = 96
%second% = 0

cls
wrap-center \^LHigh resolution or \^Lcolors?
print
loop
	%mode$=inkey$
endloop unless (%mode$ <> "H" and %mode$ <> "C")

wrap-center Enter the hour and minute to start the clock at. Once the clock circle forms, press any key to start the clock.
print

loop
	input "Hour:minute"; %hour%, %minute%
endloop unless (%hour% < 0 or %minute% < 0 or %hour% > 23 or %minute% > 59)
if %hour% >= 12 then %hour% -= 12

switch
case (%mode$ = "H")
	pmode 4,1
	color 2,1
	%delay% = 297
case (%mode$ = "C") #colors are orange(0), white(1), green(2), magenta(3)
	pmode 3,1
	color 3
	%delay% = 303
endswitch

#switch to graphics screen
pcls
screen 1,1

circle(%xcenter%,%ycenter%),%ycenter%-1
circle(%xcenter%,%ycenter%),%ycenter%-2
circle(%xcenter%,%ycenter%),%ycenter%-3

%firstTime% = 1
color 2

pause

loop
	#second hand
	line(%xcenter%,%ycenter%)-(%secondX%,%secondY%),preset
	%length% = 88
	%degrees% = %second%
	gosub getEndpoint
	%secondX% = x
	%secondY% = y
	line(%xcenter%,%ycenter%)-(%secondX%,%secondY%),pset

	switch
	case (%second% = 0 or %second%-1 = %minute%) #minute hand
		line(%xcenter%,%ycenter%)-(%minuteX%,%minuteY%),preset
		%length% = 77
		%degrees% = %minute%
		gosub getEndpoint
		%minuteX% = x
		%minuteY% = y
		line(%xcenter%,%ycenter%)-(%minuteX%,%minuteY%),pset

	case (%second% = 0 and %minute% = 0 or %second%-1 = %hour%*5 or %firstTime% = 1) #hour hand
		line(%xcenter%,%ycenter%)-(%hourX%, %hourY%),preset
		%length% = 55
		%degrees% = %hour%*5
		gosub getEndpoint
		%hourX% = x
		%hourY% = y
		line(%xcenter%,%ycenter%)-(%hourX%,%hourY%),pset
	endswitch

	circle(%xcenter%,%ycenter%),8

	for i = 1 to %delay%:next i

	%second%++
	switch
	case (%second% = 60)
		%second%=0
		%minute%++
	case (%minute% = 60)
		%minute%=0
		%hour%++
	case (%hour% = 12)
		%hour%=0
	endswitch
	%firstTime% = 0
endloop

#endpoint using length and degrees
sub getEndpoint
	#adjust clock seconds to radians
	%degrees% *= 6
	%degrees% -= 90
	%radians% = %degrees%/57.29577951

	#calculate cartesian x and y from polar coordinates
	x = %length% * cos(%radians%) + %xcenter%
	y = %length% * sin(%radians%) + %ycenter%
endsub
