-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSphere.java
More file actions
89 lines (75 loc) · 2.26 KB
/
Copy pathSphere.java
File metadata and controls
89 lines (75 loc) · 2.26 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
package geometries;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import primitives.*;
public class Sphere extends RadialGeometry {
private Point_3D _center;
// ***************** Constructors ********************** //
/**
* construct Sphere with point and radius
*
* @param radius
* - double
* @param center
* - Point_3D
*/
public Sphere(double radius, Point_3D center, Color color, Material material) {
super(radius, color, material);
_center = center;
}
// ***************** Getters/Setters ********************** //
public Point_3D center() {
return _center;
}
public double radius() {
return _radius;
}
public Color emmission() {
return _emmission;
}
public Material Material() {
return _material;
}
// ***************** Operations ******************** //
@Override
public Vector getNormal(Point_3D other) {
return _center.subtract(other).normalize();
}
@SuppressWarnings("unlikely-arg-type")
@Override
public Map<Geometry, List<Point_3D>> findIntersections(Ray ray) {
List<Point_3D> intersectionsList = new ArrayList<Point_3D>();
Map<Geometry, List<Point_3D>> intersections = new HashMap<Geometry, List<Point_3D>>();
Point_3D rayP0 = ray.p0();
Vector rayDir = ray.direction();
Vector u;
try {
u = _center.subtract(rayP0);
} catch (IllegalArgumentException e) {
intersectionsList.add(rayP0.add(rayDir.scale(_radius)));
intersections.put(this, intersectionsList);
return intersections;
}
double tm = u.dotProduct(rayDir);
double d = Math.sqrt(u.dotProduct(u) - tm * tm);
if (d > _radius)
return intersections;
double th = Math.sqrt(_radius * _radius - d * d);
if (Coordinate.ZERO.equals(th)) {
if (tm > 0)
intersectionsList.add(rayP0.add(rayDir.scale(tm)));
} else {
double t1 = tm - th;
if (t1 > 0 && !Coordinate.ZERO.equals(t1))
intersectionsList.add(rayP0.add(rayDir.scale(t1)));
double t2 = tm + th;
if (t2 > 0 && !Coordinate.ZERO.equals(t2))
intersectionsList.add(rayP0.add(rayDir.scale(t2)));
}
if (!intersectionsList.isEmpty())
intersections.put(this, intersectionsList);
return intersections;
}
}