-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathHelper.cpp
More file actions
executable file
·56 lines (46 loc) · 1.19 KB
/
MathHelper.cpp
File metadata and controls
executable file
·56 lines (46 loc) · 1.19 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
#include "MathHelper.h"
#include <float.h>
#include <cmath>
const float MathHelper::Infinity = FLT_MAX;
const float MathHelper::Pi = 3.1415926535f;
float MathHelper::AngleFromXY(float x, float y)
{
float theta = 0.0f;
if(x >= 0.0f)
{
theta = atanf(y/x);
if(theta < 0.0f)
theta += 2.0f*Pi;
}
else
{
theta = atanf(y/x) + Pi;
}
return theta;
}
XMVECTOR MathHelper::RandUnitVec3()
{
XMVECTOR One = XMVectorSet(1.0f,1.0f,1.0f,1.0f);
XMVECTOR Zero = XMVectorZero();
while (true)
{
XMVECTOR v = XMVectorSet(MathHelper::RandF(-1.0f,1.0f),MathHelper::RandF(-1.0f,1.0f),MathHelper::RandF(-1.0f,1.0f),0.0f);
if(XMVector3Greater(XMVector3LengthSq(v),One))
continue;
return XMVector3Normalize(v);
}
}
XMVECTOR MathHelper::RandHemisphereUnitVec3(XMVECTOR n)
{
XMVECTOR One = XMVectorSet(1.0f,1.0f,1.0f,1.0f);
XMVECTOR Zero = XMVectorZero();
while (true)
{
XMVECTOR v = XMVectorSet(MathHelper::RandF(-1.0f,1.0f),MathHelper::RandF(-1.0f,1.0f),MathHelper::RandF(-1.0f,1.0f),0.0f);
if(XMVector3Greater(XMVector3LengthSq(v),One))
continue;
if(XMVector3Less(XMVector3Dot(n,v),Zero))
continue;
return XMVector3Normalize(v);
}
}