Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 216
  • Last Modified:

Moving Cursor, using Pythagorous - Nietod

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 :-)

  • 2
1 Solution
AntBonAuthor Commented:
Adjusted points from 10 to 25
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.
AntBonAuthor Commented:
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now