3 个稳定版本
2.2.0 | 2019年3月18日 |
---|---|
2.0.1 | 2019年3月16日 |
1.1.3 | 2019年3月16日 |
#1432 in Rust 模式
2,348 个月下载量
用于 5 crates
25KB
513 行
vector2d
一个简单方便的二维向量类型,不使用过多的外部依赖。如果其他向量库是瑞士军刀,那么 vector2d 就是勺子;安全、直观、方便。此外,由于 Unlicense 的宽松许可,使用此库不会遇到任何法律问题。
使用 vector2d
您可能不需要任何文档就可以使用 Vector2D
类型;像 dot
、length
和 angle
这样的函数可能都足够直观,您可以轻松掌握。如果您对某些功能部分感到困惑,请务必查看 文档,在那里您可以找到示例和所有功能的解释。
lib.rs
:
vector2d
一个简单方便的二维向量库,不使用过多的外部依赖。如果其他向量库是瑞士军刀,那么 vector2d 就是勺子;安全、直观、方便。此外,由于 Unlicense 的宽松许可,使用此库不会遇到任何法律问题。
此库中只有一个类型 Vector2D
,它高度通用;根据其内部组件类型的特性行功能转换。
示例
use vector2d::Vector2D;
fn main() {
// Vectors have fields X and Y, these can be of any type
let v1: Vector2D<i32> = Vector2D { x: 10, y: 5 };
// Alternatively you can use new(..) to condense instantiation
let v2: Vector2D<f64> = Vector2D::new(13.0, 11.5);
// There are two ways to cast between Vector2Ds, depending on the source
// and target types.
//
// If the target type has a implementation of From<SourceType>, then you
// can either use source.into_vec2d() or Vector2D::from_vec2d(source).
assert_eq!(Vector2D::new(10.0, 5.0), v1.into_vec2d());
assert_eq!(Vector2D::new(10.0, 5.0), Vector2D::from_vec2d(v1));
// If there is no From or Into implementation, then you're out of luck
// unless you are using specific primitives, such as i32 and f64. In
// this case you can use specialised functions, as shown below:
assert_eq!(Vector2D::new(13, 11), v2.as_i32s());
// The full list of interoperable primitives is as follows:
// - i32, i64, isize
// - u32, u64, usize
// - f32, f64
// As primitives generally implement From/Into for lossless casts,
// an as_Ts() function is not available for those types, and
// from(..)/into() should be favoured.
//
// Casts between signed and unsigned primitives will perform bounds
// checking, so casting the vector (-10.0, 2.0) to a Vector2D<u32> will
// result in the vector (0, 2).
// For types with an Add and Mul implementation, the functions dot() and
// length_squared() are available. For access to length(), normalise(),
// or angle() however, you must be using either Vector2D<f32> or
// Vector2D<f64>.
let _v1_len_sq = v1.length_squared();
let v2_len = v2.length();
let v2_dir = v2.normalise();
// Assuming the operator traits are implemented for the types involved,
// you can add and subtract Vector2Ds from one-another, as well as
// multiply and divide them with scalar values.
assert_eq!(v2, v2_dir * v2_len);
assert_eq!(Vector2D::new(23.0, 16.5), v2 + v1.into_vec2d()) ;
// If you feel the need to multiply or divide individual components of
// vectors with the same type, you can use mul_components(...) or
// div_components(...) provided that their types can be multiplied or
// divided.
// For any Vector2D<T>, there is an implementation of
// From<(T, T)> and From<[T; 2]>
let v4: Vector2D<f64> = Vector2D::new(1.5, 2.3);
assert_eq!(v4, (1.5, 2.3).into());
assert_eq!(v4, [1.5, 2.3].into());
// Additionally, there is an Into<(T, T)> implementation for any types
// that the vector components have their own Into implementations for
assert_eq!((1.5, 2.3), v4.into());
// If you want the normal of a vector you can just call normal()
let v5 = Vector2D::new(-10.0, -2.3);
assert_eq!(Vector2D::new(2.3, -10.0), v5.normal());
// You can get a vector consisting of only the horizontal or vertical
// component of a vector by calling horizontal() or vertical()
// respectively
let v6 = Vector2D::new(12.3, 83.2);
assert_eq!(Vector2D::new(12.3, 0.0), v6.horizontal());
assert_eq!(Vector2D::new(0.0, 83.2), v6.vertical());
}