/**
*
*/
package com.androiddefense.engine.coordinate;
import android.util.Log;
/**
* Coord class
*
* @author Steven Christou
*
*/
public class Coord {
public final int x, y;
public Coord(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return x + "," + y;
}
@Override
public boolean equals(Object other) {
return other instanceof Coord &&
((Coord) other).x == x &&
((Coord) other).y == y;
}
public boolean isWithin(final int howFar, Coord dest) {
return howFar >= Math.abs(x - dest.x) + Math.abs(y - dest.y);
}
public Coord moveToByGreaterDist(Coord other) {
if (this.equals(other)) {
return other;
}
Log.i("GameModel", "moveToByGreaterDist, this Coord " + this
+ ", other " + other);
final int dx = other.x - x;
final int dy = other.y - y;
final int absDx = Math.abs(dx);
final int absDy = Math.abs(dy);
if (absDx > absDy) {
return new Coord(x + absDx / dx, y);
} else {
return new Coord(x, y + absDy / dy);
}
}
}
|