1个不稳定版本
0.1.0 | 2024年4月3日 |
---|
#1150 in 数学
22KB
768 行
简单三维向量
Rust中的简单基于网格的三维向量。
还提供了用于处理simple_2d_vector
的先进操作。
入门指南
要开始使用simple_3d_vector
,请使用cargo add simple_3d_vector
将其添加到您的项目中。
然后,您可以通过使用提供的Vector3D
结构体来使用它。
示例
使用Vector3D::new()
创建一个Vector3D
并与一个Vector3D::null()
进行比较
use vector3d::{Vector3D, Point3D};
fn main() {
let vector = Vector3D::new(
Point3D::zero(), // The origin of the vector
Poine3D::zero() // The target of the vector
);
// Null vectors are vectors with a length of zero
// They are also called zero-length vectors as they only have an origin
let null_vector = Vector3D::null(Point3D::new(0.0, 0.0, 0.0)); // A null vector
assert_eq!(vector, null_vector); // The two vectors are the same
}
使用向量执行加法和减法
use vector3d::{Vector3D, Point3D};
fn main() {
let vector1 = Vector3D::new(
Point3D::new(
10.0,
10.0,
10.0
),
Point3D::new(
10.0,
5.0,
5.0
)
);
let vector2 = Vector3D::new(
Point3D::new(
10.0,
10.0,
10.0
),
Point3D::new(
5.0,
10.0,
5.0
)
);
let result_vector_addition = Vector3D::new(
Point3D::new(
10.0,
10.0,
10.0
),
Point3D::new(
15.0,
15.0,
10.0
)
);
let result_vector_subtraction = Vector3D::new(
Point3D::new(
10.0,
10.0,
10.0
),
Point3D::new(
5.0,
-5.0,
0.0
)
);
assert_eq!(vector1 + vector2, result_vector_addition);
assert_eq!(vector1 - vector2, result_vector_subtraction);
}
平移向量
use vector3d::{Vector3D, Point3D};
fn main() {
let vector = Vector3D::new(
Point3D::new(
10.0,
10.0,
10.0
),
Point3D::new(
10.0,
5.0,
5.0
)
);
// `Vector3D.shift` automatically converts applicable types into f64
let shift = (-2, 1.25, 2_u16); // This allows for a mismatch of types
// Shifting a vector moves only its `origin`,
// as it's `target` is relative to its `origin`
let result_vector = Vector3D::new(
Point3D::new(
8.0,
11.25,
12.0
),
Point3D::new(
10.0,
5.0,
5.0
)
);
assert_eq!(vector.shift(shift), result_vector);
}
依赖关系
~18KB