summaryrefslogtreecommitdiff
path: root/3dmath.c
diff options
context:
space:
mode:
authorMartin Stensgård <mastensg@ping.uio.no>2010-10-19 00:20:35 +0200
committerMartin Stensgård <mastensg@ping.uio.no>2010-10-19 00:20:35 +0200
commit7e071db6bee659dbd2b4261830f4e2f10bc8ba33 (patch)
treee49bfd229464f378113a2b5653e6e0c04de2826e /3dmath.c
parentdf6a25599abac84f0f736b3029024cdd500bc106 (diff)
Fail.
Diffstat (limited to '3dmath.c')
-rw-r--r--3dmath.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/3dmath.c b/3dmath.c
new file mode 100644
index 0000000..d729323
--- /dev/null
+++ b/3dmath.c
@@ -0,0 +1,36 @@
+#include <math.h>
+
+#define POW2(x) ((x) * (x))
+
+float
+dot(float x[3], float y[3]) {
+ return x[0] * y[0] + x[1] * y[1] + x[2] * y[2];
+}
+
+void
+normalize(float x[3]) {
+ float len;
+ int i;
+
+ len = sqrt(dot(x, x));
+
+ for(i = 0; i < 3; ++i)
+ x[i] /= len;
+}
+
+float
+sphere_intersect(float s[3], float d[3], float c[3], float r) {
+ int i;
+ float D;
+ float v[3];
+
+ for(i = 0; i < 3; ++i)
+ v[i] = s[i] - c[i];
+
+ D = POW2(dot(v, d)) - 4 * (dot(v, v) - POW2(r));
+
+ if(D < 0)
+ return -1;
+
+ return 1;
+}