The one handed clock problem

Fun little challenge posed by John T at LB:


' One handed clock.bas SmallBASIC 0.12.8 [B+=MGA] 2017-03-25
' fun challenge posted at LB 3/24
' What are the times that the two hands of clock make straight line?

' I think once an hour the minute hand is exactly 180 degrees past the hour hand
' oops! not quite ;-))

cx = 800 / 2 : cy = 600 / 2 'center of clock
times = "" 'save one long armed times here
bkc = rgb(100, 255, 255)
color bkc, bkc
cls
color 1
locate 1, 5 : ? "The One (long) Handed Clock Problem"
locate 25, 5 : ? "Times at which the hands of the clock form a straight line."
color 3
circle cx, cy, 210 filled
for
secs = 0 to 3600 * 12 'get these to the sec

' 360 degrees * fraction of circle hour hand makes to 12:00
hourDeg = 360 * secs / (3600 * 12)
' 360 degrees * fraction of circle minute hand makes to 12:00
minDeg = 360 * secs / 3600

'digital time
hrs = str(int(secs / 3600))
if hrs = "0" then hrs = "12" else hrs = right("00" + hrs, 2)
mins = right("00" + str(int(secs / 60) mod 60), 2)
scs = right("00" + str(secs mod 60), 2)
ctime = hrs + ":" + mins + ":" + scs

' End points of hour amd minute hands: convert angles
' to radians for Sin and Cos such that 12:00:00 = 0 radians
' so minus 90 because JB 0 degrees is due East.
hx = cx + 100 * cos(rad(hourDeg - 90))
hy = cy + 100 * sin(rad(hourDeg - 90))
mx = cx + 140 * cos(rad(minDeg - 90))
my = cy + 140 * sin(rad(minDeg - 90))
'note: hour hand is 100 and minute hand is 140, sum = 240

'the two arms form striaght line:
'when the distance from two end points NEARLY
'equals the sum of length of hands <= .00005
dist = ((hx - mx) ^ 2 + (hy - my) ^ 2) ^ .5

'get the first one within our limit of .00005 for hour
if abs(240 - dist) < .00002 then 'NEARLY straight line
hours = left(ctime, 2)
if hours <> left(lastHour, 2) then
times = times + chr(10) + space(5) + ctime
lastHour = ctime
pF = 1
end if
end if

'every (speeded up) 60 secs or pF =pauseFlag, display update
if secs mod 60 = 0 or pF then
color rgb(0, 255, 200)
circle cx, cy, 200 filled
color rgb(200, 0, 0)
for i = 1 to 12
x = cx + 155 * cos(i * 2 * PI /12)
y = cy + 155 * sin(i * 2 * PI /12)
if i mod 3 = 0 then circle x, y, 12 filled else circle x, y, 8 filled
next
color 0, bkc
locate 4, 5 : ? ctime
locate 8, 1 : ? times
color rgb(0, 100, 128)
thickLine cx, cy, hx, hy, 4
circle hx, hy, 10 filled
thickLine cx, cy, mx, my, 4
if pF then delay 1000
pF = 0
'showpage '<<<< funny the hands don't stop at the right places with this
' 6:00:00 is the best bad example!!!
end if
next
pause

sub
thickLine(x1, y1, x2, y2, rThick)
'x1, y1 is one endpoint of line
'x2, y2 is the other endpoint of the line
'rThick is the radius of the tiny circles that will be drawn
' from one end point to the other to create the thick line
'Yes, the line will then extend beyond the endpoints with circular ends.

local length, stepx, stepy, dx, dy, i
stepx = x2 - x1 : stepy = y2 - y1
length = (stepx ^ 2 + stepy ^ 2) ^.5
if length then
dx = stepx / length : dy = stepy / length
for i = 0 to length
circle x1 + dx * i, y1 + dy * i, rThick filled
next
end if
end


' Clock hand math.bas SmallBASIC 0.12.8 [B+=MGA] 2017-03-26
' clock hands math.txt for JB [B+=MGA] 2017-03-26

'Facts and logic:
' in exactly 12 hours the minute hand forms a straight angle with hour hand 11 times
' that is once every 12/11 hours = 1_1/11 ~ 1.09090909... 1 hour
' 1/11 hours * 60 mins/hour = 60/11 = 5_5/11 mins +5 minutes
' 5/11 minutes * 60 secs/min = 300/11 secs = 27_3/11 +27 secs
' + more stuff

' at 6:00 the hands are exactly one straight edge,
' so every 1_1/11 hours from 6:00 the hands should be in alignment again!

' so the hours passed
'6
'7_1/11 and this matches time from clock code 07:05:27
'8_2/11 08:10:54
'9_3/11 then min and secs fraction exceeds 1 09:16:21
' ...
'
' so all we need do is convert these hours and fractions into digital times
? "More accurately, the times when the hour hand and the"
? "minute hand form a long straight is:
?
for
n = 0 to 11
hr = 6 + n + int(n/11)
if hr > 12 then hr = hr - 12
h = right("00" + str(hr), 2) + ":"
mn = int(60 * n/11) mod 60
m = right("00" + str(mn), 2) + ":"
sc = int((60 * n/11 - mn) * 60)
s = right("00" + str(sc), 2) + " R:"
ex = (60 * n/11 - mn) * 60 - int((60 * n/11 - mn) * 60)
e = str(ex)
print h + m + s + e
next
pause