-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvec3.cpp
More file actions
71 lines (59 loc) · 963 Bytes
/
vec3.cpp
File metadata and controls
71 lines (59 loc) · 963 Bytes
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
/*
* vec3.cpp
* proj3
*
* Created by Michael Atwood on 5/30/08.
* Copyright 2008. All rights reserved.
*
*/
#include <iostream>
#include <cmath>
#include "vec3.h"
/**
* Returns the magnitude of the vector
*/
GLdouble vec3::magnitude()
{
return sqrt((x*x)+(y*y)+(z*z));
}
/**
* Normalizes the vector
*/
void vec3::normalize()
{
GLdouble length = magnitude();
if (length == 0)
return;
x /= length;
y /= length;
z /= length;
}
/**
* Cross product operator overloading
*/
const vec3 operator*(const vec3& v1, const vec3& v2)
{
vec3 out;
out.x = v1.y * v2.z - v1.z * v2.y;
out.y = v1.z * v2.x - v1.x * v2.z;
out.z = v1.x * v2.y - v1.y * v2.x;
return out;
}
const vec3 operator+(const vec3& v1, const vec3& v2)
{
vec3 out;
out.x = v1.x + v2.x;
out.y = v1.y + v2.y;
out.z = v1.z + v2.z;
return out;
}
/**
* Debug out
*/
void vec3::toString()
{
std::cout << "x:" << x
<< " y:" << y
<< " z:" << z
<< std::endl;
}