toytracer/vec3.cpp

25 lines
583 B
C++
Raw Normal View History

#include "vec3.h"
Vec3 random_in_unit_sphere() {
while (true) {
auto p = Vec3::random(-1, 1);
if (p.length_squared() >= 1) continue;
return p;
}
}
Vec3 random_unit_vector() {
auto a = random_double(0, 2*pi);
auto z = random_double(-1, 1);
auto r = std::sqrt(1 - z*z);
return Vec3(r*std::cos(a), r*std::sin(a), z);
}
Vec3 random_in_hemisphere(const Vec3& normal) {
Vec3 in_unit_sphere = random_in_unit_sphere();
if (dot(in_unit_sphere, normal) > 0.0)
return in_unit_sphere;
else
return -in_unit_sphere;
}