001: package org.cougaar.core.qos.frame.visualizer.util;
002:
003: public class Vec2d {
004: private double x;
005: private double y;
006:
007: public Vec2d() {
008: }
009:
010: public Vec2d(Vec2d v) {
011: this (v.x, v.y);
012: }
013:
014: public Vec2d(double x, double y) {
015: setXY(x, y);
016: }
017:
018: public Vec2d copy() {
019: return new Vec2d(this );
020: }
021:
022: public void setXY(double x, double y) {
023: this .x = x;
024: this .y = y;
025: }
026:
027: public void setX(double x) {
028: this .x = x;
029: }
030:
031: public void setY(double y) {
032: this .y = y;
033: }
034:
035: public double getX() {
036: return x;
037: }
038:
039: public double getY() {
040: return y;
041: }
042:
043: public double dot(Vec2d v) {
044: return x * v.x + y * v.y;
045: }
046:
047: public double length() {
048: return Math.sqrt(lengthSquared());
049: }
050:
051: public double lengthSquared() {
052: return this .dot(this );
053: }
054:
055: public void normalize() {
056: double len = length();
057: if (len == 0.0f)
058: return;
059: scale(1.0f / len);
060: }
061:
062: public Vec2d times(double val) {
063: Vec2d newVec = new Vec2d(this );
064: newVec.scale(val);
065: return newVec;
066: }
067:
068: public void scale(double val) {
069: x *= val;
070: y *= val;
071: }
072:
073: public Vec2d plus(Vec2d arg) {
074: Vec2d newVec = new Vec2d();
075: newVec.add(this , arg);
076: return newVec;
077: }
078:
079: public void add(Vec2d b) {
080: add(this , b);
081: }
082:
083: public void add(Vec2d a, Vec2d b) {
084: x = a.x + b.x;
085: y = a.y + b.y;
086: }
087:
088: public Vec2d addScaled(double s, Vec2d arg) {
089: Vec2d newVec = new Vec2d();
090: newVec.addScaled(this , s, arg);
091: return newVec;
092: }
093:
094: public void addScaled(Vec2d a, double s, Vec2d b) {
095: x = a.x + s * b.x;
096: y = a.y + s * b.y;
097: }
098:
099: public Vec2d minus(Vec2d arg) {
100: Vec2d newVec = new Vec2d();
101: newVec.sub(this , arg);
102: return newVec;
103: }
104:
105: public void sub(Vec2d b) {
106: sub(this , b);
107: }
108:
109: public void sub(Vec2d a, Vec2d b) {
110: x = a.x - b.x;
111: y = a.y - b.y;
112: }
113:
114: public String toString() {
115: return "Vec2d[" + x + ", " + y + "]";
116: }
117: }
|