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.
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 valuesCode: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
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




Reply With Quote
