Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Locale;
- public class Vector2D {
- public double vX, vY;
- public Vector2D() {
- this.vX = 1.0;
- this.vY = 1.0;
- }
- public Vector2D(double x, double y) {
- this.vX = x;
- this.vY = y;
- }
- public Vector2D(Vector2D vector) {
- this.vX = vector.vX;
- this.vY = vector.vY;
- }
- public void print() {
- System.out.println("(" + String.format(Locale.US, "%.2f", vX) + ", " + String.format(Locale.US, "%.2f", vY) + ")");
- }
- public void add(Vector2D vector) {
- this.vX += vector.vX;
- this.vY += vector.vY;
- }
- public void sub(Vector2D vector) {
- this.vX -= vector.vX;
- this.vY -= vector.vY;
- }
- public double length(){
- return Math.pow(Math.pow(this.vX, 2) + Math.pow(this.vY, 2), 0.5);
- }
- public void scale(double scale) {
- this.vX *= scale;
- this.vY *= scale;
- }
- public void normalized() {
- this.vX = this.vX / this.length();
- this.vY = this.vY / this.length();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement