Created
January 17, 2013 00:50
-
-
Save RaasAhsan/4552530 to your computer and use it in GitHub Desktop.
my description of a vector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment