-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnTuple.java
More file actions
102 lines (85 loc) · 2.22 KB
/
nTuple.java
File metadata and controls
102 lines (85 loc) · 2.22 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
/* File: nTuple.java
* Date: 4/20/17
* Student: William Tyas
* ID: W01203451
* Class: CSCI 241, Spring 2017
* Description: a three-dimensional n-Tuple
*/
public class nTuple {
private float x;
private float y;
private float z;
private double vectorLength;
private nTuple p;
private nTuple v;
public nTuple() {
this.x = 0.0f;
this.y = 0.0f;
this.z = 0.0f;
}
public nTuple(float x, float y) {
this.x = x;
this.y = y;
}
public nTuple(float x, float y, float z) {
this.x = x;
this.y = y;
this.z = z;
}
public nTuple(nTuple p, nTuple v) {
this.p = p; //somehow set x to p
this.v = v; //somehow set y to v
}
public float x() {
return this.x;
}
public float y() {
return this.y;
}
public float z() {
return this.z;
}
// The next two methods return an nTuple stored within this nTuple
public nTuple getP() {
return this.p;
}
public nTuple getV() {
return this.v;
}
public nTuple getNTuple() {
return this;
}
public void setNTuple(float newX, float newY) {
this.x = newX;
this.y = newY;
this.z = 0.0f;
}
public void setNTuple(float newX, float newY, float newZ) {
this.x = newX;
this.y = newY;
this.z = newZ;
}
public void setX(float newX) {
this.x = newX;
}
public void setY(float newY) {
this.y = newY;
}
public void setZ(float newZ) {
this.z = newZ;
}
// Normalize the nTuple
public void normalize() {
if (this.x != 0 | this.y != 0 | this.z != 0) {
vectorLength = Math.sqrt(Math.pow((double) x, (double) 2) + Math.pow((double) y, (double) 2) + Math.pow((double) z, (double) 2));
this.x = (float) (x / vectorLength);
this.y = (float) (y / vectorLength);
this.z = (float) (z / vectorLength);
}
}
// Multiply this nTuple by a factor
public nTuple multiply(float factor) {
this.setNTuple(factor * this.x(), factor * this.y(), factor * this.z());
return this;
}
}