|
-
Aug 19th, 2001, 10:19 AM
#1
Thread Starter
Hyperactive Member
moving a Line Control in circualr motion
how do i move a Line control ( Line1 ) in circualr motion so that it can move circulary ( in Clockwise direction )?
thanks
-
Aug 19th, 2001, 10:41 AM
#2
I think that will help you
GO here
-
Aug 19th, 2001, 02:48 PM
#3
New Member
I did not write this code but found it on the web when I was wanting to do something similar.
Hope it helps
add two lines a command button and a timer with the interval set to 100.
Option Explicit
Private Const D_THETA = 3.14159265 / 12
Private Theta As Single
Private R1 As Single
Private R2 As Single
Private Cx1 As Single
Private Cy1 As Single
Private Cx2 As Single
Private Cy2 As Single
Private Sub Command1_Click()
Timer1.Enabled = Not Timer1.Enabled
If Timer1.Enabled Then
Command1.Caption = "Stop"
Else
Command1.Caption = "Start"
End If
End Sub
Private Sub Form_Load()
Dim dx As Single
Dim dy As Single
dx = Line1.X2 - Line1.X1
dy = Line1.Y2 - Line1.Y1
R1 = Sqr(dx * dx + dy * dy)
Cx1 = Line1.X1
Cy1 = Line1.Y1
dx = Line2.X2 - Line2.X1
dy = Line2.Y2 - Line2.Y1
R2 = Sqr(dx * dx + dy * dy) / 2
Cx2 = (Line2.X1 + Line2.X2) / 2
Cy2 = (Line2.Y1 + Line2.Y2) / 2
End Sub
Private Sub Timer1_Timer()
Theta = Theta + D_THETA
Line1.X2 = Cx1 + Cos(Theta) * R1
Line1.Y2 = Cy1 + Sin(Theta) * R1
Line2.X1 = Cx2 + Cos(Theta) * R2
Line2.Y1 = Cy2 + Sin(Theta) * R2
Line2.X2 = Cx2 - Cos(Theta) * R2
Line2.Y2 = Cy2 - Sin(Theta) * R2
End Sub
Rab.
-
Aug 19th, 2001, 03:30 PM
#4
Thread Starter
Hyperactive Member
I worte this code
Add a timer ( Timer1 )
Code:
Const pi = 3.1415926
Private Sub Form_Load()
Timer1.Interval = 100
End Sub
Private Sub Timer1_Timer()
Form1.Cls
r = 2500
'seconds
sec = Second(Time)
Form1.PSet (4000, 3000), white
Form1.Line (4000, 3000)-(Sin((180 - sec * 6) * pi / 180) * r + 4000, _
Cos((180 - sec * 6) * pi / 180) * r + 3000)
'minutes
Min = Minute(Time)
Form1.Line (4000, 3000)-(Sin((180 - Min * 6) * pi / 180) * (r - 280) + 4000, _
Cos((180 - Min * 6) * pi / 180) * (r - 280) + 3000)
'hours
hour_ = Hour(Time)
Form1.Line (4000, 3000)-(Sin((180 - hour_ * 30) * pi / 180) * (r - 1000) + 4000, _
Cos((180 - hour_ * 30) * pi / 180) * (r - 1000) + 3000)
Form1.Circle (4000, 3000), 2550
Caption = Time
End Sub
does anyone have better code
-
Aug 20th, 2001, 03:17 AM
#5
Fanatic Member
Whether or not this is better is left up to the viewer, but I do think it's cleaner. After I dug around a little, I found the thing where I messed with this and stripped it down to the part in question. You will need to place 3 line controls on the form as part of an array named linClock and a timer control named tmrClock. The rest is handled by the code. I wrote the UpdateHands() procedure in such a way that you could override the current time and give it your own values for H/M/S.
VB Code:
Option Explicit
Private Const dblPi As Double = 3.14159265358979
Private Const dblNoonOffset As Double = -1.5707963267949 '-pi/2, the angle of "noon" (needed to skew VBs reference frame)
Private Const dblHSeg As Double = 0.523598775598298 '2pi/12, an arc of one hour, 1/12 of a circle
Private Const dblMSSeg As Double = 0.10471975511966 '2pi/60, an arc of 1 min or sec, 1/60 of a circle
Private Const dblLength As Double = 50 'length of the hands in pixels
Private Enum EClockHands
chSec = 0
chMin
chHour
End Enum
Private Sub Form_Load()
Dim i As Double
Me.ScaleMode = vbPixels
Me.AutoRedraw = True
Me.DrawWidth = 2
'simple clock face drawing
Me.Circle (100, 100), 55, vbWhite
For i = 0 To (2 * dblPi) Step dblHSeg
Me.PSet (100 + (dblLength * Cos(i)), 100 + (dblLength * Sin(i))), vbBlack
Next i
Me.Refresh
For i = EClockHands.chSec To EClockHands.chHour
With linClock(i)
'all hands are the same length, but that's not necessary
'endpoint (x1,y1) is fixed, (x2,y2) rotates
.X1 = 100: .Y1 = 100
.X2 = .X1: .Y2 = .Y1 + dblLength
End With
Next i
linClock(EClockHands.chSec).BorderColor = vbRed
linClock(EClockHands.chMin).BorderColor = vbBlack
linClock(EClockHands.chHour).BorderColor = vbBlue
tmrClock.Interval = 1000
tmrClock.Enabled = True
UpdateHands
Me.Show
End Sub
Private Sub tmrClock_Timer()
UpdateHands
End Sub
Private Sub UpdateHands(Optional ByVal blnUseCurrentTime As Boolean = True, Optional ByVal intHour As Integer = 0, Optional ByVal intMin As Integer = 0, Optional ByVal intSec As Integer = 0)
Dim dblH As Double, dblM As Double, dblS As Double
If blnUseCurrentTime Then
intHour = Hour(Now)
intMin = Minute(Now)
intSec = Second(Now)
End If
'calculate the angles of the hands
dblS = dblNoonOffset + (intSec * dblMSSeg)
dblM = dblNoonOffset + (intMin * dblMSSeg)
'the last term of the next calculation fudges the hour hand moving through the hour instead of going from hour to hour in a single jump
dblH = dblNoonOffset + (intHour * dblHSeg) + ((intMin / 12) * dblMSSeg)
With linClock(EClockHands.chHour)
.X2 = .X1 + (dblLength * Cos(dblH))
.Y2 = .Y1 + (dblLength * Sin(dblH))
End With
With linClock(EClockHands.chMin)
.X2 = .X1 + (dblLength * Cos(dblM))
.Y2 = .Y1 + (dblLength * Sin(dblM))
End With
With linClock(EClockHands.chSec)
.X2 = .X1 + (dblLength * Cos(dblS))
.Y2 = .Y1 + (dblLength * Sin(dblS))
End With
Me.Caption = Format$(Time$, "Long Time")
End Sub
Last edited by Kaverin; Sep 5th, 2001 at 03:14 AM.
I'm baaaack...
VB5 Professional Edition, VC++ 6
Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se
I feel special because I finally figured out how to loop midis: Post link
I'm a fanatic too 
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
|