home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <math.h>
- #include <stdlib.h>
- #include <windows.h>
- #include <applicat.h>
-
- #include "vector.h"
-
- // Magnitude of a vector:
- double Mag(VECTOR& v)
- {
- return sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
- }
-
- // Add two vectors
- VECTOR Add(VECTOR& v1, VECTOR& v2)
- {
- VECTOR res;
-
- res.x = v1.x + v2.x;
- res.y = v1.y + v2.y;
- res.z = v1.z + v2.z;
-
- return res;
- }
-
-
- // Subtract the two vectors
- VECTOR Subtract(VECTOR& v1, VECTOR& v2)
- {
- VECTOR res;
-
- res.x = v1.x - v2.x;
- res.y = v1.y - v2.y;
- res.z = v1.z - v2.z;
-
- return res;
- }
-
- // Cross multiply two vectors
- VECTOR Cross(VECTOR& v1, VECTOR& v2)
- {
- VECTOR res;
-
- res.x = v1.y * v2.z - v2.y * v1.z;
- res.y = v1.z * v2.x - v2.z * v1.x;
- res.z = v1.x * v2.y - v2.x * v1.y;
-
- return res;
- }
-
- // Multiply by constant
- VECTOR Multiply(VECTOR& v, double num)
- {
- VECTOR res;
-
- res.x = v.x * num;
- res.y = v.y * num;
- res.z = v.z * num;
-
- return res;
- }
-
-
- // Divide the vector with a scalar
- VECTOR Divide(VECTOR& v, double num)
- {
- VECTOR res;
-
- if (num == 0) {
- MessageBox(NULL,"Division by zero error in vector.cpp: Divide.","Closedown Query",MB_OK+MB_ICONEXCLAMATION);
- }
-
- res.x = v.x / num;
- res.y = v.y / num;
- res.z = v.z / num;
-
- return res;
- }
-
- // Normalize the vector
- void Normalize(VECTOR& v)
- {
- double len = Mag(v);
-
- if (len != 0) {
- v = Divide(v,len);
- }
- }
-
- void PrintVector(LPSTR text, VECTOR& v)
- {
-
- while (*text) {
- cout << *text;
- text++;
- }
- cout << " (" << v.x << ", " << v.y << ", " << v.z << ")\n";
- }