Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // mutable 2D vectors
- public final class Vector2D {
- public double x, y;
- // constructor for zero vector
- public Vector2D() {
- this.x = 0;
- this.y = 0;
- }
- // constructor for vector with given coordinates
- public Vector2D(double x, double y) {
- this.x = x;
- this.y = y;
- }
- // constructor that copies the argument vector
- public Vector2D(Vector2D v) {
- this.x = v.x;
- this.y = v.y;
- }
- // set coordinates
- public Vector2D set(double x, double y) {
- this.x = x;
- this.y = y;
- return this;
- }
- // set coordinates based on argument vector
- public Vector2D set(Vector2D v) {
- this.x = v.x;
- this.y = v.y;
- return this;
- }
- // compare for equality (note Object type argument)
- public boolean equals(Object o) {
- if (o instanceof Vector2D){
- Vector2D v = (Vector2D) o;
- return this.x == v.x && this.y == v.y;
- }
- return false;
- }
- // String for displaying vector as text
- public String toString() {
- return "This vector has the X cord: " + this.x + "And Y cord: " + this.y;
- }
- // magnitude (= "length") of this vector
- public double mag() {
- return Math.hypot(this.x, this.y);
- }
- // angle between vector and horizontal axis in radians
- // can be calculated using Math.atan2
- public double angle() {
- return Math.atan2(this.y, this.x);
- }
- // angle between this vector and another vector
- // take difference of angles, add 2*Math.PI if result is negative
- public double angle(Vector2D other) {
- return subtract(other).angle();
- }
- // add argument vector
- public Vector2D add(Vector2D v) {
- this.x += v.x;
- this.y += v.y;
- return this;
- }
- // add values to coordinates
- public Vector2D add(double x, double y) {
- this.x += x;
- this.y += y;
- return this;
- }
- // weighted add - surprisingly useful
- public Vector2D addScaled(Vector2D v, double fac) {
- this.x += (v.x*fac);
- this.y += (v.y*fac);
- return this;
- }
- // subtract argument vector
- public Vector2D subtract(Vector2D v) {
- this.x -= v.x;
- this.y -= v.y;
- return this;
- }
- // subtract values from coordinates
- public Vector2D subtract(double x, double y) {
- this.x -= x;
- this.y -= y;
- return this;
- }
- // multiply with factor
- public Vector2D mult(double fac) {
- this.x = this.x*fac;
- this.y = this.y*fac;
- return this;
- }
- // rotate by angle given in radians
- public Vector2D rotate(double angle) {
- double a = this.x;
- double b = this.y;
- this.x = a*Math.cos(angle) - b*Math.sin(angle);
- this.y = a*Math.sin(angle) + b*Math.cos(angle);
- return this;
- }
- // "dot product" ("scalar product") with argument vector
- public double dot(Vector2D v) {
- return this.x * v.x + this.y * v.y;
- }
- // distance to argument vector
- public double dist(Vector2D v) {
- return new Vector2D(v.x - this.x, v.y - this.y).mag();
- }
- // normalise vector so that magnitude becomes 1
- public Vector2D normalise() {
- return this.mult(1/this.mag());
- }
- // wrap-around operation, assumes w> 0 and h>0
- public Vector2D wrap(double w, double h) {
- this.x = (x+w)%w;
- this.y = (y+h)%h;
- return this;
- }
- // construct vector with given polar coordinates
- public static Vector2D polar(double angle, double mag) {
- return new Vector2D(angle * Math.cos(mag), angle * Math.sin(mag));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement