first gradient
This commit is contained in:
168
src/vec3.rs
Normal file
168
src/vec3.rs
Normal file
@@ -0,0 +1,168 @@
|
||||
use std::{ops::{Add, Sub, AddAssign, SubAssign, Mul, Div, MulAssign, DivAssign}};
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Vec3 {
|
||||
pub elements: [f64; 3],
|
||||
}
|
||||
|
||||
impl Vec3 {
|
||||
pub fn new(x: f64, y: f64, z: f64) -> Self {
|
||||
Vec3 { elements: [x, y, z] }
|
||||
}
|
||||
|
||||
pub fn x(&self) -> f64 {
|
||||
self.elements[0]
|
||||
}
|
||||
|
||||
pub fn y(&self) -> f64 {
|
||||
self.elements[1]
|
||||
}
|
||||
|
||||
pub fn z(&self) -> f64 {
|
||||
self.elements[2]
|
||||
}
|
||||
|
||||
pub fn length(&self) -> f64 {
|
||||
self.length_squared().sqrt()
|
||||
}
|
||||
|
||||
pub fn length_squared(&self) -> f64 {
|
||||
self.dot(self)
|
||||
}
|
||||
|
||||
pub fn dot(&self, rhs: &Self) -> f64 {
|
||||
self.x() * rhs.x()
|
||||
+ self.y() * rhs.y()
|
||||
+ self.z() * rhs.z()
|
||||
}
|
||||
|
||||
pub fn cross(&self, rhs: &Self) -> Self {
|
||||
Self {
|
||||
elements: [
|
||||
self.y() * rhs.z() + self.z() * rhs.y(),
|
||||
self.z() * rhs.x() + self.x() * rhs.z(),
|
||||
self.x() * rhs.y() + self.y() * rhs.x()
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
pub fn unit_vector(self) -> Self {
|
||||
self / self.length()
|
||||
}
|
||||
}
|
||||
|
||||
impl Add for Vec3 {
|
||||
type Output = Self;
|
||||
|
||||
fn add(self, rhs: Self) -> Self {
|
||||
Self {
|
||||
elements: [
|
||||
self.x() + rhs.x(),
|
||||
self.y() + rhs.y(),
|
||||
self.z() + rhs.z()
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Sub for Vec3 {
|
||||
type Output = Self;
|
||||
|
||||
fn sub(self, rhs: Self) -> Self::Output {
|
||||
Self {
|
||||
elements: [
|
||||
self.x() - rhs.x(),
|
||||
self.y() - rhs.y(),
|
||||
self.z() - rhs.z()
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Mul<f64> for Vec3 {
|
||||
type Output = Self;
|
||||
|
||||
fn mul(self, rhs: f64) -> Self::Output {
|
||||
Self {
|
||||
elements: [
|
||||
self.x() * rhs,
|
||||
self.y() * rhs,
|
||||
self.z() * rhs
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Mul<Vec3> for f64 {
|
||||
type Output = Vec3;
|
||||
|
||||
fn mul(self, rhs: Vec3) -> Self::Output {
|
||||
rhs * self
|
||||
}
|
||||
}
|
||||
|
||||
impl Div<f64> for Vec3 {
|
||||
type Output = Self;
|
||||
|
||||
fn div(self, rhs: f64) -> Self::Output {
|
||||
Self {
|
||||
elements: [
|
||||
self.x() / rhs,
|
||||
self.y() / rhs,
|
||||
self.z() / rhs
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl AddAssign for Vec3 {
|
||||
fn add_assign(&mut self, rhs: Self) {
|
||||
*self = Self {
|
||||
elements: [
|
||||
self.x() + rhs.x(),
|
||||
self.y() + rhs.y(),
|
||||
self.z() + rhs.z()
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl SubAssign for Vec3 {
|
||||
fn sub_assign(&mut self, rhs: Self) {
|
||||
*self = Self {
|
||||
elements: [
|
||||
self.x() - rhs.x(),
|
||||
self.y() - rhs.y(),
|
||||
self.z() - rhs.z()
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl MulAssign<f64> for Vec3 {
|
||||
fn mul_assign(&mut self, rhs: f64) {
|
||||
*self = Self {
|
||||
elements: [
|
||||
self.x() * rhs,
|
||||
self.y() * rhs,
|
||||
self.z() * rhs
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl DivAssign<f64> for Vec3 {
|
||||
fn div_assign(&mut self, rhs: f64) {
|
||||
*self = Self {
|
||||
elements: [
|
||||
self.x() / rhs,
|
||||
self.y() / rhs,
|
||||
self.z() / rhs
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub type Color = Vec3;
|
||||
pub type Point3 = Vec3;
|
||||
Reference in New Issue
Block a user