-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvec3.cpp
More file actions
105 lines (85 loc) · 1.5 KB
/
Copy pathvec3.cpp
File metadata and controls
105 lines (85 loc) · 1.5 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "vec3.hpp"
#include <cmath>
#include <cassert>
#include <iostream>
vec3::vec3()
:x(0.0f),y(0.0f),z(0.0f)
{}
vec3::vec3(float x_param,float y_param,float z_param)
:x(x_param),y(y_param),z(z_param)
{}
vec3& vec3::operator+=(const vec3& v)
{
x+=v.x;
y+=v.y;
z+=v.z;
return *this;
}
vec3& vec3::operator-=(const vec3& v)
{
x-=v.x;
y-=v.y;
z-=v.z;
return *this;
}
vec3& vec3::operator*=(float s)
{
x*=s; y*=s; z*=s;
return *this;
}
vec3& vec3::operator/=(float s)
{
assert(fabs(s)>10e-6);
x/=s; y/=s; z/=s;
return *this;
}
void print_screen(const vec3& v)
{
std::cout<<v.x<<" "<<v.y<<" "<<v.z<<std::endl;
}
float norm(const vec3& v)
{
return std::sqrt(v.x*v.x+v.y*v.y+v.z*v.z);
}
float dot(const vec3& v0,const vec3& v1)
{
return v0.x*v1.x+v0.y*v1.y+v0.z*v1.z;
}
vec3 cross(const vec3& v0,const vec3& v1)
{
return vec3(v0.y*v1.z-v0.z*v1.y,
v0.z*v1.x-v0.x*v1.z,
v0.x*v1.y-v0.y*v1.x);
}
vec3 normalize(const vec3& v)
{
vec3 temp=v;
temp/=norm(v);
return temp;
}
vec3 operator+(const vec3& v0,const vec3& v1)
{
vec3 temp=v0;
temp+=v1;
return temp;
}
vec3 operator-(const vec3& v0,const vec3& v1)
{
vec3 temp=v0;
temp-=v1;
return temp;
}
vec3 operator*(const vec3& v0,float s)
{
vec3 temp=v0;
temp*=s;
return temp;
}
vec3 operator*(float s,const vec3& v0)
{
return v0*s;
}
vec3 operator/(const vec3& v0,float s)
{
vec3 temp=v0;temp/=s;return temp;
}