Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > Interface and Graphics > Drawing a clock in vb6


Reply
 
Thread Tools Display Modes
  #1  
Old 02-18-2009, 06:48 AM
picogsm's Avatar
picogsm picogsm is offline
Freshman
 
Join Date: Mar 2005
Location: Uk
Posts: 43
Default 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?
Reply With Quote
  #2  
Old 02-18-2009, 09:52 AM
OnErr0r's Avatar
OnErr0r OnErr0r is offline
Obsessive OPtimizer

Administrator
* Guru *
 
Join Date: Jun 2002
Location: Debug Window
Posts: 13,685
Default

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.
Reply With Quote
  #3  
Old 02-19-2009, 04:54 AM
picogsm's Avatar
picogsm picogsm is offline
Freshman
 
Join Date: Mar 2005
Location: Uk
Posts: 43
Default

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
Reply With Quote
  #4  
Old 02-19-2009, 07:15 AM
Iceplug's Avatar
Iceplug Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: California, USA
Posts: 16,583
Default

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.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #5  
Old 02-19-2009, 07:31 AM
picogsm's Avatar
picogsm picogsm is offline
Freshman
 
Join Date: Mar 2005
Location: Uk
Posts: 43
Default

Guys, between the two of you it works.

many thanks for taking the time to help
Reply With Quote
  #6  
Old 02-19-2009, 09:16 AM
passel's Avatar
passel passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,714
Default

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.
Reply With Quote
  #7  
Old 02-19-2009, 05:24 PM
OnErr0r's Avatar
OnErr0r OnErr0r is offline
Obsessive OPtimizer

Administrator
* Guru *
 
Join Date: Jun 2002
Location: Debug Window
Posts: 13,685
Default

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.
Reply With Quote
  #8  
Old 02-20-2009, 01:45 AM
picogsm's Avatar
picogsm picogsm is offline
Freshman
 
Join Date: Mar 2005
Location: Uk
Posts: 43
Default

Quote:
Originally Posted by passel View Post
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
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->