-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector.java
More file actions
153 lines (129 loc) · 4.46 KB
/
Vector.java
File metadata and controls
153 lines (129 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package jp.jaxa.iss.kibo.rpc.Salcedo;
import gov.nasa.arc.astrobee.types.Point;
public class Vector {
private final int n; // length of the vector
private double[] data; // array of vector's components
// create the zero vector of length n
public Vector(int n) {
this.n = n;
this.data = new double[n];
}
// create a vector from an array
public Vector(double[] data) {
n = data.length;
// defensive copy so that client can't alter our copy of data[]
this.data = new double[n];
for (int i = 0; i < n; i++)
this.data[i] = data[i];
}
public Vector(Point data) {
this.n = 3;
// defensive copy so that client can't alter our copy of data[]
this.data = new double[n];
this.data[0] = data.getX();
this.data[1] = data.getY();
this.data[2] = data.getZ();
}
// create a vector from either an array or a vararg list
// this constructor uses Java's vararg syntax to support
// a constructor that takes a variable number of arguments, such as
// Vector x = new Vector(1.0, 2.0, 3.0, 4.0);
// Vector y = new Vector(5.0, 2.0, 4.0, 1.0);
/*
public Vector(double... data) {
n = data.length;
// defensive copy so that client can't alter our copy of data[]
this.data = new double[n];
for (int i = 0; i < n; i++)
this.data[i] = data[i];
}
*/
public Point toPoint() {
// defensive copy so that client can't alter our copy of data[]
Point point = new Point(this.data[0],this.data[1],this.data[2]);
return point;
}
// return the length of the vector
public int length() {
return n;
}
// return the inner product of this Vector a and b
public double dot(Vector that) {
if (this.length() != that.length())
throw new IllegalArgumentException("dimensions disagree");
double sum = 0.0f;
for (int i = 0; i < n; i++)
sum = sum + (this.data[i] * that.data[i]);
return sum;
}
// return the Euclidean norm of this Vector
public double magnitude() {
return (double)Math.sqrt(this.dot(this));
}
// return the Euclidean distance between this and that
public double distanceTo(Vector that) {
if (this.length() != that.length())
throw new IllegalArgumentException("dimensions disagree");
return this.minus(that).magnitude();
}
// return this + that
public Vector plus(Vector that) {
if (this.length() != that.length())
throw new IllegalArgumentException("dimensions disagree");
Vector c = new Vector(n);
for (int i = 0; i < n; i++)
c.data[i] = this.data[i] + that.data[i];
return c;
}
// return this - that
public Vector minus(Vector that) {
if (this.length() != that.length())
throw new IllegalArgumentException("dimensions disagree");
Vector c = new Vector(n);
for (int i = 0; i < n; i++)
c.data[i] = this.data[i] - that.data[i];
return c;
}
public Vector minusScalar(double scalar) {
Vector c = new Vector(n);
for (int i = 0; i < n; i++)
c.data[i] = this.data[i] - scalar;
return c;
}
// return the corresponding coordinate
public double cartesian(int i) {
return data[i];
}
// create and return a new object whose value is (this * factor)
@Deprecated
public Vector times(double factor) {
Vector c = new Vector(n);
for (int i = 0; i < n; i++)
c.data[i] = factor * data[i];
return c;
}
// create and return a new object whose value is (this * factor)
public Vector scale(double factor) {
Vector c = new Vector(n);
for (int i = 0; i < n; i++)
c.data[i] = factor * data[i];
return c;
}
// return the corresponding unit vector
public Vector direction() {
if (this.magnitude() == 0.0f)
throw new ArithmeticException("zero-vector has no direction");
return this.scale(1.0f / this.magnitude());
}
// return a string representation of the vector
public String toString() {
StringBuilder s = new StringBuilder();
s.append('(');
for (int i = 0; i < n; i++) {
s.append(data[i]);
if (i < n-1) s.append(", ");
}
s.append(')');
return s.toString();
}
}