Minor fix
This commit is contained in:
parent
00a1fb16bd
commit
0f4840a09b
3 changed files with 13 additions and 9 deletions
|
|
@ -57,7 +57,7 @@ impl Image {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_pixel(&mut self, x: i32, y: i32) -> Option<&mut Pixel> {
|
pub fn get_pixel(&mut self, x: i32, y: i32) -> Option<&mut Pixel> {
|
||||||
if x >= self.width || y >= self.height {
|
if x >= self.width || y >= self.height || x < 0 || y < 0{
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -60,9 +60,9 @@ impl Hittable for Sphere {
|
||||||
|
|
||||||
let sqrtd = discriminant.sqrt();
|
let sqrtd = discriminant.sqrt();
|
||||||
let mut root = (h - sqrtd) / a;
|
let mut root = (h - sqrtd) / a;
|
||||||
if !interval.surounds(root) {
|
if !interval.surrounds(root) {
|
||||||
root = (h + sqrtd) / a;
|
root = (h + sqrtd) / a;
|
||||||
if !interval.surounds(root) {
|
if !interval.surrounds(root) {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
16
src/vec3.rs
16
src/vec3.rs
|
|
@ -41,7 +41,7 @@ impl Interval {
|
||||||
self.min <= x && x <= self.max
|
self.min <= x && x <= self.max
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn surounds(self, x:f64) -> bool {
|
pub fn surrounds(self, x:f64) -> bool {
|
||||||
self.min < x && x < self.max
|
self.min < x && x < self.max
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -136,21 +136,21 @@ impl Vec3 {
|
||||||
self.x * other.x + self.y * other.y + self.z * other.z
|
self.x * other.x + self.y * other.y + self.z * other.z
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn x(self) -> f64 {
|
pub fn x(&self) -> f64 {
|
||||||
self.x
|
self.x
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn y(self) -> f64 {
|
pub fn y(&self) -> f64 {
|
||||||
self.y
|
self.y
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn z(self) -> f64 {
|
pub fn z(&self) -> f64 {
|
||||||
self.z
|
self.z
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn cross(self, other: Vec3) -> Vec3 {
|
pub fn cross(self, other: Vec3) -> Vec3 {
|
||||||
let x = self.y * other.z - self.z * other.y;
|
let x = self.y * other.z - self.z * other.y;
|
||||||
let y = self.y * other.x - self.x * other.y;
|
let y = self.z * other.x - self.x * other.z;
|
||||||
let z = self.x * other.y - self.y * other.x;
|
let z = self.x * other.y - self.y * other.x;
|
||||||
Vec3 { x, y, z }
|
Vec3 { x, y, z }
|
||||||
}
|
}
|
||||||
|
|
@ -164,6 +164,10 @@ impl Vec3 {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn unit(self) -> Vec3 {
|
pub fn unit(self) -> Vec3 {
|
||||||
self / self.length()
|
if self.length() == 0.0{
|
||||||
|
self
|
||||||
|
} else {
|
||||||
|
self / self.length()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue