Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Coordinates problem

Coordinates problem

Scheduled Pinned Locked Moved C#
helpquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Silvyster
    wrote on last edited by
    #1

    I have two coordinates on the screen x1 and y1 and i have an object at that location.. now i have x2 and y2 which will be the destination point of my object.. I am planning to move my object 1 pixel at a time following the path from x1, y1 to x2, y2.. is there a formula that you can recommend?

    M 1 Reply Last reply
    0
    • S Silvyster

      I have two coordinates on the screen x1 and y1 and i have an object at that location.. now i have x2 and y2 which will be the destination point of my object.. I am planning to move my object 1 pixel at a time following the path from x1, y1 to x2, y2.. is there a formula that you can recommend?

      M Offline
      M Offline
      molesworth
      wrote on last edited by
      #2

      Just work out the ratio of x2 - x1 to y2 - y1, and that will tell you the ratio of pixels to move in x versus y. If you want reasonably smooth movement you'll need to track positions in floats. If you want very smooth movement you'll need to take the time per update frame into account.

      There are three kinds of people in the world - those who can count and those who can't...

      S 1 Reply Last reply
      0
      • M molesworth

        Just work out the ratio of x2 - x1 to y2 - y1, and that will tell you the ratio of pixels to move in x versus y. If you want reasonably smooth movement you'll need to track positions in floats. If you want very smooth movement you'll need to take the time per update frame into account.

        There are three kinds of people in the world - those who can count and those who can't...

        S Offline
        S Offline
        Silvyster
        wrote on last edited by
        #3

        yeah i was asking for the ratio formula

        M 1 Reply Last reply
        0
        • S Silvyster

          yeah i was asking for the ratio formula

          M Offline
          M Offline
          molesworth
          wrote on last edited by
          #4

          Try something like :-

          float ratio = (float)(x2 - x1) / (float)(y2 - y1);
          

          The casts make sure you don't truncate the result.

          There are three kinds of people in the world - those who can count and those who can't...

          S 1 Reply Last reply
          0
          • M molesworth

            Try something like :-

            float ratio = (float)(x2 - x1) / (float)(y2 - y1);
            

            The casts make sure you don't truncate the result.

            There are three kinds of people in the world - those who can count and those who can't...

            S Offline
            S Offline
            Silvyster
            wrote on last edited by
            #5

            well actually from what i understand the locations in the C# controls such as a form and a panel are by pixels and of the INT data type. I'm trying to move it pixel by pixel.. I do remember I had a formula taught to me by my math teacher that given the coordinates of both ends of a straight line, you can solve all the POINTS in between.

            OriginalGriffO M 2 Replies Last reply
            0
            • S Silvyster

              well actually from what i understand the locations in the C# controls such as a form and a panel are by pixels and of the INT data type. I'm trying to move it pixel by pixel.. I do remember I had a formula taught to me by my math teacher that given the coordinates of both ends of a straight line, you can solve all the POINTS in between.

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              What you are looking for is called Bresenham's algorithm Clickity[^]

              No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              1 Reply Last reply
              0
              • S Silvyster

                well actually from what i understand the locations in the C# controls such as a form and a panel are by pixels and of the INT data type. I'm trying to move it pixel by pixel.. I do remember I had a formula taught to me by my math teacher that given the coordinates of both ends of a straight line, you can solve all the POINTS in between.

                M Offline
                M Offline
                molesworth
                wrote on last edited by
                #7

                Silvyster wrote:

                from what i understand the locations in the C# controls such as a form and a panel are by pixelsand of the INT data type.

                The problem you'll have is that unless you're moving your object exactly along the X or Y direction, or at 45 degrees, you won't have an integer number of pixels in each direction. What you need to do is something like :-

                float ratio = (float)(y2 - y1) / (float)(x2 - x1);    // note : this is dY / dX, not as in previous post
                
                int step = (x2 > x1)? 1 : -1;
                
                float newFloatY = y1;
                int newX = x1;
                
                for (int loop=x1; loop!=x2; loop+=step)
                {
                    newX++;
                    newFloatY += ratio;
                
                    int newY = (int)(newFloatY);
                
                    // position object at newX, newY...
                }
                

                You'll need to modify it if you want to move by 1 pixel in Y, and cope with special cases like moving exactly horizontally or vertically, but those are "left as an exercise for the reader"... :)

                There are three kinds of people in the world - those who can count and those who can't...

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups