-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcylinder.c
81 lines (69 loc) · 2.23 KB
/
cylinder.c
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cylinder.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bmikaeli <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2014/03/12 16:47:08 by aelola #+# #+# */
/* Updated: 2014/03/17 11:13:58 by bmikaeli ### ########.fr */
/* */
/* ************************************************************************** */
#include "cylinder.h"
#include <stdio.h>
t_cyl *new_cyl(float x, float y, float z, int rad, t_color *c)
{
t_cyl *new;
new = NEW(t_cyl);
new->pos = new_vect(x, y, z);
new->rad = rad;
new->color = c;
return (new);
}
static float get_min_sol(float b, float calc, float a)
{
float t1;
float t2;
t1 = ((- b + sqrt(calc)) / (2 * a));
t2 = ((- b - sqrt(calc)) / (2 * a));
if (t1 < t2)
return (t1);
else
return (t2);
}
t_vect *ray_cylinder_b(t_vect *pos, t_vect *cam)
{
float a;
t_vect *mult_vector;
a = DOTPROD(cam, pos);
mult_vector = vector_mult_scal(pos, a);
return (vector_sub(cam, mult_vector));
}
t_vect *ray_cylinder_a(t_vect *pos, t_vect *dir)
{
float a;
t_vect *mult_vector;
a = DOTPROD(dir, pos);
mult_vector = vector_mult_scal(pos, a);
return (vector_sub(dir, mult_vector));
}
float intersect_cyl(t_vect *dir, t_vect *cam, t_cyl *cyl)
{
float a;
float b;
float c;
float calc;
t_vect *tab[2];
vector_normalize(cyl->pos);
tab[0] = ray_cylinder_a(cyl->pos, dir);
tab[1] = ray_cylinder_b(cyl->pos, cam);
a = DOTPROD(tab[0], tab[0]);
b = 2 * DOTPROD(tab[0], tab[1]);
c = DOTPROD(tab[1], tab[1]) - pow(cyl->rad, 2);
calc = (pow(b, 2) - (4 * a * c)) / vector_lenght(dir);
c = DOTPROD(tab[1], tab[1]) - pow(cyl->rad, 2);
calc = (pow(b, 2) - (4 * a * c));
if (calc >= 0)
return (get_min_sol(b, calc, a));
return (-1);
}