-
Notifications
You must be signed in to change notification settings - Fork 0
/
ray.cpp
107 lines (94 loc) · 1.97 KB
/
ray.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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "ray.h"
#ifndef INCLUDED_STDLIB
#include <stdlib.h>
#define INCLUDED_STDLIB
#endif
#ifndef INCLUDED_IOSTREAM
#include <iostream>
#define INCLUDED_IOSTREAM
#endif
#define SQ(x) ((x)*(x))
int Ray::cross(OBJ* obj, pointer_t* p){
switch(obj.type){
case SPHERE:
double A = SQ(direction.getScalar());
double B = (start_v - obj.center) * direction;
double C = SQ((obj.center - start_v).getScalar()) - obj.radius;
double t1,t2;
if((SQ(B) - A * C) > 0){
t1 = (-B + sqrt(SQ(B)-AC))/A;
t2 = (-B - sqrt(SQ(B)-AC))/A;
if(t1<t2){
p->x = start_v.x + t1*direction.x;
p->y = start_v.y + t1*direction.y;
p->z = start_v.z + t1*direction.z;
break;
}else{
p->x = start_v.x + t2*direction.x;
p->y = start_v.y + t2*direction.y;
p->z = start_v.z + t2*direction.z;
break;
}
}else if((SQ(B) - A * C) == 0){
t1 = -B / A;
p->x = start_v.x + t1*direction.x;
p->y = start_v.y + t1*direction.y;
p->z = start_v.z + t1*direction.z;
}else return -1;
break;
default:
return -2;
}
return 0;
}
Ray::Ray(int x, int y, int z, Vector p){
start.x = x;
start.y = y;
start.z = z;
direction = p;
pram = 0;
}
Ray::Ray(int scalar, int xyangle, int xzangle, pointer_t s){
start = s;
Vector direction(scalar, xyangle, xzangle);
pram = 0;
}
Ray::Ray(pointer_t s, Vector d){
start = s;
direction = d;
pram = 0;
}
Ray::Ray(){
start.x = 0;
start.y = 0;
start.z = 0;
pram = 0;
}
Ray::Ray(const Ray &obj){
start = obj.start;
start_v = obj.start_v;
direction = obj.direction;
pram = obj.pram;
flag = obj.flag;
}
void Ray::Set(int x, int y, int z, Vector direction){
start.x = x;
start.y = y;
start.z = z;
direction = p;
return;
}
void Ray::Set(int scalar, int xyangle, int xzangle, pointer_t s){
start = s;
Vector direction(scalar, xyangle, xzangle);
pram = 0;
}
int Ray::pass(pointer_t p){
Vector line = (pram * direction);
if(double(p.x/line.x == p.y/line.y) && double(p.x/line.x == p.z/line.z)){
return 1;
}
return 0;
}
Ray::~Ray(){
}