Guest User

Untitled

a guest
Jul 21st, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. private class TileArea {
  2. public int ny, sy, wx, ex;
  3. //wx is the western x, ny the northern y, and so on..
  4. public TileArea(int wx, int ny, int ex, int sy) {
  5. this.ny = ny;
  6. this.sy = sy;
  7. this.ex = ex;
  8. this.wx = wx;
  9. }
  10. //nw is the North west tile, and se the South east tile in the TileArea
  11. public TileArea(RSTile nw, RSTile se) {
  12. ny = nw.getY();
  13. wx = nw.getX();
  14. sy = se.getY();
  15. ex = se.getX();
  16. }
  17. //returns true if the tile area contains the given tile
  18. public boolean contains(RSTile t) {
  19. return t.getX() >= wx && t.getX() <= ex && t.getY() <= ny && t.getY() >= sy;
  20. }
  21. //gives you a random tile from inside the tile area
  22. public RSTile getRandomTile() {
  23. return new RSTile(random(wx, ex+1), random(sy, ny+1));
  24. }
  25. //DDM sucks at trig :D
  26. //returns the closest tile inside the tilearea to t
  27. //if t is inside this tilearea, it will return t
  28. public RSTile getNearestTileTo(RSTile t) {
  29. RSTile closest = null;
  30. double cx = (ex+wx)/2, cy = (ny+sy)/2;
  31. double dx = t.getX() - cx, dy = t.getY() - cy, d = Math.sqrt(dx*dx+dy*dy);
  32. if(dx == 0 && dy == 0)
  33. return t;
  34. if(dx > dy) {
  35. double sideX = dy/dx*(getWidth()/2), sideY = dy/dx/(getHeight()/2);
  36. return new RSTile((int)(cx+Math.min(dx,sideX)),(int)(cy+Math.min(dy,sideY)));
  37. } else {
  38. double sideX = dx/dy/(getWidth()/2), sideY = dx/dy*(getHeight()/2);
  39. return new RSTile((int)(cx+Math.min(dx,sideX)),(int)(cy+Math.min(dy,sideY)));
  40. }
  41. }
  42. public int getWidth() { return ex-wx; }
  43. public int getHeight() { return ny-sy; }
  44. }
Add Comment
Please, Sign In to add comment