User:Nat/Free code

From Robowiki
< User:Nat
Revision as of 13:29, 31 March 2009 by Nat (talk | contribs) (update some grammar, code)
Jump to navigation Jump to search

Here I'll put all my code snippets. 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 {
	private static final long serialVersionUID = 74324L;
	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)