-
Notifications
You must be signed in to change notification settings - Fork 1
/
sphere_intersection.c
56 lines (50 loc) · 1.7 KB
/
sphere_intersection.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sphere_intersection.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mghalmi <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/28 11:40:38 by mghalmi #+# #+# */
/* Updated: 2024/02/08 13:41:40 by mghalmi ### ########.fr */
/* */
/* ************************************************************************** */
#include "main.h"
void solve_sphere(double x[2], t_point o, t_point d, t_obj *lst)
{
double disc;
t_point oc;
double k[3];
oc = vsubstr(o, lst->fig.sp.c);
k[0] = dot(d, d);
k[1] = 2 * dot(d, oc);
k[2] = dot(oc, oc) - lst->fig.sp.r * lst->fig.sp.r;
disc = k[1] * k[1] - (4 * k[0] * k[2]);
if (disc < 0)
{
x[0] = INFINITY;
x[1] = INFINITY;
return ;
}
x[0] = (-k[1] + sqrt(disc)) / (2 * k[0]);
x[1] = (-k[1] - sqrt(disc)) / (2 * k[0]);
}
double return_x(double x[2])
{
if (x[0] < x[1])
return (x[0]);
return (x[1]);
}
double sphere_intersection(t_point o, t_point d, t_obj *lst)
{
double closest;
double x[2];
closest = INFINITY;
solve_sphere(x, o, d, lst);
if (x[0] > EPSILON && x[0] < INFINITY)
closest = x[0];
if (x[1] > EPSILON && x[1] < INFINITY)
if (x[1] < x[0])
closest = x[1];
return (closest);
}