Vector2.java

1
/*
2
 * Copyright 2006 - 2013
3
 *     Stefan Balev     <stefan.balev@graphstream-project.org>
4
 *     Julien Baudry    <julien.baudry@graphstream-project.org>
5
 *     Antoine Dutot    <antoine.dutot@graphstream-project.org>
6
 *     Yoann Pign��      <yoann.pigne@graphstream-project.org>
7
 *     Guilhelm Savin   <guilhelm.savin@graphstream-project.org>
8
 * 
9
 * This file is part of GraphStream <http://graphstream-project.org>.
10
 * 
11
 * GraphStream is a library whose purpose is to handle static or dynamic
12
 * graph, create them from scratch, file or any source and display them.
13
 * 
14
 * This program is free software distributed under the terms of two licenses, the
15
 * CeCILL-C license that fits European law, and the GNU Lesser General Public
16
 * License. You can  use, modify and/ or redistribute the software under the terms
17
 * of the CeCILL-C license as circulated by CEA, CNRS and INRIA at the following
18
 * URL <http://www.cecill.info> or under the terms of the GNU LGPL as published by
19
 * the Free Software Foundation, either version 3 of the License, or (at your
20
 * option) any later version.
21
 * 
22
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
23
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
24
 * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
25
 * 
26
 * You should have received a copy of the GNU Lesser General Public License
27
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28
 * 
29
 * The fact that you are presently reading this means that you have had
30
 * knowledge of the CeCILL-C and LGPL licenses and that you accept their terms.
31
 */
