-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSphere.java
More file actions
64 lines (53 loc) · 2.04 KB
/
Sphere.java
File metadata and controls
64 lines (53 loc) · 2.04 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
/* File: Sphere.java
* Date: 4/20/17
* Student: William Tyas
* ID: W01203451
* Class: CSCI 241, Spring 2017
* Description: a sphere object with a center, radius, and color
*/
public class Sphere {
private nTuple center;
private float r; //radius
private nTuple color;
public Sphere() {
this.center = new nTuple(0.0f, 0.0f, 0.0f);
this.r = 0;
this.color = new nTuple(0.0f, 0.0f, 0.0f);
}
public Sphere(nTuple c, float r, nTuple color) {
this.center = c;
this.r = r;
this.color = color;
}
public nTuple center() {
return this.center;
}
public float r() {
return this.r;
}
public nTuple color() {
return this.color;
}
//Sets the center of the sphere
public void setCenter(float newX, float newY, float newZ) {
this.center.setNTuple(newX, newY, newZ);
}
// Calculate the bounding box for the sphere
public nTuple getBoundBox(World world) {
nTuple lCorner = new nTuple();
nTuple uCorner = new nTuple();
// Calculate x extent
float aX = (float) Math.sqrt(Math.pow(this.center().x(), 2) + Math.pow(this.center().z() - world.camera().z(), 2));
float thetaX = (float) Math.atan(this.r() / aX);
float omegaX = (float) Math.asin(this.center().x() / aX);
float phiX = omegaX - thetaX;
// Calculate y extent
float aY = (float) Math.sqrt(Math.pow(this.center().y(), 2) + Math.pow(this.center().z() - world.camera().z(), 2));
float thetaY = (float) Math.atan(this.r() / aY);
float omegaY = (float) Math.asin(this.center().y() / aY);
float phiY = omegaY - thetaY;
lCorner.setNTuple(world.camera().z() * ((float) Math.tan(phiX)), world.camera().z() * ((float) Math.tan(phiY)), 0);
uCorner.setNTuple(world.camera().z() * ((float) Math.tan(phiX + 2 * thetaX)), world.camera().z() * ((float) Math.tan(phiY + 2 * thetaY)), 0);
return new nTuple(lCorner, uCorner);
}
}