Thread: Best way to calculate these steps?

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    184

    Best way to calculate these steps?

    I have a canvas with something at a point and when the user enters in a new point, to move it to there. The issue is that i want to move it "evenly" and smoothly.

    If abs( x - newX ) == abs( y - newY ), then there's no issue. Just move +/-1 in each direction. The issue becomes when the diff.'s in the x points is not the same as the diff's in the y points. What algorithm would i use to make it move in a straight line to the point, in consistent steps?

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I am a blond. So speak slowly here:

    [Point] -------------------------- [Some other point] ??

    Are you just trying to move in a line from one place to another (not necessarily a horizontal one, as in my ascii rendition of the problem).

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So figure out how many steps you want to take (we will call it 17 for the purposes of discussion). Then divide the difference (in x, and in y) by 17 and use that as the step size.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Or look up Bresenham's line drawing algorithm - it is one of the better.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Quote Originally Posted by matsp View Post
    Or look up Bresenham's line drawing algorithm - it is one of the better.

    --
    Mats
    Thanks, I'll look that up.

    What I am doing is figuring out which "diff" is less and then making that be the number of steps.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help! -Linked Lists, Memory Allocation, Time Steps, Debugg
    By MetallicaX in forum C Programming
    Replies: 2
    Last Post: 03-14-2009, 08:50 PM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  4. Small program that has to calculate miles per gallon
    By Guti14 in forum C++ Programming
    Replies: 6
    Last Post: 01-06-2004, 02:47 PM