Repository navigation
Expand file tree
/
Copy pathSphere.java
More file actions
73 lines (60 loc) · 1.77 KB
/
Copy pathSphere.java
File metadata and controls
73 lines (60 loc) · 1.77 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
public class Sphere {
private double rad;
private Point center;
private Color color;
private boolean isReflective;
private boolean isSpecular;
private Vector north;
private Vector equator;
private Mapping texture;
public Sphere(double rad, Point center, Color color, boolean isReflective, boolean isSpecular, String texture) {
this.rad = rad;
this.center = center;
this.color = color;
this.isReflective = isReflective;
this.isSpecular = isSpecular;
this.north = (new Vector(center.getX(), center.getY() - 1 * rad, center.getZ())).norm();
this.equator = (new Vector(center.getX() + rad, center.getY(), center.getZ())).norm();
if (texture != null)
this.texture = new Mapping(texture);
else
texture = null;
}
public boolean isTextured() {
return this.texture != null;
}
public Mapping getMapping() {
return this.texture;
}
public Color getColor() {
return this.color;
}
public Vector getNorth() {
return this.north;
}
public Vector getEquator() {
return this.equator;
}
public Point getCenter() {
return this.center;
}
public boolean isReflective() {
return this.isReflective;
}
public boolean isSpecular() {
return this.isSpecular;
}
public double intersects(Ray ray) {
Vector to_ray = ray.getPoint().sub(this.center);
double a = ray.getDir().dot(ray.getDir());
double b = 2 * ray.getDir().dot(to_ray);
double c = to_ray.dot(to_ray) - Math.pow(this.rad, 2);
double discriminant = b * b - 4 * a * c;
if (discriminant >= 0) { // Intersected once (tangent ray) or twice an object, return the closest distance
double dist = (-1 * b - Math.sqrt(discriminant)) / (2 * a);
if (dist > 0)
return dist;
}
return -1;
}
}