-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsphere.m
43 lines (38 loc) · 1.34 KB
/
sphere.m
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
classdef sphere
properties
center;
radius;
material;
end
methods
%% Constructor
function obj = sphere(c, r, material)
obj.center = c;
obj.radius = r;
obj.material = material;
end
%% Hit ditection
function [flag, rec] = hit(obj, ray, tmin, tmax)
rec = [];
oc = ray.origin - obj.center;
a = ray.direction'*ray.direction;
b = oc' * ray.direction;
c = oc' * oc - obj.radius * obj.radius;
discriminant = b*b - a*c;
flag = false;
if(discriminant > 0)
t = (-b - sqrt(discriminant))/(a);
if(t < tmax & t > tmin)
rec = hitable(t, ray.point_at(t), (ray.point_at(t) - obj.center)./norm(ray.point_at(t) - obj.center), obj.material);
flag = true;
else
t = (-b + sqrt(discriminant))/(a);
if(t < tmax & t > tmin)
rec = hitable(t, ray.point_at(t), (ray.point_at(t) - obj.center)./norm(ray.point_at(t) - obj.center), obj.material);
flag = true;
end
end
end
end
end
end