Results 1 to 3 of 3

Thread: moving picture in a perfect circle.

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    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.

  2. #2
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    HAve a go

    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.

    Code:
    ' 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
    Paul Lewis

  3. #3
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    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:
    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()!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width