32
package org.graphstream.ui.geom;
33
34
public class Vector2 implements java.io.Serializable {
35
	// Attributes
36
37
	private static final long serialVersionUID = 8839258036865851454L;
38
39
	/**
40
	 * Sequence of 3 coefficients.
41
	 */
42
	public double data[];
43
44
	// Constructors
45
46
	/**
47
	 * New zero vector.
48
	 */
49
	public Vector2() {
50
		data = new double[2];
51
		data[0] = 0;
52
		data[1] = 0;
53
	}
54
55
	/**
56
	 * New (<code>x</code>,<code>y</code>) vector.
57
	 */
58
	public Vector2(double x, double y) {
59
		data = new double[2];
60
		data[0] = x;
61
		data[1] = y;
62
	}
63
64
	/**
65
	 * New vector copy of <code>other</code>.
66
	 */
67
	public Vector2(Vector2 other) {
68
		data = new double[2];
69 1 1. : removed call to org/graphstream/ui/geom/Vector2::copy → NO_COVERAGE
		copy(other);
70
	}
71
72
	/**
73
	 * New vector copy of <code>point</code>.
74
	 */
75
	public Vector2(Point2 point) {
76
		data = new double[2];
77 1 1. : removed call to org/graphstream/ui/geom/Vector2::copy → NO_COVERAGE
		copy(point);
78
	}
79
	
80
	public Vector2(Point2 from, Point2 to) {
81
		data = new double[2];
82 1 1. : Replaced double subtraction with addition → NO_COVERAGE
		data[0] = to.x - from.x;
83 1 1. : Replaced double subtraction with addition → NO_COVERAGE
		data[1] = to.y - from.y;
84
	}
85
86
	// Predicates
87
88
	/**
89
	 * Are all components to zero?.
90
	 */
91
	public boolean isZero() {
92 4 1. isZero : negated conditional → NO_COVERAGE
2. isZero : negated conditional → NO_COVERAGE
3. isZero : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
4. isZero : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
		return (data[0] == 0 && data[1] == 0);
93
	}
94
95
	/**
96
	 * Is this equal to other ?
97
	 */
98
	@Override
99
	public boolean equals(Object other) {
100
		Vector2 v;
101
102 1 1. equals : negated conditional → NO_COVERAGE
		if (!(other instanceof Vector2)) {
103 1 1. equals : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
			return false;
104
		}
105
106
		v = (Vector2) other;
107
108 4 1. equals : negated conditional → NO_COVERAGE
2. equals : negated conditional → NO_COVERAGE
3. equals : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
4. equals : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
		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 6 1. validComponent : changed conditional boundary → NO_COVERAGE
2. validComponent : changed conditional boundary → NO_COVERAGE
3. validComponent : negated conditional → NO_COVERAGE
4. validComponent : negated conditional → NO_COVERAGE
5. validComponent : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
6. validComponent : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
		return (i >= 0 && i < 2);
118
	}
119
120
	// Accessors:
121
122
	/**
123
	 * i-th element.
124
	 */
125
	public double at(int i) {
126 1 1. at : replaced return of double value with -(x + 1) for org/graphstream/ui/geom/Vector2::at → NO_COVERAGE
		return data[i];
127
	}
128
	
129
	public double x() {
130 1 1. x : replaced return of double value with -(x + 1) for org/graphstream/ui/geom/Vector2::x → NO_COVERAGE
		return data[0];
131
	}
132
	
133
	public double y() {
134 1 1. y : replaced return of double value with -(x + 1) for org/graphstream/ui/geom/Vector2::y → NO_COVERAGE
		return data[1];
135
	}
136
137
	@Override
138
	public Object clone() {
139 1 1. clone : mutated return of Object value for org/graphstream/ui/geom/Vector2::clone to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
		return new Vector2(this);
140
	}
141
142
	// Accessors
143
144
	public double dotProduct(double ox, double oy) {
145 4 1. dotProduct : Replaced double multiplication with division → NO_COVERAGE
2. dotProduct : Replaced double multiplication with division → NO_COVERAGE
3. dotProduct : Replaced double addition with subtraction → NO_COVERAGE
4. dotProduct : replaced return of double value with -(x + 1) for org/graphstream/ui/geom/Vector2::dotProduct → NO_COVERAGE
		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 4 1. dotProduct : Replaced double multiplication with division → NO_COVERAGE
2. dotProduct : Replaced double multiplication with division → NO_COVERAGE
3. dotProduct : Replaced double addition with subtraction → NO_COVERAGE
4. dotProduct : replaced return of double value with -(x + 1) for org/graphstream/ui/geom/Vector2::dotProduct → NO_COVERAGE
		return ((data[0] * other.data[0]) + (data[1] * other.data[1]));
153
	}
154
155
	/**
156
	 * Cartesian length.
157
	 */
158
	public double length() {
159 4 1. length : Replaced double multiplication with division → NO_COVERAGE
2. length : Replaced double multiplication with division → NO_COVERAGE
3. length : Replaced double addition with subtraction → NO_COVERAGE
4. length : replaced return of double value with -(x + 1) for org/graphstream/ui/geom/Vector2::length → NO_COVERAGE
		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 1 1. add : Replaced double addition with subtraction → NO_COVERAGE
		data[0] += other.data[0];
191 1 1. add : Replaced double addition with subtraction → NO_COVERAGE
		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 1 1. sub : Replaced double subtraction with addition → NO_COVERAGE
		data[0] -= other.data[0];
199 1 1. sub : Replaced double subtraction with addition → NO_COVERAGE
		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 1 1. mult : Replaced double multiplication with division → NO_COVERAGE
		data[0] *= other.data[0];
207 1 1. mult : Replaced double multiplication with division → NO_COVERAGE
		data[1] *= other.data[1];
208
	}
209
210
	/**
211
	 * Add value to each element.
212
	 */
213
	public void scalarAdd(double value) {
214 1 1. scalarAdd : Replaced double addition with subtraction → NO_COVERAGE
		data[0] += value;
215 1 1. scalarAdd : Replaced double addition with subtraction → NO_COVERAGE
		data[1] += value;
216
	}
217
218
	/**
219
	 * Substract value to each element.
220
	 */
221
	public void scalarSub(double value) {
222 1 1. scalarSub : Replaced double subtraction with addition → NO_COVERAGE
		data[0] -= value;
223 1 1. scalarSub : Replaced double subtraction with addition → NO_COVERAGE
		data[1] -= value;
224
	}
225
226
	/**
227
	 * Multiply each element by value.
228
	 */
229
	public void scalarMult(double value) {
230 1 1. scalarMult : Replaced double multiplication with division → NO_COVERAGE
		data[0] *= value;
231 1 1. scalarMult : Replaced double multiplication with division → NO_COVERAGE
		data[1] *= value;
232
	}
233
234
	/**
235
	 * Divide each element by value.
236
	 */
237
	public void scalarDiv(double value) {
238 1 1. scalarDiv : Replaced double division with multiplication → NO_COVERAGE
		data[0] /= value;
239 1 1. scalarDiv : Replaced double division with multiplication → NO_COVERAGE
		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 1 1. normalize : negated conditional → NO_COVERAGE
		if (len != 0) {
251 1 1. normalize : Replaced double division with multiplication → NO_COVERAGE
			data[0] /= len;
252 1 1. normalize : Replaced double division with multiplication → NO_COVERAGE
			data[1] /= len;
253
		}
254
255 1 1. normalize : replaced return of double value with -(x + 1) for org/graphstream/ui/geom/Vector2::normalize → NO_COVERAGE
		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 1 1. toString : mutated return of Object value for org/graphstream/ui/geom/Vector2::toString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
		return sb.toString();
288
	}
289
}

Mutations

69

1.1
Location :
Killed by : none
removed call to org/graphstream/ui/geom/Vector2::copy → NO_COVERAGE

77

1.1
Location :
Killed by : none
removed call to org/graphstream/ui/geom/Vector2::copy → NO_COVERAGE

82

1.1
Location :
Killed by : none
Replaced double subtraction with addition → NO_COVERAGE

83

1.1
Location :
Killed by : none
Replaced double subtraction with addition → NO_COVERAGE

92

1.1
Location : isZero
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : isZero
Killed by : none
negated conditional → NO_COVERAGE

3.3
Location : isZero
Killed by : none
replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE

4.4
Location : isZero
Killed by : none
replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE

