-
Notifications
You must be signed in to change notification settings - Fork 0
/
Intersection.cpp
60 lines (50 loc) · 1.28 KB
/
Intersection.cpp
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
#include "Intersection.hpp"
#include <cstdarg>
#include <limits>
Intersection::Intersection(double t, std::shared_ptr<Shape> s)
{
this->t = t;
this->object = s;
}
Intersection::~Intersection()
{
}
double Intersection::T()
{
return t;
}
std::shared_ptr<Shape> Intersection::Object()
{
return object;
}
std::vector<std::shared_ptr<Intersection>> Intersection::intersections(int n, Intersection* col,...)
{
va_list va;
std::vector<std::shared_ptr<Intersection>> times;
va_start(va, col);
times.push_back(std::make_shared<Intersection>(col->T(), col->Object()));
for(int i=1; i<n; i++)
{
auto p = va_arg(va, Intersection*);
auto intersect = std::make_shared<Intersection>(p->T(), p->Object());
times.push_back(intersect);
}
return times;
}
std::shared_ptr<Intersection> Intersection::hit(const std::vector<std::shared_ptr<Intersection>>& xs)
{
double min = std::numeric_limits<double>::max();
std::shared_ptr<Intersection> minIntersect = NULL;
for(auto& i : xs)
{
if(i->T() < 0) continue;
if(i->T() > min) continue;
min = i->T();
minIntersect = i;
}
return minIntersect;
}
bool Intersection::operator==(const Intersection& i)const
{
return i.object == object && i.t == t;
}