Moving Cursor, using Pythagorous - Nietod

Posted on 2000-04-10
Last Modified: 2010-04-02
nietod, you previously advised me to implement 'Pythagorous's theory' to move the cursor.

I have tried to do what you said, but i am still having problems..can you help me out - or tell me where i am going wrong

here is the code

if (Dest.x > Source.x) //Move to the right
//   xdir = RIGHT;
   xlength=Dest.x - Source.x;
//   xdistance=Dest.x - Source.x;
   xlength = Source.x - Dest.x; //Move to the left
//   xdistance = Source.x - Dest.x; //Move to the left
//   xdir = LEFT;

if (Dest.y > Source.y) //Move Downwards
   ylength=Dest.y - Source.y;
//   ydistance=Dest.y - Source.y;
//   ydir = DOWN;
   ylength = Source.y - Dest.y; //Move upwards
//   ydistance = Source.y - Dest.y;
//   ydir = UP;



   hypot=abs(sqrt(xlength+ylength)); // work out length of the hypotenuse in pixels

   for (i=0;i<=hypot;i++) // move in pixels

   for (int pause = 0;pause < 1000000; pause++);;


PS i have got new delay mechanism :-)

Question by:AntBon
  • 2

Author Comment

ID: 2700683
Adjusted points from 10 to 25
LVL 22

Accepted Solution

nietod earned 25 total points
ID: 2700810
int DeltaX  = Dst.x - Src.x;
int DeltaY  = Dst.y - Src.y;

// this is perhaps rounded down, that is fine.
 int distance = (int) sqrt(DeltaX*DeltaX + DeltaY*DeltaY);

// you may wnat to start i at 1, not 0,  starting at 0
// makes the agorithm incude the Source point.  
for (int i = 0; i <=1 distance; ++i)
   int x = Src.x+ i*DeltaX/distance;
   int y =  Src.y+i*DeltaY/distance;
   // move cursor or do something with x and y.

That is it.  No need to worry about signs.  DeltaX and DeltaY are signed.  Although some rounding occurs (+ or - 1) for the interior points (because of integer coordinates) The final value of x and y should be exactly the destination coordinates.  (i.e there is no rounding errors in the final coordinates.

Author Comment

ID: 2700882
Thanks very much, but i have just done it myself, but at least i can compare my solution with your, on the face of it they look the same, except i started my for loop at 0, but i will change now

Thanks Again

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now