102

1.1
Location : equals
Killed by : none
negated conditional → NO_COVERAGE

103

1.1
Location : equals
Killed by : none
replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE

108

1.1
Location : equals
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : equals
Killed by : none
negated conditional → NO_COVERAGE

3.3
Location : equals
Killed by : none
replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE

4.4
Location : equals
Killed by : none
replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE

117

1.1
Location : validComponent
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : validComponent
Killed by : none
changed conditional boundary → NO_COVERAGE

3.3
Location : validComponent
Killed by : none
negated conditional → NO_COVERAGE

4.4
Location : validComponent
Killed by : none
negated conditional → NO_COVERAGE

5.5
Location : validComponent
Killed by : none
replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE

6.6
Location : validComponent
Killed by : none
replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE

126

1.1
Location : at
Killed by : none
replaced return of double value with -(x + 1) for org/graphstream/ui/geom/Vector2::at → NO_COVERAGE

130

1.1
Location : x
Killed by : none
replaced return of double value with -(x + 1) for org/graphstream/ui/geom/Vector2::x → NO_COVERAGE

134

1.1
Location : y
Killed by : none
replaced return of double value with -(x + 1) for org/graphstream/ui/geom/Vector2::y → NO_COVERAGE

139

1.1
Location : clone
Killed by : none
mutated return of Object value for org/graphstream/ui/geom/Vector2::clone to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

145

1.1
Location : dotProduct
Killed by : none
Replaced double multiplication with division → NO_COVERAGE

2.2
Location : dotProduct
Killed by : none
Replaced double multiplication with division → NO_COVERAGE

3.3
Location : dotProduct
Killed by : none
Replaced double addition with subtraction → NO_COVERAGE

4.4
Location : dotProduct
Killed by : none
replaced return of double value with -(x + 1) for org/graphstream/ui/geom/Vector2::dotProduct → NO_COVERAGE

152

1.1
Location : dotProduct
Killed by : none
Replaced double multiplication with division → NO_COVERAGE

2.2
Location : dotProduct
Killed by : none
Replaced double multiplication with division → NO_COVERAGE

3.3
Location : dotProduct
Killed by : none
Replaced double addition with subtraction → NO_COVERAGE

4.4
Location : dotProduct
Killed by : none
replaced return of double value with -(x + 1) for org/graphstream/ui/geom/Vector2::dotProduct → NO_COVERAGE

159

1.1
Location : length
Killed by : none
Replaced double multiplication with division → NO_COVERAGE

2.2
Location : length
Killed by : none
Replaced double multiplication with division → NO_COVERAGE

3.3
Location : length
Killed by : none
Replaced double addition with subtraction → NO_COVERAGE

4.4
Location : length
Killed by : none
replaced return of double value with -(x + 1) for org/graphstream/ui/geom/Vector2::length → NO_COVERAGE

190

1.1
Location : add
Killed by : none
Replaced double addition with subtraction → NO_COVERAGE

191

1.1
Location : add
Killed by : none
Replaced double addition with subtraction → NO_COVERAGE

198

1.1
Location : sub
Killed by : none
Replaced double subtraction with addition → NO_COVERAGE

199

1.1
Location : sub
Killed by : none
Replaced double subtraction with addition → NO_COVERAGE

206

1.1
Location : mult
Killed by : none
Replaced double multiplication with division → NO_COVERAGE

207

1.1
Location : mult
Killed by : none
Replaced double multiplication with division → NO_COVERAGE

214

1.1
Location : scalarAdd
Killed by : none
Replaced double addition with subtraction → NO_COVERAGE

215

1.1
Location : scalarAdd
Killed by : none
Replaced double addition with subtraction → NO_COVERAGE

222

1.1
Location : scalarSub
Killed by : none
Replaced double subtraction with addition → NO_COVERAGE

223

1.1
Location : scalarSub
Killed by : none
Replaced double subtraction with addition → NO_COVERAGE

230

1.1
Location : scalarMult
Killed by : none
Replaced double multiplication with division → NO_COVERAGE

231

1.1
Location : scalarMult
Killed by : none
Replaced double multiplication with division → NO_COVERAGE

238

1.1
Location : scalarDiv
Killed by : none
Replaced double division with multiplication → NO_COVERAGE

239

1.1
Location : scalarDiv
Killed by : none
Replaced double division with multiplication → NO_COVERAGE

250

1.1
Location : normalize
Killed by : none
negated conditional → NO_COVERAGE

251

1.1
Location : normalize
Killed by : none
Replaced double division with multiplication → NO_COVERAGE

252

1.1
Location : normalize
Killed by : none
Replaced double division with multiplication → NO_COVERAGE

255

1.1
Location : normalize
Killed by : none
replaced return of double value with -(x + 1) for org/graphstream/ui/geom/Vector2::normalize → NO_COVERAGE

287

1.1
Location : toString
Killed by : none
mutated return of Object value for org/graphstream/ui/geom/Vector2::toString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 0.33