123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- package classes;
- import java.awt.Point;
- /**
- * Coordinates of an Object on the canvas with a (int) x-coord and a (int).
- * y-coord
- *
- * @author Gruppe14
- *
- */
- public class Position {
- /** Coordinate*/
- public int x,y;
- /**
- * Constructor with an positive x and y coord on the canvas.
- *
- * @param x Coord
- * @param y Coord
- */
- public Position(int x, int y) {
- this.x = x;
- this.y = y;
- }
- /**
- * Convert a Point to a Position;
- * @param p Point
- */
- public Position(Point p)
- {
- this.x = (int)p.getX();
- this.y = (int)p.getY();
- }
- public Position(Vector2d vector)
- {
- this.x = Math.round(vector.getX());
- this.y = Math.round(vector.getY());
- }
- /**
- * Default constructor without defined position.
- */
- public Position() {
- this.x = -1;
- this.y = -1;
- }
- /**
- * Return the Distance squared to a other Position.
- * Faster then Distance because no Sqrt() needed.
- * @param other the other Position.
- * @return squared distance to the Position
- */
- public double squareDistance(Position other)
- {
- //The Distance in X
- double xDis = x - other.x;
- //The Distance in Y
- double yDis = y - other.y;
- return xDis * xDis + yDis * yDis;
- }
- /**
- * Return the Distance to a other Position.
- * @param other the other Position.
- * @return distance to the Position.
- */
- public double Distance(Position other)
- {
- return Math.sqrt(squareDistance(other));
- }
-
- /**
- * Clamp the X Value two a upper or lower bound
- * @param min lower bound
- * @param max upper bound
- */
- public void clampX(int min, int max)
- {
- if(x < min) x = min;
- if(x > max) x = max;
- }
- /**
- * Clamp the Y Value two a upper or lower bound
- * @param min lower bound
- * @param max upper bound
- */
- public void clampY(int min, int max)
- {
- if(y < min) y = min;
- if(y > max) y = max;
- }
- /**
- * Returns a String that represents the value of this Position.
- * @return a string representation of this Position.
- */
- @Override
- public String toString() {
- return "Position[x=" + x + ",y="+ y +"]";
- }
- public Vector2d toVector() {
- return new Vector2d(x, y);
- }
- }
|