package main; public class Location { private double x; private double y; public Location(final double x, final double y) { this.x = x; this.y = y; } public Location(final Location copy) { this.x = copy.x; this.y = copy.y; } public int getX() { return (int)Math.round(this.x); } public int getY() { return (int)Math.round(this.y); } public void setLocation(final double x, final double y) { this.x = x; this.y = y; } public void setLocation(final Location loc) { this.x = loc.x; this.y = loc.y; } public boolean moveToTarget(final double x, final double y, final double distance) { final double totalDist = Math.sqrt(Math.pow(x - this.x, 2.0) + Math.pow(y - this.y, 2.0)); double ratio = 1.0; boolean arrived = false; if (distance < totalDist) { ratio = distance / totalDist; } else { arrived = true; } this.x += (x - this.x) * ratio; this.y += (y - this.y) * ratio; return arrived; } public double distance(final Location loc) { return Math.sqrt(Math.pow(this.x - loc.x, 2.0) + Math.pow(this.y - loc.y, 2.0)); } }