public
Created

my description of a vector

  • Download Gist
gistfile1.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
package com.jantox.dungmast;
 
public class Vector2D {
 
public double x, y;
public Vector2D() {
x = y = 0;
}
public Vector2D(double x, double y) {
this.x = x;
this.y = y;
}
public int getX() {
return (int) x;
}
public int getY() {
return (int) y;
}
public double dotProduct(Vector2D b) {
return x * b.x + y * b.y;
}
public double distanceSquared(Vector2D b) {
return Math.pow(b.x - x, 2) + Math.pow(b.y - y, 2);
}
public double length() {
return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
}
public void normalize() {
double length = this.length();
x /= length;
y /= length;
}
public Vector2D copy() {
return new Vector2D(x, y);
}
public void add(Vector2D b) {
x += b.x;
y += b.y;
}
public void subtract(Vector2D b) {
x -= b.x;
y -= b.y;
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.