-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloat3.h
More file actions
executable file
·109 lines (88 loc) · 1.72 KB
/
float3.h
File metadata and controls
executable file
·109 lines (88 loc) · 1.72 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
104
105
106
107
108
109
#pragma once
#include <math.h>
#include <stdlib.h>
class float3
{
public:
float x;
float y;
float z;
float3()
{
x = ((float)rand() / RAND_MAX) * 2 - 1;
y = ((float)rand() / RAND_MAX) * 2 - 1;
z = ((float)rand() / RAND_MAX) * 2 - 1;
}
float3(float x, float y, float z):x(x),y(y),z(z){}
float3 operator-() const
{
return float3(-x, -y, -z);
}
float3 operator+(const float3& addOperand) const
{
return float3(x + addOperand.x, y + addOperand.y, z + addOperand.z);
}
float3 operator-(const float3& operand) const
{
return float3(x - operand.x, y - operand.y, z - operand.z);
}
float3 operator*(const float3& operand) const
{
return float3(x * operand.x, y * operand.y, z * operand.z);
}
float3 operator*(float operand) const
{
return float3(x * operand, y * operand, z * operand);
}
void operator-=(const float3& a)
{
x -= a.x;
y -= a.y;
z -= a.z;
}
void operator+=(const float3& a)
{
x += a.x;
y += a.y;
z += a.z;
}
void operator*=(const float3& a)
{
x *= a.x;
y *= a.y;
z *= a.z;
}
void operator*=(float a)
{
x *= a;
y *= a;
z *= a;
}
float norm() const
{
return sqrtf(x*x+y*y+z*z);
}
float norm2() const
{
return x*x+y*y+z*z;
}
float3 normalize()
{
float oneOverLength = 1.0f / norm();
x *= oneOverLength;
y *= oneOverLength;
z *= oneOverLength;
return *this;
}
float3 cross(const float3& operand) const
{
return float3(
y * operand.z - z * operand.y,
z * operand.x - x * operand.z,
x * operand.y - y * operand.x);
}
float dot(const float3& operand) const
{
return x * operand.x + y * operand.y + z * operand.z;
}
};