Tuesday, April 24, 2012

Small Basic Analog Clock


I wrote a Small Basic Analog Clock. Since I really want to have background pictures, I use Shapes. The rotation is a mess. Lots of trial and error. I also put in time offset, although you have to muddle with the source code to do it. Maybe an update will allow you to do it without modifying the source code. The sample picture has random picture from Flickr. I do wish I know who took it, so it can be properly credited.

'Analog Clock - MHR140
'A Shape example
'by Harry Hardjono
' April 2012
'
'Init
GraphicsWindow.Width=400
GraphicsWindow.Height=400
GraphicsWindow.Show()
GraphicsWindow.Clear()
screen_x=GraphicsWindow.Width
screen_y=GraphicsWindow.Height

'Commented out because it hangs on my SilverLight
'BPic=Flickr.GetRandomPicture("clock")
'BImg=ImageList.LoadImage(BPic)
'BGP=Shapes.AddImage(BImg)


For i=1 to 12
Digits[i]=Shapes.AddRectangle(10,40)
mx1=1
mx2=i
mx3=12
my1=30
my3=360
map()
DAngle=my2
Dx=(screen_x/2)+150*Math.Sin(Math.GetRadians(DAngle))
Dy=(screen_y/2)-150*Math.Cos(Math.GetRadians(DAngle))
Shapes.Rotate(Digits[i],DAngle)
Shapes.Move(Digits[i],Dx,Dy)
Program.Delay(100)
endfor

GraphicsWindow.BrushColor="White"
Hand[1]=Shapes.AddRectangle(20,90) 'Hour hand
Hand[2]=Shapes.AddRectangle(12,150) 'Minute hand
Hand[3]=Shapes.AddRectangle(2,180) 'Second hand

GraphicsWindow.FontSize=15
GraphicsWindow.BrushColor="Black"
TT=Shapes.AddText(Clock.Time)
Shapes.Move(TT,150,5)

OffsetHour=0
OffsetMin=0
OffsetSec=0

Loop:
OnTick()
Program.Delay(1000)
Goto Loop

Sub OnTick
Shapes.SetText(TT,Clock.Time)
THour=Clock.Hour
mx1=0
mx2=Math.Remainder(THour+OffsetHour,12)
mx3=12
my1=0
my3=360
map()
DAngle=my2
Dx=((screen_x/2)+10)+40*Math.Sin(Math.GetRadians(DAngle))
Dy=((screen_y/2)-22.5)-40*Math.Cos(Math.GetRadians(DAngle))
Shapes.Rotate(Hand[1],DAngle)
Shapes.Move(Hand[1],Dx-15,Dy)

TMin=Clock.Minute
mx1=0
mx2=Math.Remainder(TMin+OffsetMin,60)
mx3=60
my1=0
my3=360
map()
DAngle=my2
Dx=((screen_x/2)+6)+60*Math.Sin(Math.GetRadians(DAngle))
Dy=((screen_y/2)-75)-60*Math.Cos(Math.GetRadians(DAngle))
Shapes.Rotate(Hand[2],DAngle)
Shapes.Move(Hand[2],Dx-8,Dy+20)

TSec=Clock.Second
mx1=0
mx2=Math.Remainder(TSec+OffsetSec,60)
mx3=60
my1=0
my3=360
map()
DAngle=my2
Dx=((screen_x/2)+1)+70*Math.Sin(Math.GetRadians(DAngle))
Dy=((screen_y/2)-90)-70*Math.Cos(Math.GetRadians(DAngle))
Shapes.Rotate(Hand[3],DAngle)
Shapes.Move(Hand[3],Dx+3,Dy+20)
Endsub


Sub map 'map function
'x1-x2-x3 y1-y2-y3
my2=((my3-my1)*(mx2-mx1)/(mx3-mx1))+my1
EndSub


No comments: