Thread: Moving in a straight line to the target

  1. #1
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020

    Moving in a straight line to the target

    Hi,

    FOr example i have this situation:
    Code:
    |                              
    |                            
    |                                   X
    | 
    |    O
    |
    |
    |_____________________
    And i want "O" to move to "X". How can i do this in a straight line. Using pythagoras' theorem?

    Thanks

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Subtracting O's position from X's will give you a vector. A vector is a direction and a displacement. That should probably help you.

    EDIT: Fixed a typo!
    Last edited by MrWizard; 10-10-2002 at 10:59 AM.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    Nah. Can you please give me a formula or something, from which i can then work out the concept.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Okay, some basic mathematics. We have a point O and a point X in 2D cartesian space. X - O = V ( a 2D vector ).

    P = O + kV

    where P is our new point , O is our original point, k is a scalar between 0 and 1 and V is our vector. You see, if we have 0 for k we get point O. If we use 1 for k we get the endpoint of the vector, or X. So, by incrementing k we will affectively move along this line towards X our destination. Hope this helped you. Let me know if I need to clarify.

  5. #5
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    mmm..What about the x and y values of "O"? ALso in your first post you actually said to minus X off O, which is O - X, so i was confused ^.^, and i still is. How do you calculate the x and y values along the straight path towards the new point?

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    oops! My first post is incorrect! My second one is mathematically sound, however. Let's assume that O is at (1, 1) and x is at (7,2). We do our computation.

    P = O + kV ( V = X - O = (6,1) )

    It's trivial to plug in 0 for k because we will get (1,1) the starting point of O. If we plug in 0.5 for example, we get.

    P = (1,1) + (0.5)(7,2) =
    P = (1,1) + (3.5,1) =
    P = (4.5,2)

    That's halfway to our destination!

    An example with 0.1 as k ( remember k is between 0 and 1 )

    P = (1,1) + (0.1)(7,2) =
    P = (1,1) + (0.7,0.2) =
    P = (1.7,1.2)

    Now due to rounding you will lost accuracy of course but you get the idea.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Line of data input method
    By larry_2k4 in forum C Programming
    Replies: 2
    Last Post: 04-28-2009, 11:34 PM
  2. help again with scrolling without wrapping
    By Dukefrukem in forum C Programming
    Replies: 8
    Last Post: 09-21-2007, 12:48 PM
  3. Trouble replacing line of file
    By Rpog in forum C Programming
    Replies: 4
    Last Post: 04-19-2004, 10:22 AM
  4. Read only one line using seekg
    By RedZippo in forum C++ Programming
    Replies: 3
    Last Post: 03-31-2004, 11:10 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM