 |

02-18-2009, 06:48 AM
|
 |
Freshman
|
|
Join Date: Mar 2005
Location: Uk
Posts: 43
|
|
Drawing a clock in vb6
|
hi,
Whats wrong with this code?
I want to draw 360 degrees of lines radiating from the centre of the picture box, but it does not work.
All I see is a small segment (maybe 2 degree worth) in the upper left quadrant, no matter what I do...
Code:
Dim x1 As Integer
Dim y1 As Integer
Dim x2 As Double
Dim y2 As Double
Dim i As Double
Dim j As Double
x1 = Picture1.ScaleWidth / 2
y1 = Picture1.ScaleHeight / 2
For i = 0 To 360 Step 20
j = (i / 180) * 3.14159
x2 = Cos(j) * 2
y2 = Sin(j) * 2
Picture1.Line (x1, y1)-(x2, y2), vbRed
Next i
Any ideas?
|
|

02-18-2009, 09:52 AM
|
 |
Obsessive OPtimizer
Administrator * Guru *
|
|
Join Date: Jun 2002
Location: Debug Window
Posts: 13,685
|
|
I'll let you figure out what is wrong with yours.
Here is my working code:
Code:
Option Explicit
Const RAD_SECOND As Double = 0.10471975511966 ' PI / 60
' interval = 10
Private Sub Timer1_Timer()
DrawSecondHand Second(Now)
End Sub
Private Sub DrawSecondHand(ByVal lSecond As Long)
Dim x As Long
Dim y As Long
Dim lCenterX As Long
Dim lCenterY As Long
Dim lRadius As Long
Static lLastSecond As Long
' Flicker protection
If lSecond <> lLastSecond Then
Caption = lSecond
Cls
' Determine centers
lCenterX = ScaleWidth \ 2
lCenterY = ScaleHeight \ 2
lRadius = lCenterX
' Use minimum
If lCenterY < lCenterX Then lRadius = lCenterY
Circle (lCenterX, lCenterY), lRadius
x = Sin(lSecond * RAD_SECOND) * lRadius
y = Cos(lSecond * RAD_SECOND) * lRadius
Line (lCenterX, lCenterY)-(lCenterX + x, lCenterY - y)
lLastSecond = lSecond
End If
End Sub
|
__________________
Quis custodiet ipsos custodues.
|

02-19-2009, 04:54 AM
|
 |
Freshman
|
|
Join Date: Mar 2005
Location: Uk
Posts: 43
|
|
Hi, thanks for the hints!
Problem is, I do not really want to create a clock (its just to help explain).
I would like to draw a line at any of the 360 degrees in a circle.
Had a play more with the code:
Code:
Dim x1 As Long
Dim y1 As Long
Dim x2 As Long
Dim y2 As Long
Dim ra As Long
Dim i As Long
Dim j As Long
x1 = Picture1.ScaleWidth / 2
y1 = Picture1.ScaleHeight / 2
If x1 > y1 Then ra = y1 Else ra = x1
Picture1.Circle (x1, y1), ra
For i = 0 To 360
j = (i * 3.14159) / 180 'My original calc was wrong!!!
x2 = Cos(j) * ra
y2 = Sin(j) * ra
Picture1.Line (x1, y1)-(x1 + x2, y1 - y2), vbRed
Next i
Now what this does is draw a circle which is segmented at around 60 degrees, no matter what step value i put into the for..next loop.
Its really confusing 
|
|

02-19-2009, 07:15 AM
|
 |
MetaCenturion
Retired Moderator * Guru *
|
|
Join Date: Aug 2001
Location: California, USA
Posts: 16,583
|
|
|
Well, if you are using Longs, you aren't going to get any detail between numbers such as 59 and 60. However, if you use a Single/Double/Currency, you can use Step 0.5 or lower and get a more detailed circle.
|
|

02-19-2009, 07:31 AM
|
 |
Freshman
|
|
Join Date: Mar 2005
Location: Uk
Posts: 43
|
|
Guys, between the two of you it works.
many thanks for taking the time to help 
|
|

02-19-2009, 09:16 AM
|
 |
Sinecure Expert
Super Moderator * Guru *
|
|
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,714
|
|
|
Hopefully, you realize that it wasn't a problem with the step, but the fact that you were using a Long Type (all Integer, no fractional part) to hold your converted Angle in Radians, which is why it jumped by about 60 degrees.
1 Radian = 57.295779... Degrees
2 Radians = 114.591559... Degrees
3 Radians = 171.887338.... Degrees
etc...
|
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
|

02-19-2009, 05:24 PM
|
 |
Obsessive OPtimizer
Administrator * Guru *
|
|
Join Date: Jun 2002
Location: Debug Window
Posts: 13,685
|
|
Also note that my code only used floating point operations (Sin/Cos) as intermediates. After being multiplied with the radius the lack of a decimal really isn't that important, since pixel operations are integers (even though the line method takes singles).
In the same way you could have used all longs, omitted j and simply done this:
Code:
x2 = Cos((i * 3.14159) / 180) * ra
y2 = Sin((i * 3.14159) / 180) * ra
|
__________________
Quis custodiet ipsos custodues.
|

02-20-2009, 01:45 AM
|
 |
Freshman
|
|
Join Date: Mar 2005
Location: Uk
Posts: 43
|
|
Quote:
Originally Posted by passel
Hopefully, you realize that it wasn't a problem with the step, but the fact that you were using a Long Type (all Integer, no fractional part) to hold your converted Angle in Radians, which is why it jumped by about 60 degrees.
1 Radian = 57.295779... Degrees
2 Radians = 114.591559... Degrees
3 Radians = 171.887338.... Degrees
etc...
|
Yes, I see it now. I also used a Print statment to debug the coordinates being created and saw them all with no decimal places!
Really wish I had taken some formal programming course when younger, don't know what I would do without websites like this
Thanks again 
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|