Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. private int ComputeDelta(int src, int dst, int mapSize)
  2. {
  3. int increasing, decreasing;
  4. if (dst >= src)
  5. {
  6. increasing = dst - src; // increasing direction is direct
  7. decreasing = (mapSize + src) - dst; // decreasing direction is toroidal
  8. }
  9. else
  10. {
  11. increasing = (mapSize + dst) - src; // increasing direction is toroidal
  12. decreasing = src - dst; // decreasing direction is direct
  13. }
  14.  
  15. if (increasing <= decreasing) { return increasing; }
  16. else { return -decreasing; }
  17. }
  18.  
  19. public Position GetNextStepTowards(Position origin, Position target)
  20. {
  21. Position nextStep = new Position(0, 0);
  22.  
  23. // compute the distances
  24. int dx = ComputeDelta(origin.X, target.X, mapXSize);
  25. int dy = ComputeDelta(origin.Y, target.Y, mapYSize);
  26.  
  27. // keep the dominant distance, and clear the other distance
  28. if (dx*dx > dy*dy) { dy = 0; }
  29. else if (dx*dx < dy*dy) { dx = 0; }
  30.  
  31. // normalize the distances so they are -1, 0, or 1
  32. nextStep.X = dx.CompareTo(0);
  33. nextStep.Y = dy.CompareTo(0);
  34.  
  35. return nextStep;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement