Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. #![warn(
  2. warnings,
  3. future_incompatible,
  4. nonstandard_style,
  5. rust_2018_compatibility,
  6. rust_2018_idioms,
  7. unused
  8. )]
  9.  
  10. use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
  11.  
  12. fn main() {
  13. let x_size = 200;
  14. let y_size = 100;
  15. let max_color_channel_value = 255_f64;
  16. print!("P3\n{} {}\n255\n", x_size, y_size);
  17. for y_pixel in (0..y_size).rev() {
  18. let y_float = f64::from(y_pixel);
  19. for x_pixel in 0..x_size {
  20. let x_float = f64::from(x_pixel);
  21. // The reference on the following line is kind of awkward.
  22. let color = &Vec3(x_float / f64::from(x_size), y_float / f64::from(y_size), 0.2) * max_color_channel_value;
  23. println!("{}", color.to_ppm());
  24. }
  25. }
  26. }
  27.  
  28. struct Vec3(f64, f64, f64);
  29.  
  30. impl Vec3 {
  31. fn len(&self) -> f64 {
  32. (self.0 + self.1 + self.2).sqrt()
  33. }
  34.  
  35. fn unit_vector(&self) -> Self {
  36. self / (self.len())
  37. }
  38.  
  39. fn dot(&self, other: &Self) -> f64 {
  40. (self.0 * other.0) + (self.1 * other.1) + (self.2 * other.2)
  41. }
  42.  
  43. fn cross(&self, other: &Self) -> Self {
  44. Vec3(
  45. (self.1 * other.2) - (self.2 * other.1),
  46. -((self.0 * other.2) - (self.2 * other.0)),
  47. (self.0 * other.1) - (self.1 * other.0),
  48. )
  49. }
  50.  
  51. fn to_ppm(&self) -> String {
  52. format!("{:.0} {:.0} {:.0}", self.0, self.1, self.2)
  53. }
  54. }
  55.  
  56. impl Add<&Vec3> for &Vec3 {
  57. type Output = Vec3;
  58.  
  59. fn add(self, other: &Vec3) -> Vec3 {
  60. Vec3(self.0 + other.0, self.1 + other.1, self.2 + other.2)
  61. }
  62. }
  63.  
  64. impl AddAssign for Vec3 {
  65. fn add_assign(&mut self, other: Self) {
  66. self.0 += other.0;
  67. self.1 += other.1;
  68. self.2 += other.2;
  69. }
  70. }
  71.  
  72. impl Sub<&Vec3> for &Vec3 {
  73. type Output = Vec3;
  74.  
  75. fn sub(self, other: &Vec3) -> Vec3 {
  76. Vec3(self.0 - other.0, self.1 - other.1, self.2 - other.2)
  77. }
  78. }
  79.  
  80. impl SubAssign for Vec3 {
  81. fn sub_assign(&mut self, other: Self) {
  82. self.0 -= other.0;
  83. self.1 -= other.1;
  84. self.2 -= other.2;
  85. }
  86. }
  87.  
  88. impl Mul<f64> for &Vec3 {
  89. type Output = Vec3;
  90.  
  91. fn mul(self, other: f64) -> Vec3 {
  92. Vec3(self.0 * other, self.1 * other, self.2 * other)
  93. }
  94. }
  95.  
  96. impl Mul<&Vec3> for &Vec3 {
  97. type Output = Vec3;
  98.  
  99. fn mul(self, other: &Vec3) -> Vec3 {
  100. Vec3(self.0 * other.0, self.1 * other.1, self.2 * other.2)
  101. }
  102. }
  103.  
  104. impl MulAssign for Vec3 {
  105. fn mul_assign(&mut self, other: Self) {
  106. self.0 *= other.0;
  107. self.1 *= other.1;
  108. self.2 *= other.2;
  109. }
  110. }
  111.  
  112. impl Div<f64> for &Vec3 {
  113. type Output = Vec3;
  114.  
  115. fn div(self, other: f64) -> Vec3 {
  116. Vec3(self.0 / other, self.1 / other, self.2 / other)
  117. }
  118. }
  119.  
  120. impl Div<&Vec3> for &Vec3 {
  121. type Output = Vec3;
  122.  
  123. fn div(self, other: &Vec3) -> Vec3 {
  124. Vec3(self.0 / other.0, self.1 / other.1, self.2 / other.2)
  125. }
  126. }
  127.  
  128. impl DivAssign for Vec3 {
  129. fn div_assign(&mut self, other: Self) {
  130. self.0 /= other.0;
  131. self.1 /= other.1;
  132. self.2 /= other.2;
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement