skela
New Member
Posts: 6
|
Post by skela on Mar 25, 2020 17:33:27 GMT
i am having a hard time implementing chapter 5 intersection in c language i dont know which is better malloc an array or chained list ?
|
|
|
Post by sdiego on Mar 25, 2020 20:42:40 GMT
I did through arrays
t_x_t intersect_sp(void *v_s, t_ray r, t_x_t x, int obj_n)
{
double a;
double b;
double c;
t_vec sp_to_ray;
double disc;
t_ray ray2;
t_sp *s;
t_t_o temp;
s = (t_sp*)v_s;
if (matrix_inverse_test(s->transform) == 1)
{
ray2 = transform(r, matrix_inverse(s->transform));
sp_to_ray = sub(ray2.o, s->c);
a = dot(ray2.d, ray2.d);
b = 2 * dot(ray2.d, sp_to_ray);
c = dot(sp_to_ray, sp_to_ray) - 1;
disc = (b * b) - 4 * a * c;
if (disc < 0)
{
//x.max_obj = x.max_obj + 2;
return (x);
}
else
{
x.t[x.max_obj].t = ((-b - sqrt(disc)) / (2 * a));
x.t[x.max_obj].obj = obj_n;
x.t[x.max_obj].count = 2;
x.max_obj = x.max_obj + 1;
x.t[x.max_obj].t = ((-b + sqrt(disc)) / (2 * a));
x.t[x.max_obj].obj = obj_n;
x.t[x.max_obj].count = 2;
x.max_obj = x.max_obj + 1;
return (x);
}
}
printf("matrix_inverse_test error in intersect_sp\n");
return(x);
}
|
|
fremag
Junior Member
Posts: 73
|
Post by fremag on Mar 26, 2020 6:55:53 GMT
When looking for intersections, you don't know how many hits you will get. An array has a length you must know when you allocate it. Maybe a linked list is not a bad idea.
|
|
skela
New Member
Posts: 6
|
Post by skela on Mar 27, 2020 16:38:45 GMT
thanks i could do it with a linked list it is working pretty well
|
|