001/*
002 * Copyright 2006 - 2013
003 *     Stefan Balev     <stefan.balev@graphstream-project.org>
004 *     Julien Baudry    <julien.baudry@graphstream-project.org>
005 *     Antoine Dutot    <antoine.dutot@graphstream-project.org>
006 *     Yoann Pigné      <yoann.pigne@graphstream-project.org>
007 *     Guilhelm Savin   <guilhelm.savin@graphstream-project.org>
008 * 
009 * This file is part of GraphStream <http://graphstream-project.org>.
010 * 
011 * GraphStream is a library whose purpose is to handle static or dynamic
012 * graph, create them from scratch, file or any source and display them.
013 * 
014 * This program is free software distributed under the terms of two licenses, the
015 * CeCILL-C license that fits European law, and the GNU Lesser General Public
016 * License. You can  use, modify and/ or redistribute the software under the terms
017 * of the CeCILL-C license as circulated by CEA, CNRS and INRIA at the following
018 * URL <http://www.cecill.info> or under the terms of the GNU LGPL as published by
019 * the Free Software Foundation, either version 3 of the License, or (at your
020 * option) any later version.
021 * 
022 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
023 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
024 * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
025 * 
026 * You should have received a copy of the GNU Lesser General Public License
027 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
028 * 
029 * The fact that you are presently reading this means that you have had
030 * knowledge of the CeCILL-C and LGPL licenses and that you accept their terms.
031 */
032package org.graphstream.ui.geom;
033
034public class Vector2 implements java.io.Serializable {
035        // Attributes
036
037        private static final long serialVersionUID = 8839258036865851454L;
038
039        /**
040         * Sequence of 3 coefficients.
041         */
042        public double data[];
043
044        // Constructors
045
046        /**
047         * New zero vector.
048         */
049        public Vector2() {
050                data = new double[2];
051                data[0] = 0;
052                data[1] = 0;
053        }
054
055        /**
056         * New (<code>x</code>,<code>y</code>) vector.
057         */
058        public Vector2(double x, double y) {
059                data = new double[2];
060                data[0] = x;
061                data[1] = y;
062        }
063
064        /**
065         * New vector copy of <code>other</code>.
066         */
067        public Vector2(Vector2 other) {
068                data = new double[2];
069                copy(other);
070        }
071
072        /**
073         * New vector copy of <code>point</code>.
074         */
075        public Vector2(Point2 point) {
076                data = new double[2];
077                copy(point);
078        }
079        
080        public Vector2(Point2 from, Point2 to) {
081                data = new double[2];
082                data[0] = to.x - from.x;
083                data[1] = to.y - from.y;
084        }
085
086        // Predicates
087
088        /**
089         * Are all components to zero?.
090         */
091        public boolean isZero() {
092                return (data[0] == 0 && data[1] == 0);
093        }
094
095        /**
096         * Is this equal to other ?
097         */
098        @Override
099        public boolean equals(Object other) {
100                Vector2 v;
101
102                if (!(other instanceof Vector2)) {
103                        return false;
104                }
105
106                v = (Vector2) other;
107
108                return (data[0] == v.data[0] && data[1] == v.data[1]);
109        }
110
111        /**
112         * Is i the index of a component ?
113         * 
114         * In other words, is i &gt;= 0 &amp;&amp; &lt; than #count() ?
115         */
116        public boolean validComponent(int i) {
117                return (i >= 0 && i < 2);
118        }
119
120        // Accessors:
121
122        /**
123         * i-th element.
124         */
125        public double at(int i) {
126                return data[i];
127        }
128        
129        public double x() {
130                return data[0];
131        }
132        
133        public double y() {
134                return data[1];
135        }
136
137        @Override
138        public Object clone() {
139                return new Vector2(this);
140        }
141
142        // Accessors
143
144        public double dotProduct(double ox, double oy) {
145                return ((data[0] * ox) + (data[1] * oy));
146        }
147
148        /**
149         * Dot product of this and other.
150         */
151        public double dotProduct(Vector2 other) {
152                return ((data[0] * other.data[0]) + (data[1] * other.data[1]));
153        }
154
155        /**
156         * Cartesian length.
157         */
158        public double length() {
159                return Math.sqrt((data[0] * data[0]) + (data[1] * data[1]));
160        }
161
162        // Commands
163
164        /**
165         * Assign value to all elements.
166         */
167        public void fill(double value) {
168                data[0] = data[1] = value;
169        }
170
171        /**
172         * Explicitly set the i-th component to value.
173         */
174        public void set(int i, double value) {
175                data[i] = value;
176        }
177
178        /**
179         * Explicitly set the three components.
180         */
181        public void set(double x, double y) {
182                data[0] = x;
183                data[1] = y;
184        }
185
186        /**
187         * Add each element of other to the corresponding element of this.
188         */
189        public void add(Vector2 other) {
190                data[0] += other.data[0];
191                data[1] += other.data[1];
192        }
193
194        /**
195         * Subtract each element of other to the corresponding element of this.
196         */
197        public void sub(Vector2 other) {
198                data[0] -= other.data[0];
199                data[1] -= other.data[1];
200        }
201
202        /**
203         * Multiply each element of this by the corresponding element of other.
204         */
205        public void mult(Vector2 other) {
206                data[0] *= other.data[0];
207                data[1] *= other.data[1];
208        }
209
210        /**
211         * Add value to each element.
212         */
213        public void scalarAdd(double value) {
214                data[0] += value;
215                data[1] += value;
216        }
217
218        /**
219         * Substract value to each element.
220         */
221        public void scalarSub(double value) {
222                data[0] -= value;
223                data[1] -= value;
224        }
225
226        /**
227         * Multiply each element by value.
228         */
229        public void scalarMult(double value) {
230                data[0] *= value;
231                data[1] *= value;
232        }
233
234        /**
235         * Divide each element by value.
236         */
237        public void scalarDiv(double value) {
238                data[0] /= value;
239                data[1] /= value;
240        }
241
242        /**
243         * Transform this into an unit vector.
244         * 
245         * @return the vector length.
246         */
247        public double normalize() {
248                double len = length();
249
250                if (len != 0) {
251                        data[0] /= len;
252                        data[1] /= len;
253                }
254
255                return len;
256        }
257
258        // Utility
259
260        /**
261         * Make this a copy of other.
262         */
263        public void copy(Vector2 other) {
264                data[0] = other.data[0];
265                data[1] = other.data[1];
266        }
267
268        /**
269         * Make this a copy of <code>point</code>.
270         */
271        public void copy(Point2 point) {
272                data[0] = point.x;
273                data[1] = point.y;
274        }
275
276        // Misc.
277
278        @Override
279        public String toString() {
280                StringBuffer sb = new StringBuffer("[");
281
282                sb.append(data[0]);
283                sb.append('|');
284                sb.append(data[1]);
285                sb.append(']');
286
287                return sb.toString();
288        }
289}