From 64f2596a246823699e6ce955166287e5b687e67b Mon Sep 17 00:00:00 2001 From: JKetelaar Date: Sat, 1 Nov 2014 00:33:28 +0100 Subject: [PATCH] Added the Area class --- .../rev317/min/api/methods/Calculations.java | 6 + src/org/rev317/min/api/wrappers/Area.java | 119 ++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 src/org/rev317/min/api/wrappers/Area.java diff --git a/src/org/rev317/min/api/methods/Calculations.java b/src/org/rev317/min/api/methods/Calculations.java index 3ec71e5..72c4648 100644 --- a/src/org/rev317/min/api/methods/Calculations.java +++ b/src/org/rev317/min/api/methods/Calculations.java @@ -1,7 +1,11 @@ package org.rev317.min.api.methods; +import org.rev317.min.Loader; +import org.rev317.min.accessors.Client; import org.rev317.min.api.wrappers.Tile; +import java.awt.*; + /** * @author Everel */ @@ -153,4 +157,6 @@ public class Calculations { } } + + } diff --git a/src/org/rev317/min/api/wrappers/Area.java b/src/org/rev317/min/api/wrappers/Area.java new file mode 100644 index 0000000..1c20b0d --- /dev/null +++ b/src/org/rev317/min/api/wrappers/Area.java @@ -0,0 +1,119 @@ +package org.rev317.min.api.wrappers; + +import java.awt.*; +import java.util.ArrayList; + +/** + * + * @author Matt, JKetelaar + * + */ +public class Area { + private Polygon p; + + /** + * Initializes a PolygonArea with the tiles given + * + * @param tiles + * tiles to use in the area + */ + public Area(Tile... tiles) { + this.p = new Polygon(); + for (int i = 0; i < tiles.length; i++) { + p.addPoint(tiles[i].getX(), tiles[i].getY()); + } + } + + /** + * Adds a tile to the area + * + * @param t + * The tile to add the area + */ + public void addTile(Tile t) { + p.addPoint(t.getX(), t.getY()); + } + + /** + * Gets all the tiles that are points in the area + * + * @return the tiles. + */ + public Tile[] getPoints() { + Tile[] tiles = new Tile[p.npoints]; + for (int i = 0; i < tiles.length; i++) + tiles[i] = new Tile(p.xpoints[i], p.ypoints[i]); + return tiles; + } + + /** + * Gets all the tiles that are contained within the points. + * + * @return the tiles. + */ + public Tile[] getTiles() { + int lowestX = -1; + int lowestY = -1; + int highestX = -1; + int highestY = -1; + ArrayList t = new ArrayList(); + for (int i : p.xpoints) { + if (i < lowestX || lowestX == -1) { + lowestX = i; + } + if (i > highestX || highestX == -1) { + highestX = i; + } + } + for (int i : p.ypoints) { + if (i < lowestY || lowestY == -1) { + lowestY = i; + } + if (i > highestY || highestY == -1) { + highestY = i; + } + } + for (int x = lowestX - 1; x < highestX + 1; x++) { + for (int y = lowestY - 1; y < highestY + 1; y++) { + if (this.contains(x, y)) { + t.add(new Tile(x, y)); + } + } + } + return t.toArray(new Tile[t.size()]); + } + + /** + * Checks if a tile is in the area + * + * @param tile + * The tile to check + * @return true if area does contain the tile, otherwise false + */ + public boolean contains(Tile tile) { + return this.contains(tile.getX(), tile.getY()); + } + + /** + * Checks if a tile is in the area + * + * @param x The x-axis from the tile + * @param y The y-axis from the tile + * + * @return True if the area does contain the tile, otherwise false + */ + public boolean contains(int x, int y) { + int i; + int j; + boolean result = false; + for (i = 0, j = p.npoints - 1; i < p.npoints; j = i++) { + if ((p.ypoints[i] > y - 1) != (p.ypoints[j] > y - 1) + && (x <= (p.xpoints[j] - p.xpoints[i]) * (y - p.ypoints[i]) + / (p.ypoints[j] - p.ypoints[i]) + p.xpoints[i])) { + result = !result; + } + } + return result; + } + +}