VB icon

I Claim The Easiest Anolog Clock Method (Gulp)

Email
Submitted on: 2/12/2015 11:23:00 PM
By: Steven Atkiss (from psc cd)  
Level: Beginner
User Rating: By 5 Users
Compatibility: VB 3.0, VB 4.0 (16-bit), VB 4.0 (32-bit), VB 5.0, VB 6.0
Views: 705
 
     Heres One For The Beginners. There Is An Extremely Easy Method To Clocks Using Sin\Cos, I've Seen a Few On PSC That Do The Job But Are A Litle Over Complex. At A Glance It Looks Long, Just Remove All The Comments And You'll See How Short The Code Actually Is. I hope This Helps

 

Windows API/Global Declarations:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
'Windows API/Global Declarations for :I Claim The Easiest Anolog Clock Method (Gulp)
'**************************************
None... It's Just maths
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: I Claim The Easiest Anolog Clock Method (Gulp)
' Description:Heres One For The Beginners. There Is An Extremely Easy Method To Clocks Using Sin\Cos, I've Seen a Few On PSC That Do The Job But Are A Litle Over Complex. At A Glance It Looks Long, Just Remove All The Comments And You'll See How Short The Code Actually Is. I hope This Helps
' By: Steven Atkiss (from psc cd)
'
' Inputs:You Just Need To Insert A Timer1
'**************************************

Private Sub Form_Load()
Me.AutoRedraw = False
Me.ScaleMode = 3
Timer1.Interval = 500
End Sub
Private Sub Timer1_Timer()
DrawHands Hour(Now), Minute(Now), Second(Now)
End Sub
Private Sub DrawHands(HourVal As Single, MinuteVal As Single, SecondVal As Single)
Dim AMPM As String
Dim Pi As Single
Dim i As Single
Dim X As Single
Dim Y As Single
Dim SecondAngle As Long
Dim MinuteAngle As Long
Dim HourAngle As Long
Dim ClockDiamiter As Single
Dim ClockPos As Single
'Adjust Time For 12Hour Clock If Its 24Hour
If Val(HourVal) > 12 Then
HourVal = (HourVal - 12)
AMPM = "PM"
ElseIf HourVal = 0 Then
HourVal = 12
AMPM = "AM"
Else
AMPM = "AM"
End If
ClockDiamiter = 90
ClockPos = ClockDiamiter + 10
'Getting The Sin\Cos Of Your Clock
'===========================
'The Main Works, Or "Cogs" Are All To do With This Bit,
'You Can Leave Th Pi At The Start Of Your Program And
'Never Call It Again. As You Can See, The Circles Points
'Are Drawn In Memory To The Diamiter And Step You Set.
'Then When Your Drawing Your Points You Specify Where You
'Want The Center Point Of The Clock To Be Drawn. Easy Eh!
'Your Step Is Worked Out As Follows 360 \ How Many Points,
'In This Case Theres 60Sec And 12Hrs In A Full Circle, So
'Minutes And Seconds= 360 \ 60 = Step Every 6 Degs
'Hourse = 360 \ 12 = Step Every 30 Degs
'Convert Degrees To Radians
Pi = 3.14159265 / 180
Cls
'Draw The Clock Seconds Points
For i = 0 To 360 Step 6
X = ClockDiamiter * Cos(i * Pi)
Y = ClockDiamiter * Sin(i * Pi)
Me.PSet (ClockPos + X, ClockPos + Y)
Next i
'============================
'Draw The Clocks Hours Points
For i = 0 To 360 Step 30
X = ClockDiamiter * Cos(i * Pi)
Y = ClockDiamiter * Sin(i * Pi)
Line (ClockPos + X - 1, ClockPos + Y - 1)-(ClockPos + X + 1, ClockPos + Y + 1), , BF
Next i
'Determine Hand Angles
'===========================
'(-15 and -3)This Part Only Shifts The Position Of 12Oclock
'When Using Sin And CosIn This Way, 0 Degrees Isn't At
'The Top Of The Circle, It's At The "3 Oclock Position.
'All We Have To Do Is Set The Display Back 1\4 To Get
'12 Oclock Where 12 Oclock Should Be.
SecondAngle = (360 / 60) * (SecondVal - 15)
MinuteAngle = (360 / 60) * (MinuteVal - 15)
HourAngle = (360 / 12) * (HourVal - 3)
'===========================
 
'Draw The Hour Hand
X = (ClockDiamiter / 1.4) * Cos(HourAngle * Pi)
Y = (ClockDiamiter / 1.4) * Sin(HourAngle * Pi)
Line (ClockPos, ClockPos)-(ClockPos + X, ClockPos + Y), vbBlack
'Draw The Minute Hands
X = ClockDiamiter * Cos(MinuteAngle * Pi)
Y = ClockDiamiter * Sin(MinuteAngle * Pi)
Line (ClockPos, ClockPos)-(ClockPos + X, ClockPos + Y), vbBlue
'Draw The Seconds Hand
X = ClockDiamiter * Cos(SecondAngle * Pi)
Y = ClockDiamiter * Sin(SecondAngle * Pi)
Line (ClockPos, ClockPos)-(ClockPos + X, ClockPos + Y), vbRed
'Digital Time Display
Me.Caption = HourVal & ":" & MinuteVal & ":" & SecondVal & " " & AMPM
End Sub


Other 11 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

9/28/2015 7:53:33 PMdave Carter

Nice, thanks :)
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.