-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector.cpp
85 lines (70 loc) · 1.38 KB
/
Vector.cpp
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "Vector.hpp"
#include <math.h>
#include <stdio.h>
#include <iostream>
using namespace std;
Vector::Vector(){
x = 0.0;
y = 0.0;
z = 0.0;
}
Vector::Vector(GLfloat dx, GLfloat dy, GLfloat dz){
x = dx;
y = dy;
z = dz;
}
void Vector::set(GLfloat dx, GLfloat dy, GLfloat dz){
x = dx;
y = dy;
z = dz;
}
void Vector::set(Vector& v){
x = v.x;
y = v.y;
z = v.z;
}
void Vector::ctm_multiply() {
GLfloat M[4];
M[0] = x;
M[1] = y;
M[2] = z;
M[3] = 0;
glMultMatrixf(M);
}
Vector Vector::cross(Vector b) //return this cross b
{
Vector c(y*b.z - z*b.y, z*b.x - x*b.z, x*b.y - y*b.x);
return c;
}
GLfloat Vector::dot(Vector b) {
return x * b.x + y * b.y + z * b.z;
}
void Vector::build4tuple(float v[]){
v[0] = x; v[1] = y; v[2] = z; v[3] = 0.0f;
}
void Vector::normalize(){
GLdouble sqs = x * x + y * y + z * z;
if(sqs < 0.0000001)
{
std::cerr << "\nnormalize() sees vector (0,0,0)!";
return; // does nothing to zero vectors;
}
GLfloat scaleFactor = 1.0/(GLfloat)sqrt(sqs);
x *= scaleFactor;
y *= scaleFactor;
z *= scaleFactor;
}
/* setDiff
* set to difference a - b
*/
void Vector::setDiff(Point& a, Point& b){
x = a.x - b.x;
y = a.y - b.y;
z = a.z - b.z;
}
void Vector::flip(){
x = -x; y = -y; z = -z;
}
void Vector::printVector(){
printf("(x:%f, y:%f, z:%f)\n",x,y,z);
}