/* Created by Greydon Foil 5.21.04 vectorMath.c This file contains the methods to do basic vector manipulation including calculating the difference between two points, finding the cross product, calculating the normal, and calculating the unit vector of a given input normal. NOTE: I originally used pointers and references to pass variables between methods but i was having problems somewhere along the way. Since my project was not based around raw speed, I just wrote these methods the way I'm used to. */ #include "vectorMath.h" #include #include /* This method calculates the cross product between points p1 and p2 and returns point result */ point crossProduct(point p1, point p2, point result){ result.x = (p1.y * p2.z) - (p1.z * p2.y); result.y = (p1.z * p2.x) - (p1.x * p2.z); result.z = (p1.x * p2.y) - (p1.y * p2.x); return result; } /* crossProduct */ /* This method calculates the difference between two points p1 and p2 and calculates the result */ point pointDiff(point p1, point p2, point result) { result.x = p1.x - p2.x; result.y = p1.y - p2.y; result.z = p1.z - p2.z; return result; } /* pointDiff */ /* This method calculates the normal vector of point center with regards to points left and right */ point calcNormal(point center, point left, point right) { point temp1, temp2, normal; temp1 = pointDiff(left, center, temp1); temp2 = pointDiff(right, center, temp2); crossProduct(temp1, temp2, normal); normal = normalizeVect(normal); return normal; } /* calcNormal */ /* This method takes an input vector and finds its length, using that to calculate its unit vector */ point normalizeVect(point normal) { float length = sqrt(normal.x * normal.x + normal.y * normal.y + normal.z * normal.z); /* This has never happened so far, but just in case... */ if (length == 0) { printf("Length of one of the normal vectors is 0!!!\n"); fflush(stdout); length = 0.0001; } normal.x = normal.x/length; normal.y = normal.y/length; normal.z = normal.z/length; return normal; } /* normalizeVect */ /* This method just prints out the information stored in the input point a */ void printPoint(point a) { printf("x: %f, y: %f, z: %f\n", a.x, a.y, a.z); } /* printPoint */