Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private class TileArea {
- public int ny, sy, wx, ex;
- //wx is the western x, ny the northern y, and so on..
- public TileArea(int wx, int ny, int ex, int sy) {
- this.ny = ny;
- this.sy = sy;
- this.ex = ex;
- this.wx = wx;
- }
- //nw is the North west tile, and se the South east tile in the TileArea
- public TileArea(RSTile nw, RSTile se) {
- ny = nw.getY();
- wx = nw.getX();
- sy = se.getY();
- ex = se.getX();
- }
- //returns true if the tile area contains the given tile
- public boolean contains(RSTile t) {
- return t.getX() >= wx && t.getX() <= ex && t.getY() <= ny && t.getY() >= sy;
- }
- //gives you a random tile from inside the tile area
- public RSTile getRandomTile() {
- return new RSTile(random(wx, ex+1), random(sy, ny+1));
- }
- //DDM sucks at trig :D
- //returns the closest tile inside the tilearea to t
- //if t is inside this tilearea, it will return t
- public RSTile getNearestTileTo(RSTile t) {
- RSTile closest = null;
- double cx = (ex+wx)/2, cy = (ny+sy)/2;
- double dx = t.getX() - cx, dy = t.getY() - cy, d = Math.sqrt(dx*dx+dy*dy);
- if(dx == 0 && dy == 0)
- return t;
- if(dx > dy) {
- double sideX = dy/dx*(getWidth()/2), sideY = dy/dx/(getHeight()/2);
- return new RSTile((int)(cx+Math.min(dx,sideX)),(int)(cy+Math.min(dy,sideY)));
- } else {
- double sideX = dx/dy/(getWidth()/2), sideY = dx/dy*(getHeight()/2);
- return new RSTile((int)(cx+Math.min(dx,sideX)),(int)(cy+Math.min(dy,sideY)));
- }
- }
- public int getWidth() { return ex-wx; }
- public int getHeight() { return ny-sy; }
- }
Add Comment
Please, Sign In to add comment