Skip to content

Instantly share code, notes, and snippets.

@MattRoelle
Created December 6, 2012 04:10
Show Gist options
  • Save MattRoelle/4221715 to your computer and use it in GitHub Desktop.
Save MattRoelle/4221715 to your computer and use it in GitHub Desktop.
import static java.lang.Math.*;
public class Vector {
public float x, y, magnitude;
public Vector(double angle) {
this.x = (float) cos(angle);
this.y = (float) sin(angle);
initValues();
}
public Vector(float angle) {
this.x = (float) cos(angle);
this.y = (float) sin(angle);
initValues();
}
public Vector(float x, float y) {
this.x = x;
this.y = y;
initValues();
}
private void initValues() {
this.magnitude = (float) sqrt( (pow(x,2)+pow(y,2)) );
}
public Vector normalize() {
float dx = this.x / this.magnitude;
float dy = this.y / this.magnitude;
return new Vector(dx,dy);
}
public void speedUp(float speed) {
this.x = this.x*speed;
this.y = this.y*speed;
initValues();
}
public Vector add(Vector v2) {
float x3 = this.x+v2.x;
float y3 = this.y+v2.y;
return new Vector(x3,y3);
}
public Vector subtract(Vector v2) {
float x3 = this.x-v2.x;
float y3 = this.y-v2.y;
return new Vector(x3,y3);
}
public Vector multiply(Vector v2) {
float x3 = this.x*v2.x;
float y3 = this.y*v2.y;
return new Vector(x3,y3);
}
public Vector divide(Vector v2) {
float x3 = this.x/v2.x;
float y3 = this.y/v2.y;
return new Vector(x3,y3);
}
public Vector mod(Vector v2) {
float x3 = this.x%v2.x;
float y3 = this.y%v2.y;
return new Vector(x3,y3);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment