Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 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. let color = &Vec3(x_float / f64::from(x_size), y_float / f64::from(y_size), 0.2) * max_color_channel_value;
  22. println!("{}", color.to_ppm());
  23. }
  24. }
  25. }
  26.  
  27. struct Vec3(f64, f64, f64);
  28.  
  29. impl Vec3 {
  30. fn len(&self) -> f64 {
  31. (self.0 + self.1 + self.2).sqrt()
  32. }
  33.  
  34. fn unit_vector(&self) -> Self {
  35. self / (self.len())
  36. }
  37.  
  38. fn dot(&self, other: &Self) -> f64 {
  39. (self.0 * other.0) + (self.1 * other.1) + (self.2 * other.2)
  40. }
  41.  
  42. fn cross(&self, other: &Self) -> Self {
  43. Vec3(
  44. (self.1 * other.2) - (self.2 * other.1),
  45. -((self.0 * other.2) - (self.2 * other.0)),
  46. (self.0 * other.1) - (self.1 * other.0),
  47. )
  48. }
  49.  
  50. fn to_ppm(&self) -> String {
  51. format!("{:.0} {:.0} {:.0}", self.0, self.1, self.2)
  52. }
  53. }
  54.  
  55. impl Add<&Vec3> for &Vec3 {
  56. type Output = Vec3;
  57.  
  58. fn add(self, other: &Vec3) -> Vec3 {
  59. Vec3(self.0 + other.0, self.1 + other.1, self.2 + other.2)
  60. }
  61. }
  62.  
  63. impl AddAssign for Vec3 {
  64. fn add_assign(&mut self, other: Self) {
  65. self.0 += other.0;
  66. self.1 += other.1;
  67. self.2 += other.2;
  68. }
  69. }
  70.  
  71. impl Sub<&Vec3> for &Vec3 {
  72. type Output = Vec3;
  73.  
  74. fn sub(self, other: &Vec3) -> Vec3 {
  75. Vec3(self.0 - other.0, self.1 - other.1, self.2 - other.2)
  76. }
  77. }
  78.  
  79. impl SubAssign for Vec3 {
  80. fn sub_assign(&mut self, other: Self) {
  81. self.0 -= other.0;
  82. self.1 -= other.1;
  83. self.2 -= other.2;
  84. }
  85. }
  86.  
  87. impl Mul<f64> for &Vec3 {
  88. type Output = Vec3;
  89.  
  90. fn mul(self, other: f64) -> Vec3 {
  91. Vec3(self.0 * other, self.1 * other, self.2 * other)
  92. }
  93. }
  94.  
  95. impl Mul<&Vec3> for &Vec3 {
  96. type Output = Vec3;
  97.  
  98. fn mul(self, other: &Vec3) -> Vec3 {
  99. Vec3(self.0 * other.0, self.1 * other.1, self.2 * other.2)
  100. }
  101. }
  102.  
  103. impl MulAssign for Vec3 {
  104. fn mul_assign(&mut self, other: Self) {
  105. self.0 *= other.0;
  106. self.1 *= other.1;
  107. self.2 *= other.2;
  108. }
  109. }
  110.  
  111. impl Div<f64> for &Vec3 {
  112. type Output = Vec3;
  113.  
  114. fn div(self, other: f64) -> Vec3 {
  115. Vec3(self.0 / other, self.1 / other, self.2 / other)
  116. }
  117. }
  118.  
  119. impl Div<&Vec3> for &Vec3 {
  120. type Output = Vec3;
  121.  
  122. fn div(self, other: &Vec3) -> Vec3 {
  123. Vec3(self.0 / other.0, self.1 / other.1, self.2 / other.2)
  124. }
  125. }
  126.  
  127. impl DivAssign for Vec3 {
  128. fn div_assign(&mut self, other: Self) {
  129. self.0 /= other.0;
  130. self.1 /= other.1;
  131. self.2 /= other.2;
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement