Results 1 to 10 of 10

Thread: [RESOLVED] Making a Shape move along a Line

Hybrid View

  1. #1
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Making a Shape move along a Line

    Try this formula. Ensure you pass the distance to move in appropriate scale units of the Line's container. For simplicity, suggest just changing the line's container (form, picbox, whatever) to a ScaleMode of vbPixels. The DistanceToMove parameter is number of units from the line's X1,Y1 point. Obviously you will want to center the shape over the returned NewX,NewY coordinates.

    Code:
    Private Sub GetNextPoint(Path As VB.Line, DistanceToMove As Long, NewX As Single, NewY As Single)
    
    ' DistanceToMove is number of ScaleUnits (twips,pixels,etc) to move from Path's X1,Y1 properties
    ' NewX & NewY will be the coordinates relative to Path's X1,Y1 & DistanceToMove
    
    Dim dx As Single, dy As Single
    Dim sLen As Single
    
    dx = Path.X2 - Path.X1
    dy = Path.Y2 - Path.Y1
    sLen = Sqr(dx * dx + dy * dy)
    
    NewY = (dy * DistanceToMove) / sLen + Path.Y1
    NewX = (dx * DistanceToMove) / sLen + Path.X1
    
    End Sub
    Edited: I didn't add any logic to test whether your distance exceeds the line length. You can test that yourself. Just calc the length as shown in the subroutine & ensure you don't pass a distance greater than that. Or, within the subroutine, if DistanceToMove > sLen then return Path.X2,Path.Y2 as the NewX,NewY values

    As-is, the line is used just for a starting point, direction & angle, there is no stopping point in the logic. You can traverse that line in either direction at an infinite +/- DistanceToMove

    After re-reading your post, shape can start at either end? This changes the logic within the subroutine. Modified below:
    Code:
    Private Sub GetNextPoint(Path As VB.Line, ByVal DistanceToMove As Long, StartAtX1Y1 As Boolean, _
                                 NewX As Single, NewY As Single)
    
    ' DistanceToMove is number of ScaleUnits (twips,pixels,etc) to move from Path's X1,Y1 properties
    ' NewX & NewY will be the coordinates relative to Path's X1,Y1 & DistanceToMove
    ' StartAtX1Y1 is true if starting point is X1,Y1 else False if starting from X2,Y2
    
    Dim dx As Single, dy As Single
    Dim sLen As Single
    
        dx = Path.X2 - Path.X1
        dy = Path.Y2 - Path.Y1
        sLen = Sqr(dx * dx + dy * dy)
        If StartAtX1Y1 = False Then DistanceToMove = sLen - DistanceToMove
        NewY = (dy * DistanceToMove) / sLen + Path.Y1
        NewX = (dx * DistanceToMove) / sLen + Path.X1
    
    End Sub
    Last edited by LaVolpe; Nov 16th, 2014 at 01:38 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

Tags for this Thread

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