-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircle.h
33 lines (31 loc) · 820 Bytes
/
circle.h
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
#pragma once
#include "Hittable.h"
class circle : public Hittable
{
public:
vec3 center;
vec3 normal;
double radius;
circle(const vec3 center = vec3::zero, double radius = 1, const vec3 normal = vec3::up, shared_ptr<material> mat = nullptr)
: center(center), radius(radius), normal(normal)
{
mat_ptr = mat;
}
bool hit(const ray& r, hit_record& rec) const override
{
auto cosa = r.direction.dot(normal);
if (fabs(cosa) < 1e-6)
return false;
auto root = (center - r.origin).dot(normal) / cosa;
if (root < r.t_min || root > r.t_max)
return false;
auto pos = r.at(root);
if ((pos - center).magnitute2() > square(radius))
return false;
rec.t = root;
rec.pos = pos;
rec.normal = cosa < 0 ? normal : -normal;
rec.mat_ptr = mat_ptr;
return true;
}
};