0191-向量的四则运算

环境

  • Time 2022-11-15
  • WSL-Ubuntu 22.04
  • Rust 1.65.0

前言

说明

参考:https://raytracing.github.io/books/RayTracingInOneWeekend.html

目标

定义三维向量的几个基本运算函数。

加法

两个向量相加,结果为对应方向上的数相加。

// 向量的加法
impl Add for Vector3 {
    type Output = Self;

    fn add(self, rhs: Self) -> Self {
        Vector3 {
            x: self.x + rhs.x,
            y: self.y + rhs.y,
            z: self.z + rhs.z,
        }
    }
}

减法

两个向量相减,结果为对应方向上的数相减。

// 向量的减法
impl Sub for Vector3 {
    type Output = Self;

    fn sub(self, rhs: Self) -> Self {
        Self {
            x: self.x - rhs.x,
            y: self.y - rhs.y,
            z: self.z - rhs.z,
        }
    }
}

数乘

一个数乘以向量,对应每个方向上乘以这个数。

// 向量和数字的乘法
impl Mul<f64> for Vector3 {
    type Output = Self;

    fn mul(self, rhs: f64) -> Self {
        Self {
            x: self.x * rhs,
            y: self.y * rhs,
            z: self.z * rhs,
        }
    }
}

// 数字和向量的乘法
impl Mul<Vector3> for f64 {
    type Output = Vector3;

    fn mul(self, rhs: Vector3) -> Vector3 {
        rhs * self
    }
}

点乘

向量的点乘,每个方向上的数相乘然后相加。

/// 向量的点乘
pub fn dot(self, other: Vector3) -> f64 {
    self.x * other.x + self.y * other.y + self.z * other.z
}

长度

/// 向量的长度
pub fn length(self) -> f64 {
    self.dot(self).sqrt()
}

单位向量

///  单位向量
pub fn unit(self) -> Vector3 {
    self / self.length()
}

总结

实现了向量的几种运算规则的函数。

附录

vector3.rs

use std::ops::{Add, Div, Mul, Sub};

pub type Color = Vector3;
pub type Point3 = Vector3;

#[derive(Default, Clone, Copy)]
pub struct Vector3 {
    x: f64,
    y: f64,
    z: f64,
}

impl Vector3 {
    pub fn new(x: f64, y: f64, z: f64) -> Self {
        Self { x, y, z }
    }

    pub fn format_str(self) -> String {
        let x = (255.999 * self.x) as u64;
        let y = (255.999 * self.y) as u64;
        let z = (255.999 * self.z) as u64;

        format!("{x} {y} {z}\n")
    }

    pub fn y(self) -> f64 {
        self.y
    }

    /// 向量的长度
    pub fn length(self) -> f64 {
        self.dot(self).sqrt()
    }

    /// 向量的点乘
    pub fn dot(self, other: Vector3) -> f64 {
        self.x * other.x + self.y * other.y + self.z * other.z
    }

    ///  单位向量
    pub fn unit(self) -> Vector3 {
        self / self.length()
    }
}

// 向量的加法
impl Add for Vector3 {
    type Output = Self;

    fn add(self, rhs: Self) -> Self {
        Vector3 {
            x: self.x + rhs.x,
            y: self.y + rhs.y,
            z: self.z + rhs.z,
        }
    }
}

// 向量的减法
impl Sub for Vector3 {
    type Output = Self;

    fn sub(self, rhs: Self) -> Self {
        Self {
            x: self.x - rhs.x,
            y: self.y - rhs.y,
            z: self.z - rhs.z,
        }
    }
}

// 向量和数字的乘法
impl Mul<f64> for Vector3 {
    type Output = Self;

    fn mul(self, rhs: f64) -> Self {
        Self {
            x: self.x * rhs,
            y: self.y * rhs,
            z: self.z * rhs,
        }
    }
}

// 向量和数字的乘法
impl Mul<Vector3> for f64 {
    type Output = Vector3;

    fn mul(self, rhs: Vector3) -> Vector3 {
        rhs * self
    }
}

// 向量的除法
impl Div<f64> for Vector3 {
    type Output = Self;

    fn div(self, rhs: f64) -> Self {
        Self {
            x: self.x / rhs,
            y: self.y / rhs,
            z: self.z / rhs,
        }
    }
}
posted @   jiangbo4444  阅读(29)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2020-07-17 【JavaScript】对象基础
点击右上角即可分享
微信分享提示