Difference between revisions of "User:Nat/Free code"

From Robowiki
Jump to navigation Jump to search
(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 ...)
 
m (update some grammar, code)
Line 1: Line 1:
Here I put all my code snippet here. No license apply.
+
Here I'll put all my code snippets. No license apply.
__NOTOC__ __NOEDITSECTION__
 
 
[[Category:Code Snippet|Nat's Free Code]]
 
[[Category:Code Snippet|Nat's Free Code]]
 
== Extended Point2D ==
 
== Extended Point2D ==
Line 6: Line 5:
 
<pre>
 
<pre>
 
public class ExtendedPoint2D extends Point2D.Double {
 
public class ExtendedPoint2D extends Point2D.Double {
 +
private static final long serialVersionUID = 74324L;
 
public ExtendedPoint2D(double x, double y) {
 
public ExtendedPoint2D(double x, double y) {
 
super(x, y);
 
super(x, y);

Revision as of 13:29, 31 March 2009

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)