-
Notifications
You must be signed in to change notification settings - Fork 3
/
Plane.c
34 lines (22 loc) · 831 Bytes
/
Plane.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
#include "Plane.h"
#include "Ray.h"
#include <math.h>
Plane makePlane(const Vector normal, const float distance) {
return (Plane) {normal, distance};
}
Intersection pIntersect(const Plane plane, const Ray ray) {
Intersection intersection;
float normalDotRayDirection = vDot(plane.normal, ray.direction);
if (normalDotRayDirection < 0 && !pIsInside(plane, ray.origin)){
intersection.hitType = surface;
intersection.distance = (plane.distance - vDot(plane.normal, ray.origin)) / normalDotRayDirection;
intersection.position = vAdd(ray.origin, vsMul(ray.direction, intersection.distance));
intersection.normal = plane.normal;
} else {
intersection.hitType = missed;
}
return intersection;
};
bool pIsInside(const Plane plane, const Vector point) {
return vDot(point, plane.normal) < plane.distance;
};