Results 1 to 7 of 7

Thread: [RESOLVED] how can i walk throw the line?

  1. #1

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,515

    Resolved [RESOLVED] how can i walk throw the line?

    see these image:
    https://2.bp.blogspot.com/-STOKeDBaa...0/distanc1.png

    i know calculate the AB distance using the Pythagorean theorem.
    my question is: imagine that i'm on vector A, how can i move, vector a vector, to B?
    is just:
    X = X + 1
    Y = Y + 1
    ?
    and, true, depends on direction.. what i don't know use it..
    can anyone explain these to me?
    and yes these is a 2D, i need to know, too, in 3D.
    VB6 2D Sprite control

    To live is difficult, but we do it.

  2. #2
    Hyperactive Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    488

    Re: how can i walk throw the line?

    vector by vector AB= AO + OB :

    AO |X=xb-xa + OB|X=0.........= AB |X =xb-xa
    .....|Y=0.............|Y=yb-ya..........|Y =yb-ya

    the coordinates of a vector are the coordinates of the last point minus the first point

    Euclidian vector
    Last edited by Delaney; Jun 20th, 2020 at 02:40 PM.

  3. #3
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,251

    Re: how can i walk throw the line?

    You wouldn't add 1 to each, unless you want to move at a 45 degree angle.
    You need the ratio of the change in values. Traditionally, I think, the ratio is given as dY/dX.

    Depending on what is desired, I don't calculate the ratio, but just calculate the delta, and then scale each delta based on a step size, or a percentage.

    In a card game I wrote, for instance, I want cards to return "home" when they are free to do so, and I want to be able to move several cards at the same time so you can have multiple cards moving across the form on a line from where they were to the home positions.

    Since the cards should "stack" in the home position in order, from Ace to King, I don't want a lower card to arrive after a higher card. If I simply moved the cards at the same speed, the closer cards would arrive earlier than cards further away, regardless of what value the card was.

    So, I move the cards the same number of steps to reach home. If the card is further away, its steps will be larger. So, when I start moving the cards in order, from low to high, the lower card will arrive before the higher card regardless of which card is closer to home at the start.

    Using your diagram,
    Code:
    dx = Xb - Xa
    dy = Yb - Ya
    
    To move from point A to point B in 50 steps.
    stepX = dx / 50
    stepY = dy / 50
    
    PosX = Xa
    PosY = Ya
    
    For I = 1 to 49
      PosX = PosX + stepX
      PosY = PosY + stepY
      draw new position indication
    Next
    PosX = Xb  'just to ensure we end up exactly on the point, no rounding issues
    PosY = Yb
    draw at final position.
    Or you could use a percentage position to place at a specific position.
    'calculate a point 75% of the way from A to B
    PosX = Xa + .75 * dx
    PosY = Ya + .75 * dy

    There are other alternatives, but the above should be fairly understandable, I hope.

    For 3D, just add the same code for the Z axis values, i.e. calculate dz and scale it by the same amount as you scale x and Y to get a new 3d point along a straight line between the two points.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  4. #4

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,515

    Re: how can i walk throw the line?

    isn't 100% but works fine:
    Code:
    'if the vector is the destiny position, change the parameter order for get the right result... i had tested
    Private Function GetInCamVector(Origin As Position3D, Destiny As Position3D) As Position3D
        Dim Steps As Double
        Steps = Math.Sqr(Math.Abs(Destiny.x - Origin.x) ^ 2 + Math.Abs(Destiny.Y - Origin.Y) ^ 2 + Math.Abs(Destiny.Z - Origin.Z) ^ 2)
        Steps = Ceiling(Steps)
        
        Dim increment As Position3D
        increment.x = (Destiny.x - Origin.x) / Steps
        increment.Y = (Destiny.Y - Origin.Y) / Steps
        increment.Z = (Destiny.Z - Origin.Z) / Steps
        
        Dim nextpoint As Position3D
        nextpoint = Origin
        Dim i As Integer
        
        Dim inter As Position3D
        Dim size As Size3D
        For i = 1 To Steps
            nextpoint.x = nextpoint.x + increment.x
            nextpoint.Y = nextpoint.Y + increment.Y
            nextpoint.Z = nextpoint.Z + increment.Z
            inter.x = Math.Round(nextpoint.x)
            inter.Y = Math.Round(nextpoint.Y)
            inter.Z = Math.Round(nextpoint.Z)
            If (IsOnCamera(inter, camera1.Position, camera1.size) = True) Then
                GetInCamVector = inter
                Exit For 'yes i must exit the 'for' for not get unexpected results
            End If
        Next i
    End Function
    thank you so much for all
    VB6 2D Sprite control

    To live is difficult, but we do it.

  5. #5

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,515

    Re: how can i walk throw the line?

    i'm testing more these function, but i don't get some points and i don't know why
    Code:
    'if the line don't use Z, the Z value must be zero
    Public Function GetLinePoints(ByRef Origin As Position3D, ByRef Destiny As Position3D) As Position3D()
        Dim Steps As Double
        Dim Points() As Position3D
        
        Steps = Math.Sqr(Math.Abs(Destiny.X - Origin.X) ^ 2 + Math.Abs(Destiny.Y - Origin.Y) ^ 2 + Math.Abs(Destiny.Z - Origin.Z) ^ 2)
        Steps = Ceil(Steps)
        
        Dim increment As Position3D
        If (Steps = 0) Then Steps = 1
        increment.X = (Destiny.X - Origin.X) / Steps
        increment.Y = (Destiny.Y - Origin.Y) / Steps
        increment.Z = (Destiny.Z - Origin.Z) / Steps
        
        Dim nextpoint As Position3D
        nextpoint = Origin
        Dim i As Integer
        
        Dim inter As Position3D
        Dim size As Size3D
        ReDim Points(Steps)
        For i = 1 To Steps
            nextpoint.X = nextpoint.X + increment.X
            nextpoint.Y = nextpoint.Y + increment.Y
            nextpoint.Z = nextpoint.Z + increment.Z
            inter.X = Math.Round(nextpoint.X)
            inter.Y = Math.Round(nextpoint.Y)
            inter.Z = Math.Round(nextpoint.Z)
            Points(i).X = inter.X
            Points(i).Y = inter.Y
            Points(i).Z = inter.Z
            'Debug.Print "X: " + CStr(inter.X) + vbTab + "Y: " + CStr(inter.Y) + vbTab + "Z: " + CStr(inter.Z)
        Next i
        GetLinePoints = Points
    End Function
    heres how i use it:
    Code:
    Friend Sub DrawImageRectanglePoints(DestinationHDC As Long, Points() As Position3D, WorldSize As Size3D)
        'Points(0) is the Upper-Left
        'Points(1) is the Upper-Right
        'Points(2) is the Low-Right
        'Points(3) is the Low-Left
        
        Dim PointsUpperDownLeft() As Position3D
        PointsUpperDownLeft = GetLinePoints(Points(0), Points(3))
        Dim PointsUpperDownRight() As Position3D
        PointsUpperDownRight = GetLinePoints(Points(1), Points(2))
        Dim X As Long
        Dim Y As Long
        Dim R As Long
        Dim G As Long
        Dim B As Long
        
        Dim DrawPixelPoints() As Position3D
        For Y = UBound(PointsUpperDownLeft) To 0 Step -1
            DrawPixelPoints = GetLinePoints(PointsUpperDownLeft(Y), PointsUpperDownRight(Y))
            For X = 0 To UBound(DrawPixelPoints)
                Dim Point As POINTAPI
                Dim color As Long
                Dim PosX As Long
                Dim PosY As Long
                PosX = X
                PosY = Y
                If (PosX > ImageWidth) Then
                    While (PosX >= ImageWidth)
                        PosX = PosX - ImageWidth
                    Wend
                End If
                
                If (PosY >= ImageHeight) Then
                    While (PosY > ImageHeight)
                        PosY = PosY - ImageHeight
                    Wend
                End If
                
                GdipBitmapGetPixel hBitmap, PosX, PosY, color
                R = color And &HFF
                G = (color And &HFF00&) \ &H100&
                B = (color And &HFF0000) \ &H10000
                If (color = CLR_INVALID) Then Exit For
                Point = ConvertPositon3DTo2D(DrawPixelPoints(X), WorldSize)
                SetPixelV DestinationHDC, Point.X, Point.Y, RGB(R, G, B)
            Next X
        Next Y
    End Sub
    the result is:
    Attachment 179362
    the bottom horizontal lines, aren't painted... maybe these function don't calculate that points on vertical line.... what i'm doing wrong?
    VB6 2D Sprite control

    To live is difficult, but we do it.

  6. #6

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,515

    Re: how can i walk throw the line?

    - for get the line points is from up to bottom
    - for draw the image is bottom to up
    VB6 2D Sprite control

    To live is difficult, but we do it.

  7. #7

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,515

    Re: how can i walk throw the line?

    i found why i get that empty pixels... have to do with camera size... thank you so much for all
    VB6 2D Sprite control

    To live is difficult, but we do it.

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