00001
00004 package edu.stanford.hci.r3.util.geometry;
00005
00006 import java.awt.geom.Point2D;
00007
00016 public class Vector2D {
00017
00025 public static Point2D add(Point2D p, Vector2D v) {
00026 final Point2D result = new Point2D.Double();
00027 result.setLocation(p.getX() + v.x, p.getY() + v.y);
00028 return result;
00029 }
00030
00036 public static Vector2D add(Vector2D a, Vector2D b) {
00037 final Vector2D result = new Vector2D();
00038 result.x = a.x + b.x;
00039 result.y = a.y + b.y;
00040 return result;
00041 }
00042
00046 public static Vector2D getNormalized(Vector2D v) {
00047 return getScaled(1.0, v);
00048 }
00049
00054 public static Vector2D getScaled(double desiredMagnitude, Vector2D v) {
00055 final double scaleFactor = desiredMagnitude / v.magnitude();
00056 final double newX = v.x * scaleFactor;
00057 final double newY = v.y * scaleFactor;
00058 return new Vector2D(newX, newY);
00059 }
00060
00066 public static Vector2D multiply(double d, Vector2D vector2D) {
00067 return new Vector2D(vector2D.x * d, vector2D.y * d);
00068 }
00069
00077 public static Vector2D subtract(Point2D a, Point2D b) {
00078 final Vector2D result = new Vector2D();
00079 result.x = a.getX() - b.getX();
00080 result.y = a.getY() - b.getY();
00081 return result;
00082 }
00083
00087 private double x = 0;
00088
00092 private double y = 0;
00093
00094 public Vector2D() {
00095
00096 }
00097
00102 public Vector2D(double _x, double _y) {
00103 x = _x;
00104 y = _y;
00105 }
00106
00107 public double getX() {
00108 return x;
00109 }
00110
00114 public double getY() {
00115 return y;
00116 }
00117
00121 public double magnitude() {
00122 return Math.sqrt(x * x + y * y);
00123 }
00124
00125 }