dcsimg
Results 1 to 6 of 6

Thread: [RESOLVED] Move point around circle path

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2009
    Location
    Belarus
    Posts
    104

    Resolved [RESOLVED] Move point around circle path

    Hi!

    Say please, how possible moving shape to specific degree of another shape?
    I know only radius of big circle path, and degree which i'm needed to move the shape.
    Name:  degree3.jpg
Views: 154
Size:  10.7 KB

    My code is:
    Code:
        'get radius of path shape
        bigCircleRadius = ActiveSheet.Shapes("path").Height / 2
        
        For i = 1 To 360 Step 15
            'get radians by degree
            rad = i * (3.14159265358979 / 180)
                
            'move point to path direction for each 15 degrees
            ActiveSheet.Shapes("point").Left = ActiveSheet.Shapes("path").Left + bigCircleRadius * Cos(rad) + ActiveSheet.Shapes("point").Width / 2
            ActiveSheet.Shapes("point").Top = ActiveSheet.Shapes("path").Top + bigCircleRadius * Sin(rad) - ActiveSheet.Shapes("point").Height / 2
        Next
    but my point move stepper and not properly.
    My sample in attach. I want move point by big circle path. Who can help please?
    pathforpoint.zip
    Ten Years After - 01 You Give Me Loving

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,620

    Re: Move point around circle path

    You need to offset your computed distance from the center of the shape.
    It is easiest to compute the center of the shape at the beginning, and use it.
    So, I computer the center (CenterX, CenterY), and use the computed end point of the radius at the specified angle you provided and subtract -4.5 in X and Y to offset to the point's center, approximately.
    Code:
    Sub DrawToPath()
        
        Dim i As Integer
        Dim rad As Double
        Dim bigCircleRadius As Double
        Dim CenterX As Single, CenterY As Single
        
        'set shapes to base coordinates
        ActiveSheet.Shapes("path").Left = 36.75
        ActiveSheet.Shapes("path").Top = 59.25
    
        ActiveSheet.Shapes("point").Left = 139.1243
        ActiveSheet.Shapes("point").Top = 54.10535
    
        'get radius of path shape
        bigCircleRadius = ActiveSheet.Shapes("path").Height / 2
        CenterX = ActiveSheet.Shapes("path").Left + bigCircleRadius
        CenterY = ActiveSheet.Shapes("path").Top + bigCircleRadius
        
        For i = 0 To 360 Step 15
            'get radians by degree
            rad = i * (3.14159265358979 / 180)
                
            'mone point to path direction for each 15 degrees
            ActiveSheet.Shapes("point").Left = -4.5 + CenterX + bigCircleRadius * Cos(rad)
            ActiveSheet.Shapes("point").Top = -4.5 + CenterY + bigCircleRadius * Sin(rad)
        Next
    
    End Sub

  3. #3
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,620

    Re: Move point around circle path

    I put a short delay in so you can see the point animate around the circle.
    Originally using Application.Wait, but that has a minimum resolution of 1 second, and hangs the application while waiting.
    Then, just used a loop using Timer, which probably has a resolution of 64 times per second so a delay of 2/64 should give you a pretty steady 32hz animation.
    Code:
        For i = 0 To 360 Step 15
            'get radians by degree
            rad = i * (3.14159265358979 / 180)
                
            'mone point to path direction for each 15 degrees
            ActiveSheet.Shapes("point").Left = -4.5 + CenterX + bigCircleRadius * Cos(rad)
            ActiveSheet.Shapes("point").Top = -4.5 + CenterY + bigCircleRadius * Sin(rad)
     
         '  Application.Wait (Now + TimeValue("0:00:01"))
            Dim endTime As Single
            endTime = Timer + (2 / 64)
            Do While Timer < endTime: DoEvents: Loop
        Next
    p.s. Just changed the code to compute the point offset, depending on its size, rather than use a hardcoded -4.5
    Code:
    Sub DrawToPath()
        
        Dim i As Integer
        Dim rad As Double
        Dim bigCircleRadius As Double
        Dim CenterX As Single, CenterY As Single
        Dim pointOffset As Single
        
        'set shapes to base coordinates
        ActiveSheet.Shapes("path").Left = 36.75
        ActiveSheet.Shapes("path").Top = 59.25
    
        ActiveSheet.Shapes("point").Left = 139.1243
        ActiveSheet.Shapes("point").Top = 54.10535
    
        'get radius of path shape
        bigCircleRadius = ActiveSheet.Shapes("path").Height / 2
        
        CenterX = ActiveSheet.Shapes("path").Left + bigCircleRadius
        CenterY = ActiveSheet.Shapes("path").Top + bigCircleRadius
        pointOffset = -(ActiveSheet.Shapes("point").Width / 2)
        
        For i = 0 To 360 Step 15
            'get radians by degree
            rad = i * (3.14159265358979 / 180)
                
            'mone point to path direction for each 15 degrees
            ActiveSheet.Shapes("point").Left = pointOffset + CenterX + bigCircleRadius * Cos(rad)
            ActiveSheet.Shapes("point").Top = pointOffset + CenterY + bigCircleRadius * Sin(rad)
            Dim endTime As Single
            endTime = Timer + (2 / 64)
            Do While Timer < endTime: DoEvents: Loop
        Next
    
    End Sub
    Last edited by passel; Oct 3rd, 2018 at 02:58 PM.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Apr 2009
    Location
    Belarus
    Posts
    104

    Re: Move point around circle path

    Oh, cool! Mega merci! Thats what i'm wanted.
    Ten Years After - 01 You Give Me Loving

  5. #5
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,620

    Re: Move point around circle path

    By default, the angles and rotation are based on the common mathematical sense, i.e. 0 degrees is to the right along the X axis, and positive angles rotate counter-clockwise around the center. But since the Y axis is Positive going down in the window, the rotation ends up being clockwise, even though 90 is still along the X-axis.

    If you prefer 0 degrees to be toward the top of the Y axis, and rotating clockwise, like a compass, you have two choices (there are more than two choices, but these are the two I generally choose from).
    Keep the calculations as they are, but subtract 90 degrees from the input, or
    you swap the sin and cos functions, negating the cos, as shown below.
    Code:
            ActiveSheet.Shapes("point").Left = pointOffset + CenterX + bigCircleRadius * Sin(rad)
            ActiveSheet.Shapes("point").Top = pointOffset + CenterY + bigCircleRadius * -Cos(rad)

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Apr 2009
    Location
    Belarus
    Posts
    104

    Re: [RESOLVED] Move point around circle path

    Nice really, yeeery good additional!

    Also, i'm experimented with rotating pivot coordinates of point.
    equation of transfer of a point on an angleName:  formula.jpg
Views: 18
Size:  3.9 KB
    but also stepped drawing instead linear.
    Ten Years After - 01 You Give Me Loving

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width