Skip to content

Commit

Permalink
vec3::new => vec3::default and vec3::from => vec3::new
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy5909 committed Apr 30, 2024
1 parent 0d57312 commit 2bb6c47
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 28 deletions.
16 changes: 8 additions & 8 deletions src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl Camera {
for j in 0..=self.image_height {
eprint!("\rScanlines remaining: {} ", self.image_height-j);
for i in 0..self.image_width {
let mut pixel_color = Color::new();
let mut pixel_color = Color::default();
for _ in 0..self.samples_per_pixel {
let r = self.get_ray(i, j);
pixel_color += Self::ray_color(&r, self.max_depth, world);
Expand All @@ -46,23 +46,23 @@ impl Camera {

self.pixel_samples_scale = 1.0 / self.samples_per_pixel as f64;

self.center = Point3::from(0.0, 0.0, 0.0);
self.center = Point3::new(0.0, 0.0, 0.0);
let focal_length = 1.0;
let viewport_height = 2.0;
let viewport_width = viewport_height * (self.image_width as f64/self.image_height as f64);

let viewport_u = Vec3::from(viewport_width, 0.0, 0.0);
let viewport_v = Vec3::from(0.0, -viewport_height, 0.0);
let viewport_u = Vec3::new(viewport_width, 0.0, 0.0);
let viewport_v = Vec3::new(0.0, -viewport_height, 0.0);

self.pixel_delta_u = viewport_u / self.image_width as f64;
self.pixel_delta_v = viewport_v / self.image_height as f64;

let viewport_upper_left = self.center - Vec3::from(0.0, 0.0, focal_length) - viewport_u/2.0 - viewport_v/2.0;
let viewport_upper_left = self.center - Vec3::new(0.0, 0.0, focal_length) - viewport_u/2.0 - viewport_v/2.0;
self.pixel00_loc = viewport_upper_left + 0.5 * (self.pixel_delta_u + self.pixel_delta_v);
}
fn ray_color(r: &Ray, depth: i32, world: &dyn Hittable) -> Color {
if depth <= 0 {
return Color::new();
return Color::default();
}
let mut rec = HitRecord::new();
if world.hit(r, Interval::from(0.001, INFINITY), &mut rec) {
Expand All @@ -71,7 +71,7 @@ impl Camera {
}
let unit_direction = unit_vector(r.dir());
let a = 0.5*(unit_direction.y() + 1.0);
(1.0-a)*Color::from(1.0, 1.0, 1.0) + a*Color::from(0.5, 0.7, 1.0)
(1.0-a)*Color::new(1.0, 1.0, 1.0) + a*Color::new(0.5, 0.7, 1.0)
}
fn get_ray(&mut self, i: i32, j: i32) -> Ray {
let offset = Self::sample_square();
Expand All @@ -84,6 +84,6 @@ impl Camera {
}
fn sample_square() -> Vec3 {
let mut rng = thread_rng();
Vec3::from(rng.gen_range(-0.5..0.5), rng.gen_range(-0.5..0.5), 0.0)
Vec3::new(rng.gen_range(-0.5..0.5), rng.gen_range(-0.5..0.5), 0.0)
}
}
4 changes: 2 additions & 2 deletions src/hittable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ pub struct HitRecord {
impl HitRecord {
pub fn new() -> Self {
HitRecord{
p: Point3::from(0.0, 0.0, 0.0),
normal: Vec3::from(0.0, 0.0, 0.0),
p: Point3::new(0.0, 0.0, 0.0),
normal: Vec3::new(0.0, 0.0, 0.0),
t: 0.0,
front_face: false
}
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ mod camera;

fn main() {
let mut world = HittableList::new();
world.add(Box::from(Sphere::new(Point3::from(0.0, 0.0, -1.0), 0.5)));
world.add(Box::from(Sphere::new(Point3::from(0.0, -100.5, -1.0), 100.0)));
world.add(Box::from(Sphere::new(Point3::new(0.0, 0.0, -1.0), 0.5)));
world.add(Box::from(Sphere::new(Point3::new(0.0, -100.5, -1.0), 100.0)));

let mut cam = Camera::new(16.0/9.0, 400);
cam.samples_per_pixel = 100;
Expand Down
2 changes: 1 addition & 1 deletion src/sphere.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{hittable::{HitRecord, Hittable}, interval::Interval, vec3::{dot, Point3, Vec3}};
use crate::{hittable::{HitRecord, Hittable}, interval::Interval, vec3::{dot, Point3}};

pub struct Sphere {
center: Point3,
Expand Down
26 changes: 11 additions & 15 deletions src/vec3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ pub struct Vec3 {
pub e: [f64; 3]
}
impl Vec3 {
pub fn new() -> Self {
Self{e: [0.0,0.0,0.0]}
}

pub fn from(e0: f64, e1: f64, e2: f64) -> Self {
pub fn new(e0: f64, e1: f64, e2: f64) -> Self {
Self{e: [e0, e1, e2]}
}

Expand All @@ -22,18 +18,18 @@ impl Vec3 {
pub fn length(&self) -> f64 {self.length_squared().sqrt()}
pub fn length_squared(&self) -> f64 {self.e[0]*self.e[0] + self.e[1]*self.e[1] + self.e[2]*self.e[2]}

pub fn random() -> Self {Vec3::from(random(), random(), random())}
pub fn random() -> Self {Vec3::new(random(), random(), random())}
pub fn random_range<T: RangeBounds<f64> + Clone + SampleRange<f64>>(range: T) -> Self {
let mut random = thread_rng();
Vec3::from(random.gen_range(range.clone()), random.gen_range(range.clone()), random.gen_range(range.clone()))
Vec3::new(random.gen_range(range.clone()), random.gen_range(range.clone()), random.gen_range(range.clone()))
}
}
pub type Point3 = Vec3;
impl Neg for Vec3 {
type Output = Vec3;

fn neg(self) -> Self::Output {
Vec3::from(-self.e[0],-self.e[1],-self.e[2])
Vec3::new(-self.e[0],-self.e[1],-self.e[2])
}
}
impl Index<usize> for Vec3 {
Expand All @@ -52,14 +48,14 @@ impl Add<Vec3> for Vec3 {
type Output = Vec3;

fn add(self, rhs: Self) -> Self::Output {
Vec3::from(self.e[0] + rhs.e[0], self.e[1] + rhs.e[1], self.e[2] + rhs.e[2])
Vec3::new(self.e[0] + rhs.e[0], self.e[1] + rhs.e[1], self.e[2] + rhs.e[2])
}
}
impl Sub<Vec3> for Vec3 {
type Output = Vec3;

fn sub(self, rhs: Vec3) -> Self::Output {
Vec3::from(self.e[0] - rhs.e[0], self.e[1] - rhs.e[1], self.e[2] - rhs.e[2])
Vec3::new(self.e[0] - rhs.e[0], self.e[1] - rhs.e[1], self.e[2] - rhs.e[2])
}
}
impl AddAssign<Vec3> for Vec3 {
Expand All @@ -73,21 +69,21 @@ impl Mul<Vec3> for Vec3 {
type Output = Vec3;

fn mul(self, rhs: Self) -> Self::Output {
Vec3::from(self.e[0] * rhs.e[0], self.e[1] * rhs.e[1], self.e[2] * rhs.e[2])
Vec3::new(self.e[0] * rhs.e[0], self.e[1] * rhs.e[1], self.e[2] * rhs.e[2])
}
}
impl Mul<f64> for Vec3 {
type Output = Vec3;

fn mul(self, rhs: f64) -> Self::Output {
Vec3::from(self.e[0] * rhs, self.e[1] * rhs, self.e[2] * rhs)
Vec3::new(self.e[0] * rhs, self.e[1] * rhs, self.e[2] * rhs)
}
}
impl Mul<Vec3> for f64 {
type Output = Vec3;

fn mul(self, rhs: Vec3) -> Self::Output {
Vec3::from(self * rhs.e[0], self * rhs.e[1], self * rhs.e[2])
Vec3::new(self * rhs.e[0], self * rhs.e[1], self * rhs.e[2])
}
}
impl MulAssign<Vec3> for Vec3 {
Expand All @@ -108,7 +104,7 @@ impl Div<Vec3> for Vec3 {
type Output = Vec3;

fn div(self, rhs: Self) -> Self::Output {
Vec3::from(self.e[0] + rhs.e[0], self.e[1] + rhs.e[1], self.e[2] + rhs.e[2])
Vec3::new(self.e[0] + rhs.e[0], self.e[1] + rhs.e[1], self.e[2] + rhs.e[2])
}
}
impl Div<f64> for Vec3 {
Expand Down Expand Up @@ -138,7 +134,7 @@ pub fn dot(first: Vec3, other: Vec3) -> f64 {
first.e[2] * other.e[2]
}
pub fn cross(first: Vec3, other: Vec3) -> Vec3 {
Vec3::from(
Vec3::new(
first.e[1]*other.e[2] - first.e[2]*other.e[1],
first.e[2]*other.e[0] - first.e[0]*other.e[2],
first.e[0]*other.e[1] - first.e[1]*other.e[0]
Expand Down

0 comments on commit 2bb6c47

Please sign in to comment.