first diffused sphere
This commit is contained in:
22
src/vec3.rs
22
src/vec3.rs
@@ -1,5 +1,7 @@
|
||||
use std::{ops::{Add, Sub, AddAssign, SubAssign, Mul, Div, MulAssign, DivAssign}};
|
||||
|
||||
use crate::rtweekend::{random_f64, random_range_f64};
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Vec3 {
|
||||
pub elements: [f64; 3],
|
||||
@@ -49,6 +51,14 @@ impl Vec3 {
|
||||
pub fn unit_vector(self) -> Self {
|
||||
self / self.length()
|
||||
}
|
||||
|
||||
pub fn random() -> Self {
|
||||
Vec3::new(random_f64(), random_f64(), random_f64())
|
||||
}
|
||||
|
||||
pub fn random_range(min: f64, max: f64) -> Self {
|
||||
Vec3::new(random_range_f64(min, max), random_range_f64(min, max), random_range_f64(min, max))
|
||||
}
|
||||
}
|
||||
|
||||
impl Add for Vec3 {
|
||||
@@ -165,4 +175,14 @@ impl DivAssign<f64> for Vec3 {
|
||||
}
|
||||
|
||||
pub type Color = Vec3;
|
||||
pub type Point3 = Vec3;
|
||||
pub type Point3 = Vec3;
|
||||
|
||||
pub fn random_in_unit_sphere() -> Vec3 {
|
||||
loop {
|
||||
let p = Vec3::random_range(-1.0, 1.0);
|
||||
if p.length_squared() >= 1.0 {
|
||||
continue;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user