User:Nat/Free code

From Robowiki
< User:Nat
Revision as of 15:34, 19 March 2009 by Nat (talk | contribs) (New page: Here I put all my code snippet here. No license apply. __NOTOC__ __NOEDITSECTION__ Nat's Free Code == Extended Point2D == Here my version of extended Point2D. It ...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Here I put all my code snippet here. No license apply.

Extended Point2D

Here my version of extended Point2D. It can convert vector into location within its constructor and provide angleTo function, which apply to robocode only. I found it useful to keep enemy data in melee with Kawigi style. (class EnemyInfo extends Point2D.Double)

public class ExtendedPoint2D extends Point2D.Double {
	public ExtendedPoint2D(double x, double y) {
		super(x, y);
	}
	
	public ExtendedPoint2D(Point2D location, double angle, double dist) {
		double enemyX = location.getX() + Math.sin(angle) * dist;
		double enemyY = location.getY() + Math.cos(angle) * dist;
		x = enemyX;
		y = enemyY;
	}
	
	public void setVectorLocation(double angle, double dist) {
		double enemyX = x + Math.sin(angle) * dist;
		double enemyY = y + Math.cos(angle) * dist;
		x = enemyX;
		y = enemyY;
	}
	
	public double angleTo(Point2D location) {
		double ex = location.getX();
		double ey = location.getY();
		return Math.atan2(ex - x, ey - y);
	}
	
	public double angleTo(double x, double y) {
		return angleTo(new Point2D.Double(x, y));
	}
}

The variable name is a bit confuse because I apply term 'enemy' where it should ve only another location, but I can't think better name.

(more to be come)