public class Vector2D { public float x; public float y; Vector2D() { x = 0; y = 0; } Vector2D(float x_, float y_) { x = x_; y = y_; } Vector2D(double x_, double y_) { x = (float)x_; y = (float)y_; } Vector2D(Vector2D v) { x = v.x; y = v.y; } public void Set(Vector2D v) { x = v.x; y = v.y; } public void Set(float x_, float y_) { x = x_; y = y_; } public void Set(double x_, double y_) { x = (float)x_; y = (float)y_; } public float magnitude() { return (float) Math.sqrt(x*x + y*y); } public void Normalize() { double m = magnitude(); x /= (float)m; y /= (float)m; } public Vector2D retNormalize() { double m = magnitude(); return new Vector2D((float)(x/m), (float)(y/m)); } public Vector2D GetCopy() { return new Vector2D(this); } //can be called without an instance of Vector2D public static Vector2D Add(Vector2D v1, Vector2D v2) { return new Vector2D(v1.x+v2.x, v1.y+v2.y); } //Meant to be called on an instance of Vector2D public Vector2D Plus(Vector2D v) { return new Vector2D(x+v.x, y+v.y); } public void PlusEquals(Vector2D v) { x += v.x; y += v.y; } public static Vector2D Subtract(Vector2D v1, Vector2D v2) { return new Vector2D(v1.x-v2.x, v1.y-v2.y); } public Vector2D Minus(Vector2D v) { return new Vector2D(x-v.x, y-v.y); } public void MinusEquals(Vector2D v) { x -= v.x; y -= v.y; } public Vector2D Times(float s) { return new Vector2D(x*s,y*s); } public void TimesEquals(float s) { x *= s; y *= s; } public static float DotProd(Vector2D v1, Vector2D v2) { return (v1.x*v2.x + v1.y*v2.y); } public float Dot(Vector2D v) { return (x*v.x + y*v.y); } public void Negate() { x = -x; y = -y; } public Vector2D Negative() { return new Vector2D(-x,-y); } //Reflects V about N // Assumes that the tails of v and N are connected - may need to negate v static public Vector2D Reflect(Vector2D v, Vector2D N) { float coefficient = 2 * DotProd(v,N); return (N.Times(coefficient)).Minus(v); } //Taken from Dan Schiffman's Vector3D class public float Heading() { float angle = (float) Math.atan2(-y,x); return -1*angle; } //Maybe I should add functions for rotating 90 degrees public void Rotate90CW() { float t = x; x = -y; y = t; } //Limit vector size public void limitMax(float max) { if (magnitude() > max) { Normalize(); TimesEquals(max); } } public void limitMin(float min) { if (magnitude() < min) { Normalize(); TimesEquals(min); } } }