|
-
Jun 4th, 2007, 06:24 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Displaying a Clock in a PictureBox Problem. Displayed Time Not Exactly Right
Hello everybody,
I have a picturebox in which I draw a circle. I then display the time in clock format by moving 3 line objects using the following code. LH is the Hour hand, LM is the minute hand and LS is the seconds hand.
In general the code works for most times however for certain times it doesn't look right. For example 10:35 will look more like 11:35.
Anybody have an idea how to fix this, is there more precise positioning of the clock hands? Thanks for any help.
Code:
LH.X1 = picClock.ScaleWidth / 2
LH.Y1 = picClock.ScaleHeight / 2
LM.X1 = picClock.ScaleWidth / 2
LM.Y1 = picClock.ScaleHeight / 2
LS.X1 = picClock.ScaleWidth / 2
LS.Y1 = picClock.ScaleHeight / 2
LH.X2 = 0.65 * (picClock.ScaleWidth / 2) * Cos(PI / 180 * (30 * intTotalHours - 90)) + LH.X1
LH.Y2 = 0.65 * (picClock.ScaleHeight / 2) * Sin(PI / 180 * (30 * intTotalHours - 90)) + LH.Y1
LM.X2 = 0.85 * (picClock.ScaleWidth / 2) * Cos(PI / 180 * (6 * intMinute - 90)) + LH.X1
LM.Y2 = 0.85 * (picClock.ScaleHeight / 2) * Sin(PI / 180 * (6 * intMinute - 90)) + LH.Y1
LS.X2 = 0.9 * (picClock.ScaleHeight / 2) * Cos(PI / 180 * (6 * intSecond - 90)) + LH.X1
LS.Y2 = 0.9 * (picClock.ScaleHeight / 2) * Sin(PI / 180 * (6 * intSecond - 90)) + LH.Y1
-
Jun 4th, 2007, 11:36 PM
#2
Re: Displaying a Clock in a PictureBox Problem. Displayed Time Not Exactly Right
While the second hand might tick forward each second, the hour and minute hands should move continuously. This means you need to calculate the angles for the hour an minute hands to at least the nearest second.
For the angle of the hour hand, calculate the number of seconds elapsed since midnight/noon and divide by 43200 (the number of seconds in a 12-hour period). This tells you what percentage of one complete rotation is complete. Multiply by 360 to get the angle in degrees, or multiply by 2*pi for radians, which you need for the trig functions.
For the angle of the minute hand, calculate the number of seconds elapsed since the beginning of the hour and divide by 3600. As above, multiply by 360 for angle in degrees, or by 2*pi for radians.
Code:
Private Sub Timer1_Timer()
Const pi = 3.1415926535898
Dim HAngle As Double
Dim MAngle As Double
Dim SAngle As Double
Dim H As Integer
Dim M As Integer
Dim S As Integer
Label1.Caption = Now
H = Hour(Now)
M = Minute(Now)
S = Second(Now)
LH.X1 = picClock.ScaleWidth / 2
LH.Y1 = picClock.ScaleHeight / 2
LM.X1 = picClock.ScaleWidth / 2
LM.Y1 = picClock.ScaleHeight / 2
LS.X1 = picClock.ScaleWidth / 2
LS.Y1 = picClock.ScaleHeight / 2
HAngle = pi * (3600 * H + 60 * M + S) / 21600 - pi / 2
LH.X2 = 0.65 * (picClock.ScaleWidth / 2) * Cos(HAngle) + LH.X1
LH.Y2 = 0.65 * (picClock.ScaleHeight / 2) * Sin(HAngle) + LH.Y1
MAngle = pi * (60 * M + S) / 1800 - pi / 2
LM.X2 = 0.85 * (picClock.ScaleWidth / 2) * Cos(MAngle) + LH.X1
LM.Y2 = 0.85 * (picClock.ScaleHeight / 2) * Sin(MAngle) + LH.Y1
SAngle = pi * S / 30 - pi / 2
LS.X2 = 0.9 * (picClock.ScaleHeight / 2) * Cos(SAngle) + LH.X1
LS.Y2 = 0.9 * (picClock.ScaleHeight / 2) * Sin(SAngle) + LH.Y1
End Sub
Last edited by Logophobic; Jun 4th, 2007 at 11:53 PM.
-
Jun 5th, 2007, 04:47 AM
#3
Thread Starter
Hyperactive Member
Re: Displaying a Clock in a PictureBox Problem. Displayed Time Not Exactly Right
Thank you for your code Logophobic. I tried it in the application, it seems to work correctly for times that are between the hours of 1 to 8 (both am and pm) however 9 to 12 it is incorrect.
-
Jun 5th, 2007, 07:52 AM
#4
Re: Displaying a Clock in a PictureBox Problem. Displayed Time Not Exactly Right
to fix an overflow problem change this line
vb Code:
HAngle = pi * (CLng(3600) * H + 60 * M + S) / 21600 - pi / 2
then seems to work right
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jun 5th, 2007, 05:59 PM
#5
Thread Starter
Hyperactive Member
Re: Displaying a Clock in a PictureBox Problem. Displayed Time Not Exactly Right
 Originally Posted by westconn1
to fix an overflow problem change this line
vb Code:
HAngle = pi * (CLng(3600) * H + 60 * M + S) / 21600 - pi / 2
then seems to work right
Thanks westconn1, that fixed the problem.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|