Click to See Complete Forum and Search --> : moving picture in a perfect circle.
SteveCRM
Sep 18th, 2000, 06:50 PM
This doesn't help much, but I remember that Fox posted some code to move a label in an elipse...I'll do a search and see if anything pops up.
PaulLewis
Sep 18th, 2000, 11:30 PM
This also might not help much but here it is anyhow...
It runs on a timer control so your other code can still run while the control rotates in a circle. Change the settings for radius, speed (milliseconds) and step to see if it will do what you want.
' add a timer, command button and picturebox to a form.
Option Explicit
Dim mControl As Control
Dim mRadius As Integer
Dim xStart As Long
Dim yStart As Long
Dim mInc As Single
Dim pi As Single
Private Sub Command1_Click()
MoveObjectInCircle Picture1, 50, 10, 1
End Sub
Sub MoveObjectInCircle(ctl As Control, radius As Integer, speed As Integer, inc As Single)
' use degrees not radians for the inc parameter because
' most people think of a circle as 360 degrees rather than
' 2 * pi radians
Set mControl = ctl
mRadius = radius
mInc = inc
' get the centre of the control
xStart = ctl.Left
yStart = ctl.Top
Timer1.Interval = speed
Timer1.Enabled = True
End Sub
Private Sub Form_Load()
pi = Atn(1) * 4
End Sub
Private Sub Timer1_Timer()
' each time the timer event happens, we move the object a little bit
MoveObject
End Sub
Private Sub MoveObject()
' assumed the form is in pixels but it doesn't matter really
' spped represents how quickly the object is moved
Dim x, y As Single
Static lastX, lastY As Single
Static angle As Single
'initialise the lastX
If lastX = x Then
lastX = 0
Else
lastX = x
lastY = y
End If
' get the new angle to rotate (in radians)
angle = angle + mInc / 180# * pi
x = xStart - mRadius * Cos(angle) + mRadius
y = yStart - mRadius * Sin(angle)
mControl.Left = x
mControl.Top = y
End Sub
Cheers
Fox
Sep 19th, 2000, 12:05 AM
This may help a bit more ;)
The idea is to use sin() and cos() as Paul already showed:
Put a Picture box into a new project and add this code:
Sub Form_Load()
'A (the angle) is 0 - 2Pi instead of 360°!
Dim A as Single
Dim Radius as Long
'Position of center
Dim X As Long
Dim Y As Long
'Initialize
X = Me.ScaleWidth / 2
Y = Me.ScaleHeight / 2
Radius = Me.ScaleHeight / 4
Me.Show
'The main loop
While 1
Picture1.Move Sin(A) * Radius + X, Cos(A) * Radius + Y
A = A + 0.01
DoEvents
Wend
End Sub
Hope this helps... And don't forget to put "End" into Form_Unload()! :)
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.