-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom.cpp
More file actions
34 lines (27 loc) · 799 Bytes
/
random.cpp
File metadata and controls
34 lines (27 loc) · 799 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
#include "common.h"
Vector2f disk_sample() {
Vector2f u(next_float(), next_float());
u = 2.f*u - Vector2f(1, 1);
if (u.x == 0 && u.y == 0)
return Vector2f(0,0);
float theta, r;
if (std::abs(u.x) > std::abs(u.y))
{
r = u.x;
theta = M_PI4 * (u.y/u.x);
} else {
r = u.y;
theta = M_PI2 - M_PI4 * (u.x/u.y);
}
return r*Vector2f(std::cos(theta), std::sin(theta));
}
Vector3f cosine_hemisphere() {
Vector2f d = disk_sample();
float z = std::sqrt(std::max((float)0, 1 - d.x*d.x - d.y*d.y));
return Vector3f(d.x, d.y, z);
}
Vector3f rectangle_sample(Vector3f center, Vector3f vx, Vector3f vy) {
Vector2f u(next_float(), next_float());
u = 2.f*u - Vector2f(1, 1);
return center + u.x*vx + u.y*vy;
}