Coord.java :  » Game » androiddefense » com » androiddefense » engine » coordinate » Android Open Source

Android Open Source » Game » androiddefense 
androiddefense » com » androiddefense » engine » coordinate » Coord.java
/**
 * 
 */
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);
    }
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.