Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![warn(
- warnings,
- future_incompatible,
- nonstandard_style,
- rust_2018_compatibility,
- rust_2018_idioms,
- unused
- )]
- use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
- fn main() {
- let x_size = 200;
- let y_size = 100;
- let max_color_channel_value = 255_f64;
- print!("P3\n{} {}\n255\n", x_size, y_size);
- for y_pixel in (0..y_size).rev() {
- let y_float = f64::from(y_pixel);
- for x_pixel in 0..x_size {
- let x_float = f64::from(x_pixel);
- // The reference on the following line is kind of awkward.
- let color = &Vec3(x_float / f64::from(x_size), y_float / f64::from(y_size), 0.2) * max_color_channel_value;
- println!("{}", color.to_ppm());
- }
- }
- }
- struct Vec3(f64, f64, f64);
- impl Vec3 {
- fn len(&self) -> f64 {
- (self.0 + self.1 + self.2).sqrt()
- }
- fn unit_vector(&self) -> Self {
- self / (self.len())
- }
- fn dot(&self, other: &Self) -> f64 {
- (self.0 * other.0) + (self.1 * other.1) + (self.2 * other.2)
- }
- fn cross(&self, other: &Self) -> Self {
- Vec3(
- (self.1 * other.2) - (self.2 * other.1),
- -((self.0 * other.2) - (self.2 * other.0)),
- (self.0 * other.1) - (self.1 * other.0),
- )
- }
- fn to_ppm(&self) -> String {
- format!("{:.0} {:.0} {:.0}", self.0, self.1, self.2)
- }
- }
- impl Add<&Vec3> for &Vec3 {
- type Output = Vec3;
- fn add(self, other: &Vec3) -> Vec3 {
- Vec3(self.0 + other.0, self.1 + other.1, self.2 + other.2)
- }
- }
- impl AddAssign for Vec3 {
- fn add_assign(&mut self, other: Self) {
- self.0 += other.0;
- self.1 += other.1;
- self.2 += other.2;
- }
- }
- impl Sub<&Vec3> for &Vec3 {
- type Output = Vec3;
- fn sub(self, other: &Vec3) -> Vec3 {
- Vec3(self.0 - other.0, self.1 - other.1, self.2 - other.2)
- }
- }
- impl SubAssign for Vec3 {
- fn sub_assign(&mut self, other: Self) {
- self.0 -= other.0;
- self.1 -= other.1;
- self.2 -= other.2;
- }
- }
- impl Mul<f64> for &Vec3 {
- type Output = Vec3;
- fn mul(self, other: f64) -> Vec3 {
- Vec3(self.0 * other, self.1 * other, self.2 * other)
- }
- }
- impl Mul<&Vec3> for &Vec3 {
- type Output = Vec3;
- fn mul(self, other: &Vec3) -> Vec3 {
- Vec3(self.0 * other.0, self.1 * other.1, self.2 * other.2)
- }
- }
- impl MulAssign for Vec3 {
- fn mul_assign(&mut self, other: Self) {
- self.0 *= other.0;
- self.1 *= other.1;
- self.2 *= other.2;
- }
- }
- impl Div<f64> for &Vec3 {
- type Output = Vec3;
- fn div(self, other: f64) -> Vec3 {
- Vec3(self.0 / other, self.1 / other, self.2 / other)
- }
- }
- impl Div<&Vec3> for &Vec3 {
- type Output = Vec3;
- fn div(self, other: &Vec3) -> Vec3 {
- Vec3(self.0 / other.0, self.1 / other.1, self.2 / other.2)
- }
- }
- impl DivAssign for Vec3 {
- fn div_assign(&mut self, other: Self) {
- self.0 /= other.0;
- self.1 /= other.1;
- self.2 /= other.2;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement