001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * JUMP is Copyright (C) 2003 Vivid Solutions
006: *
007: * This program implements extensions to JUMP and is
008: * Copyright (C) 2004 Integrated Systems Analysts, Inc.
009: *
010: * This program is free software; you can redistribute it and/or
011: * modify it under the terms of the GNU General Public License
012: * as published by the Free Software Foundation; either version 2
013: * of the License, or (at your option) any later version.
014: *
015: * This program is distributed in the hope that it will be useful,
016: * but WITHOUT ANY WARRANTY; without even the implied warranty of
017: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
018: * GNU General Public License for more details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with this program; if not, write to the Free Software
022: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
023: *
024: * For more information, contact:
025: *
026: * Integrated Systems Analysts, Inc.
027: * 630C Anchors St., Suite 101
028: * Fort Walton Beach, Florida
029: * USA
030: *
031: * (850)862-7321
032: */
033:
034: package org.openjump.core.geomutils;
035:
036: import com.vividsolutions.jts.geom.Coordinate;
037:
038: public class MathVector implements Cloneable, java.io.Serializable {
039: private double x;
040: private double y;
041: private double mag;
042:
043: public MathVector() {
044: this (0, 0);
045: }
046:
047: public MathVector(Coordinate coord) {
048: this (coord.x, coord.y);
049: }
050:
051: public MathVector(double x, double y) {
052: if ((new Double(x)).isNaN())
053: this .x = 0.0;
054: else
055: this .x = x;
056:
057: if ((new Double(y)).isNaN())
058: this .y = 0.0;
059: else
060: this .y = y;
061:
062: mag = Math.sqrt(x * x + y * y);
063: }
064:
065: public double x() {
066: return x;
067: }
068:
069: public double y() {
070: return y;
071: }
072:
073: public Coordinate getCoord() {
074: return new Coordinate(x, y);
075: }
076:
077: public String toString() {
078: return "(" + x + ", " + y + ")";
079: }
080:
081: public boolean equals(Object object) {
082: if (object instanceof MathVector) {
083: MathVector vector = (MathVector) object;
084:
085: return x == vector.x() && y == vector.y();
086: }
087: return false;
088: }
089:
090: public Object clone() {
091: return new MathVector(x, y);
092: }
093:
094: public double magnitude() {
095: return mag;
096: }
097:
098: public MathVector add(MathVector vector) {
099: return new MathVector(x + vector.x(), y + vector.y());
100: }
101:
102: public MathVector scale(double number) {
103: return new MathVector(x * number, y * number);
104: }
105:
106: public MathVector unit() {
107: if (mag == 0)
108: return new MathVector();
109: else
110: return new MathVector(x / mag, y / mag);
111: }
112:
113: public double dot(MathVector vector) {
114: return x * vector.x() + y * vector.y();
115: }
116:
117: public double distance(MathVector vector) {
118: return scale(-1).add(vector).magnitude();
119: }
120:
121: public MathVector vectorBetween(MathVector vector) {
122: return new MathVector(x - vector.x(), y - vector.y());
123: }
124:
125: public double angleRad(MathVector vector) {
126: // compute the angle in radians between vectors from 0 to pi
127: // NOTE: this routine returns POSITIVE angles only
128: return Math.acos(dot(vector) / (mag * vector.magnitude()));
129: }
130:
131: public double angleDeg(MathVector vector) {
132: // compute the angle in degrees between vectors from 0 to pi
133: // NOTE: this routine returns POSITIVE angles only
134: return Math.toDegrees(Math.acos(dot(vector)
135: / (mag * vector.magnitude())));
136: }
137:
138: public MathVector rotateDeg(double angle) { //+ clockwise
139: double tr = Math.toRadians(angle);
140: double ct = Math.cos(tr);
141: double st = Math.sin(tr);
142: return new MathVector((x * ct + y * st), (y * ct - st * x));
143: }
144:
145: public MathVector rotateRad(double angle) { //+ clockwise
146: double tr = angle;
147: double ct = Math.cos(tr);
148: double st = Math.sin(tr);
149: return new MathVector((x * ct + y * st), (y * ct - st * x));
150: }
151: }
|