-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSphere.java
More file actions
86 lines (71 loc) · 2.64 KB
/
Sphere.java
File metadata and controls
86 lines (71 loc) · 2.64 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
/*
* Sphere.java
* Created by: William Tyas
* Date: 8/9/17
* Description: A sphere object, with methods to modify the sphere's
* attributes
*/
import java.awt.*;
public class Sphere {
private nTuple center;
private nTuple center_shadow; // coords in shadow coord sys
private float radius;
private nTuple color;
private float ambFactor = 0.1f; // ambient lighting
public nTuple getCenter() { return this.center; }
public nTuple getCenterShadow() { return this.center_shadow; }
public float getRadius() { return this.radius; }
public nTuple getColor() { return this.color; }
public Sphere(float x, float y, float z, float radius, float r,
float g, float b, nTuple lightBasis1, nTuple lightBasis2, nTuple lightBasis3) {
this.center = new nTuple(x, y, z);
this.center_shadow = this.center.coordChange(lightBasis1, lightBasis2, lightBasis3, this.center);
this.radius = radius;
this.color = new nTuple(r, g, b);
}
public Sphere(Sphere other) {
this.center = new nTuple(other.getCenter());
this.center_shadow = new nTuple(other.getCenterShadow());
this.radius = other.getRadius();
this.color = new nTuple(other.getColor());
}
public boolean isEqual(Sphere other) {
return (this.center.isEqual(other.getCenter()) && (this.radius == other.getRadius()) && (this.color.isEqual(other.getColor())));
}
@Override
public String toString() {
return this.center.toString();
}
//////////////////////////////////////////////////////////////////
// SPHERE MODIFICATIONS //
//////////////////////////////////////////////////////////////////
public void setSphere(Sphere other) {
this.center.setNTuple(other.getCenter());
this.center_shadow.setNTuple(other.getCenterShadow());
this.radius = other.getRadius();
this.color.setNTuple(other.getColor());
}
public void setCenter(nTuple other) {
this.center.setNTuple(other);
}
public void setCenter(float newX, float newY, float newZ) {
this.center.setNTuple(newX, newY, newZ);
}
public void setColor(nTuple newColor) {
this.color.setNTuple(newColor);
}
public Color shadeSphere(nTuple point, nTuple light, boolean inShadow) {
return lambertian(point, light, inShadow);
}
public Color lambertian(nTuple point, nTuple light, boolean inShadow) {
float cosVectors = ambFactor;
if (!inShadow) {
nTuple n = point.subtract(this.center).normalize(); // surface normal
cosVectors = n.dot(light);
if (cosVectors < ambFactor) {
cosVectors = ambFactor;
}
}
return new Color(cosVectors * this.color.getX(), cosVectors * this.color.getY(), cosVectors * this.color.getZ());
}
}