Skip to content

Commit a776b4c

Browse files
committed
☕ ray class
1 parent 32f6cfa commit a776b4c

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

src/main.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,34 @@
11
extern crate vec3D;
22

3+
mod ray;
4+
35
use vec3D::Vec3D;
6+
use ray::Ray;
7+
8+
fn color(ray: Ray) -> Vec3D {
9+
let unit_direction = ray.direction.unit();
10+
let t = 0.5 * (unit_direction.y + 1.0);
11+
(Vec3D::new(1.0, 1.0, 1.0) * (1.0 - t)) + (Vec3D::new(0.5, 0.7, 1.0) * t)
12+
}
413

514
fn main() {
6-
let max_i = 200;
7-
let max_j = 200;
15+
let nx = 200;
16+
let ny = 100;
17+
18+
println!("P3\n{} {} 255", nx, ny);
819

9-
println!("P3\n{} {} 255", max_i, max_j);
10-
for i in 0..max_i {
11-
for j in 0..max_j {
20+
let lower_left_corner = Vec3D::new(-2.0, -1.0, -1.0);
21+
let horizontal = Vec3D::new(4.0, 0.0, 0.0);
22+
let vertical = Vec3D::new(0.0, 2.0, 0.0);
23+
let origin = Vec3D::new(0.0, 0.0, 0.0);
1224

13-
let col = Vec3D::new(i as f64 / max_i as f64, j as f64 / max_j as f64, 0.2);
25+
for j in (0..ny).rev() {
26+
for i in 0..nx {
27+
let u = i as f64 / nx as f64;
28+
let v = j as f64 / ny as f64;
29+
let r = Ray::new(origin, lower_left_corner + (horizontal * u) + (vertical * v));
30+
let col = color(r);
31+
// println!("{}, t", col);
1432
let r = (255.00 * col.x).round();
1533
let g = (255.00 * col.y).round();
1634
let b = (255.00 * col.z).round();

src/ray.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use vec3D::Vec3D;
2+
3+
pub struct Ray {
4+
pub origin: Vec3D,
5+
pub direction: Vec3D,
6+
}
7+
8+
impl Ray {
9+
pub fn new(origin: Vec3D, direction: Vec3D) -> Ray {
10+
Ray {
11+
origin,
12+
direction,
13+
}
14+
}
15+
16+
pub fn point_at_parameter(self, t: f64) -> Vec3D {
17+
self.origin + self.direction * t
18+
}
19+
}

0 commit comments

Comments
 (0)