Vector2D

Struct Vector2D 

Source
pub struct Vector2D {
    pub x: f64,
    pub y: f64,
}
Expand description

定义二维向量结构体。

该结构体包含两个分量,分别表示向量的 x 和 y 坐标。

§字段

  • x:向量的 x 坐标。
  • y:向量的 y 坐标。

§示例

use rs_math::vector::vector_2d::Vector2D;

let v = Vector2D { x: 1.0, y: 2.0 };

Fields§

§x: f64§y: f64

Implementations§

Source§

impl Vector2D

Source

pub fn new(x: f64, y: f64) -> Vector2D

创建新的二维向量。

该函数创建一个新的二维向量,其 x 坐标和 y 坐标分别为 xy

§参数
  • x:向量的 x 坐标。
  • y:向量的 y 坐标。
§返回值

返回新的二维向量。

§示例
use rs_math::vector::vector_2d::Vector2D;

let v = Vector2D::new(1.0, 2.0);
Source

pub fn magnitude(&self) -> f64

计算二维向量的模(长度)。

该函数使用欧几里得范数计算二维向量的模(长度)。

§参数
  • self:二维向量。
§返回值

返回二维向量的模(长度)。

§示例
use rs_math::vector::vector_2d::Vector2D;

let v = Vector2D::new(1.0, 2.0);
Source

pub fn add(self, other: Vector2D) -> Vector2D

计算两个二维向量的和,返回一个新的向量。

该函数使用算术加法运算计算两个二维向量的和。

§参数
  • self:调用该方法的向量。
  • other:要与 self 相加的另一个向量。
§返回值

返回一个新的向量,表示 selfother 的和。

§示例
use rs_math::vector::vector_2d::Vector2D;

let v1 = Vector2D::new(1.0, 2.0);
let v2 = Vector2D::new(3.0, 4.0);

let v3 = v1.add(v2);
Source

pub fn subtract(self, other: Vector2D) -> Vector2D

向量减法。

§参数
  • other:要减去的向量。
§返回值

返回新的向量,表示两个向量的差。

§示例
use rs_math::vector::vector_2d::Vector2D;

let vector1 = Vector2D { x: 3.0, y: 2.0 };
let vector2 = Vector2D { x: 1.0, y: 4.0 };
let result = vector1.subtract(vector2);
Source

pub fn dot_product(self, other: Vector2D) -> f64

向量点积。

§参数
  • other:要进行点积计算的向量。
§返回值

返回两个向量的点积结果。

§示例
use rs_math::vector::vector_2d::Vector2D;

let vector1 = Vector2D { x: 3.0, y: 2.0 };
let vector2 = Vector2D { x: 1.0, y: 4.0 };
let result = vector1.dot_product(vector2);
Source

pub fn cross_product(self, other: Vector2D) -> f64

向量叉积。

§参数
  • other:要进行叉积计算的向量。
§返回值

返回两个向量的叉积结果。

§示例
use rs_math::vector::vector_2d::Vector2D;

let vector1 = Vector2D { x: 3.0, y: 2.0 };
let vector2 = Vector2D { x: 1.0, y: 4.0 };
let result = vector1.cross_product(vector2);

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.