Results 1 to 5 of 5

Thread: moving a Line Control in circualr motion

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    Earth
    Posts
    277

    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

  2. #2
    DaoK
    Guest
    I think that will help you
    GO here

  3. #3
    New Member
    Join Date
    Oct 2000
    Posts
    14
    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.
    Rab.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    Earth
    Posts
    277
    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

  5. #5
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    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:
    1. Option Explicit
    2.  
    3. Private Const dblPi As Double = 3.14159265358979
    4. Private Const dblNoonOffset As Double = -1.5707963267949 '-pi/2, the angle of "noon" (needed to skew VBs reference frame)
    5. Private Const dblHSeg As Double = 0.523598775598298 '2pi/12, an arc of one hour, 1/12 of a circle
    6. Private Const dblMSSeg As Double = 0.10471975511966 '2pi/60, an arc of 1 min or sec, 1/60 of a circle
    7. Private Const dblLength As Double = 50 'length of the hands in pixels
    8.  
    9. Private Enum EClockHands
    10.    chSec = 0
    11.    chMin
    12.    chHour
    13. End Enum
    14.  
    15. Private Sub Form_Load()
    16.    Dim i As Double
    17.    Me.ScaleMode = vbPixels
    18.    Me.AutoRedraw = True
    19.    Me.DrawWidth = 2
    20.    'simple clock face drawing
    21.    Me.Circle (100, 100), 55, vbWhite
    22.    For i = 0 To (2 * dblPi) Step dblHSeg
    23.       Me.PSet (100 + (dblLength * Cos(i)), 100 + (dblLength * Sin(i))), vbBlack
    24.    Next i
    25.    Me.Refresh
    26.    For i = EClockHands.chSec To EClockHands.chHour
    27.       With linClock(i)
    28.          'all hands are the same length, but that's not necessary
    29.          'endpoint (x1,y1) is fixed, (x2,y2) rotates
    30.          .X1 = 100: .Y1 = 100
    31.          .X2 = .X1: .Y2 = .Y1 + dblLength
    32.       End With
    33.    Next i
    34.    linClock(EClockHands.chSec).BorderColor = vbRed
    35.    linClock(EClockHands.chMin).BorderColor = vbBlack
    36.    linClock(EClockHands.chHour).BorderColor = vbBlue
    37.    tmrClock.Interval = 1000
    38.    tmrClock.Enabled = True
    39.    UpdateHands
    40.    Me.Show
    41. End Sub
    42.  
    43. Private Sub tmrClock_Timer()
    44.    UpdateHands
    45. End Sub
    46.  
    47. 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)
    48.    Dim dblH As Double, dblM As Double, dblS As Double
    49.    If blnUseCurrentTime Then
    50.       intHour = Hour(Now)
    51.       intMin = Minute(Now)
    52.       intSec = Second(Now)
    53.    End If
    54.    'calculate the angles of the hands
    55.    dblS = dblNoonOffset + (intSec * dblMSSeg)
    56.    dblM = dblNoonOffset + (intMin * dblMSSeg)
    57.    '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
    58.    dblH = dblNoonOffset + (intHour * dblHSeg) + ((intMin / 12) * dblMSSeg)
    59.    With linClock(EClockHands.chHour)
    60.       .X2 = .X1 + (dblLength * Cos(dblH))
    61.       .Y2 = .Y1 + (dblLength * Sin(dblH))
    62.    End With
    63.    With linClock(EClockHands.chMin)
    64.       .X2 = .X1 + (dblLength * Cos(dblM))
    65.       .Y2 = .Y1 + (dblLength * Sin(dblM))
    66.    End With
    67.    With linClock(EClockHands.chSec)
    68.       .X2 = .X1 + (dblLength * Cos(dblS))
    69.       .Y2 = .Y1 + (dblLength * Sin(dblS))
    70.    End With
    71.    Me.Caption = Format$(Time$, "Long Time")
    72. 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
  •  



Click Here to Expand Forum to Full Width