-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVec3.java
150 lines (134 loc) · 2.76 KB
/
Vec3.java
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
/**
* A Re^3 vector that supports vector operations. Compatible with the OpenGL
* convention.
*
* @author Justin C
*/
public class Vec3 extends Matrix implements Comparable<Vec3>
{
/**
* Constructs a Vec3 at (0, 0, 0).
*/
public Vec3()
{
super(3, 1);
}
/**
* Constructs a Vec3 at (x, y, z).
*/
public Vec3(double x, double y, double z)
{
super(new double[] { x, y, z });
}
/**
* Constructs a Vec3 from column "col" of Matrix o.
*/
public Vec3(Matrix o, int col)
{
this(o.v[0][col], o.v[1][col], o.v[2][col]);
}
/**
* Constructs a Vec3 from a Matrix. Also a copy constructor.
*/
public Vec3(Matrix o)
{
this(o, 0);
}
/**
* Returns the square of the length of the line segment from
* (0, 0, 0) to this Vec3.
*/
public double lengthSquared()
{
return v[0][0] * v[0][0] + v[1][0] * v[1][0] + v[2][0] * v[2][0];
}
/**
* Returns the length of the line segment from (0, 0, 0) to this Vec3.
*/
public double length()
{
return Math.sqrt(lengthSquared());
}
/**
* Returns (Vec3) (this.add(o)).
*/
public Vec3 add(Vec3 o)
{
return new Vec3(super.add(o));
}
/**
* Returns (Vec3) (this.add(-o)).
*/
public Vec3 sub(Vec3 o)
{
return this.add(o.dot(-1));
}
/**
* Returns (Vec3) (this.dot(o)).
*/
public Vec3 dot(Vec3 o)
{
return new Vec3(super.dot(o));
}
/**
* Returns (Vec3) (this.dot(o)).
*/
public Vec3 dot(double o)
{
return new Vec3(super.dot(o));
}
/**
* Returns (Vec3) (o.times(this)).
*/
public Vec3 itimes(Matrix o)
{
return new Vec3(o.times(this));
}
/**
* Returns a unit Vec3 parallel to the given Vec3.
*/
public Vec3 unit()
{
return this.dot(1 / length());
}
/**
* Returns the cross product "this x o".
*/
public Vec3 cross(Vec3 o)
{
return new Vec3(
v[1][0] * o.v[2][0] - v[2][0] * o.v[1][0],
v[2][0] * o.v[0][0] - v[0][0] * o.v[2][0],
v[0][0] * o.v[1][0] - v[1][0] * o.v[0][0]
);
}
/**
* Overwrite Object.equals
*/
public boolean equals(Object o)
{
if (o == null)
return false;
Vec3 other = (Vec3) o;
return other.v[0][0] == v[0][0] &&
other.v[1][0] == v[1][0] &&
other.v[2][0] == v[2][0];
}
/**
* Make Vec3 objects comparable.
*/
public int compareTo(Vec3 o)
{
return (int) (o.v[2][0] - v[2][0]);
}
/**
* Return the string representation of the Vec3.
*/
public String toString()
{
return "(" +
Matrix.round(v[0][0], 3)+", "+
Matrix.round(v[1][0], 3)+", "+
Matrix.round(v[2][0], 3)+")";
}
}