1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
// #![no_std]
#[cfg(any(test, feature = "std"))]
// #[cfg_attr(test, macro_use)]
// extern crate std;
// use core::fmt;
#[cfg(test)]
use core::hash;
// use core::iter::{Product, Sum};
use core::ops::{Add, Div, Mul, Neg, Rem, Sub};
// use core::str::FromStr;
#[cfg(feature = "std")]
use std::error::Error;
use num_traits::{Num, Signed, Zero};
/// The code defines a generic struct called Vector2 with two fields, x_ and y_.
///
/// Properties:
///
/// * `x_`: The `x_` property represents the x-coordinate of the Vector2 object. It is of type `T`,
/// which means it can be any type specified when creating an instance of the Vector2 struct.
/// * `y_`: The `y_` property is the y-coordinate of the `Vector2` object. It represents the vertical
/// position of the vector in a 2D coordinate system.
#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug, Default)]
// #[repr(C)]
pub struct Vector2<T> {
/// x portion of the Vector2 object
pub x_: T,
/// y portion of the Vector2 object
pub y_: T,
}
impl<T> Vector2<T> {
/// The function `new` creates a new Vector2 with the given x and y values.
///
/// Arguments:
///
/// * `x_`: The parameter `x_` represents the x-coordinate of the Vector2.
/// * `y_`: The parameter `y_` represents the y-coordinate of the Vector2. It is of type `T`, which
/// means it can be any type that is specified when the Vector2 is created.
///
/// Returns:
///
/// The `new` function is returning a new instance of the `Vector2` struct with the provided `x_`
/// and `y_` values.
///
/// # Example
///
/// ```
/// use physdes::vector2::Vector2;
///
/// assert_eq!(Vector2::new(1, 2), Vector2 { x_: 1, y_: 2 });
/// assert_eq!(Vector2::new(3, 4), Vector2 { x_: 3, y_: 4 });
/// ```
#[inline]
pub const fn new(x_: T, y_: T) -> Self {
Vector2 { x_, y_ }
}
}
impl<T: Clone + Num> Vector2<T> {
/// The `dot` function calculates the dot product of two vectors.
///
/// Arguments:
///
/// * `other`: The `other` parameter is of the same type as `self`, which means it is an instance of
/// the same struct or class that the `dot` method is defined in.
///
/// Returns:
///
/// The dot product of two vectors is being returned.
///
/// # Example
///
/// ```
/// use physdes::vector2::Vector2;
///
/// assert_eq!(Vector2::new(1, 2).dot(&Vector2::new(3, 4)), 11);
/// assert_eq!(Vector2::new(3, 4).dot(&Vector2::new(1, 2)), 11);
/// ```
#[inline]
pub fn dot(&self, other: &Self) -> T {
self.x_.clone() * other.x_.clone() + self.y_.clone() * other.y_.clone()
}
/// The `cross` function calculates the cross product of two vectors.
///
/// Arguments:
///
/// * `other`: The `other` parameter is of type `Self`, which means it is the same type as the
/// current object.
///
/// Returns:
///
/// The cross product of two vectors is being returned.
/// Returns the cross product
///
/// # Example
///
/// ```
/// use physdes::vector2::Vector2;
///
/// assert_eq!(Vector2::new(1, 2).cross(&Vector2::new(3, 4)), -2);
/// assert_eq!(Vector2::new(3, 4).cross(&Vector2::new(1, 2)), 2);
/// ```
#[inline]
pub fn cross(&self, other: &Self) -> T {
self.x_.clone() * other.y_.clone() - self.y_.clone() * other.x_.clone()
}
/// The `norm_sqr` function calculates the square of the norm of a vector.
///
/// Returns:
///
/// The `norm_sqr` function returns the squared norm of the object on which it is called.
///
/// # Example
///
/// ```
/// use physdes::vector2::Vector2;
///
/// assert_eq!(Vector2::new(1, 2).norm_sqr(), 5);
/// assert_eq!(Vector2::new(3, 4).norm_sqr(), 25);
/// ```
#[inline]
pub fn norm_sqr(&self) -> T {
self.dot(self)
}
/// The `scale` function multiplies the vector by a scalar value.
///
/// Arguments:
///
/// * `t`: The parameter `t` is a scalar value that will be used to multiply each component of
/// `self`.
///
/// Returns:
///
/// The `scale` method returns a new instance of the same type as `self`.
/// Multiplies `self` by the scalar `t`.
///
/// # Example
///
/// ```
/// use physdes::vector2::Vector2;
///
/// assert_eq!(Vector2::new(1, 2).scale(3), Vector2::new(3, 6));
/// assert_eq!(Vector2::new(3, 4).scale(2), Vector2::new(6, 8));
/// ```
#[inline]
pub fn scale(&self, t: T) -> Self {
Self::new(self.x_.clone() * t.clone(), self.y_.clone() * t)
}
/// The `unscale` function divides the coordinates of a vector by a scalar value.
///
/// Arguments:
///
/// * `t`: The parameter `t` is a scalar value that is used to divide the `self` object. It is of
/// type `T`, which is a generic type parameter. The division operation is performed on the `x_` and
/// `y_` fields of the `self` object.
///
/// Returns:
///
/// The `unscale` method returns a new instance of the same type as `self`.
/// Divides `self` by the scalar `t`.
///
/// # Example
///
/// ```
/// use physdes::vector2::Vector2;
///
/// assert_eq!(Vector2::new(3, 6).unscale(3), Vector2::new(1, 2));
/// assert_eq!(Vector2::new(6, 8).unscale(2), Vector2::new(3, 4));
/// ```
#[inline]
pub fn unscale(&self, t: T) -> Self {
Self::new(self.x_.clone() / t.clone(), self.y_.clone() / t)
}
}
impl<T: Clone + Signed> Vector2<T> {
/// The `l1_norm` function calculates the Manhattan distance from the origin.
///
/// [Manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry
///
/// Returns:
///
/// The L1 norm, which is the Manhattan distance from the origin.
/// Returns the L1 norm `|x_| + |y_|` -- the [Manhattan distance] from the origin.
///
/// # Example
///
/// ```
/// use physdes::vector2::Vector2;
///
/// assert_eq!(Vector2::new(1, 2).l1_norm(), 3);
/// assert_eq!(Vector2::new(3, 4).l1_norm(), 7);
/// ```
#[inline]
pub fn l1_norm(&self) -> T {
self.x_.abs() + self.y_.abs()
}
}
impl<T: Clone + PartialOrd> Vector2<T> {
/// The `norm_inf` function returns the maximum absolute value between `x_` and `y_`.
///
/// Returns:
///
/// The `norm_inf` function returns the maximum value between `|x_|` and `|y_|`.
/// Returns the infinity norm `max(|x_| + |y_|)`
///
/// # Example
///
/// ```
/// use physdes::vector2::Vector2;
///
/// assert_eq!(Vector2::new(1, 2).norm_inf(), 2);
/// assert_eq!(Vector2::new(3, 4).norm_inf(), 4);
/// ```
#[inline]
pub fn norm_inf(&self) -> T {
if self.x_ > self.y_ {
self.x_.clone()
} else {
self.y_.clone()
}
}
}
macro_rules! forward_xf_xf_binop {
(impl $imp:ident, $method:ident) => {
impl<'a, 'b, T: Clone + Num> $imp<&'b Vector2<T>> for &'a Vector2<T> {
type Output = Vector2<T>;
/// The function clones the input arguments and calls the specified method on them.
///
/// Arguments:
///
/// * `other`: A reference to another Vector2 object of the same type as self.
#[inline]
fn $method(self, other: &Vector2<T>) -> Self::Output {
self.clone().$method(other.clone())
}
}
};
}
macro_rules! forward_xf_val_binop {
(impl $imp:ident, $method:ident) => {
impl<'a, T: Clone + Num> $imp<Vector2<T>> for &'a Vector2<T> {
type Output = Vector2<T>;
#[inline]
fn $method(self, other: Vector2<T>) -> Self::Output {
self.clone().$method(other)
}
}
};
}
macro_rules! forward_val_xf_binop {
(impl $imp:ident, $method:ident) => {
impl<'a, T: Clone + Num> $imp<&'a Vector2<T>> for Vector2<T> {
type Output = Vector2<T>;
#[inline]
fn $method(self, other: &Vector2<T>) -> Self::Output {
self.$method(other.clone())
}
}
};
}
macro_rules! forward_all_binop {
(impl $imp:ident, $method:ident) => {
forward_xf_xf_binop!(impl $imp, $method);
forward_xf_val_binop!(impl $imp, $method);
forward_val_xf_binop!(impl $imp, $method);
};
}
// arithmetic
forward_all_binop!(impl Add, add);
// (a, b) + (c, d) == (a + c), (b + d)
impl<T: Clone + Num> Add<Vector2<T>> for Vector2<T> {
type Output = Self;
/// The function `add` takes two values of the same type and returns their sum.
///
/// Arguments:
///
/// * `other`: The `other` parameter is of the same type as `self` and represents the other object
/// that you want to add to `self`.
///
/// # Example
///
/// ```
/// use physdes::vector2::Vector2;
/// use std::ops::Add;
///
/// assert_eq!(Vector2::new(1, 2).add(Vector2::new(3, 4)), Vector2::new(4, 6));
/// assert_eq!(Vector2::new(3, 4).add(Vector2::new(1, 2)), Vector2::new(4, 6));
/// ```
#[inline]
fn add(self, other: Self) -> Self::Output {
Self::Output::new(self.x_ + other.x_, self.y_ + other.y_)
}
}
forward_all_binop!(impl Sub, sub);
// (a, b) - (c, d) == (a - c), (b - d)
impl<T: Clone + Num> Sub<Vector2<T>> for Vector2<T> {
type Output = Self;
/// The function subtracts the coordinates of two points and returns a new point.
///
/// Arguments:
///
/// * `other`: The `other` parameter is of the same type as `self` and represents the other value
/// that you want to subtract from `self`.
///
/// # Example
///
/// ```
/// use physdes::vector2::Vector2;
/// use std::ops::Sub;
///
/// assert_eq!(Vector2::new(1, 2).sub(Vector2::new(3, 4)), Vector2::new(-2, -2));
/// assert_eq!(Vector2::new(3, 4).sub(Vector2::new(1, 2)), Vector2::new(2, 2));
/// ```
#[inline]
fn sub(self, other: Self) -> Self::Output {
Self::Output::new(self.x_ - other.x_, self.y_ - other.y_)
}
}
// Op Assign
mod opassign {
use core::ops::{AddAssign, DivAssign, MulAssign, SubAssign};
use num_traits::NumAssign;
use crate::Vector2;
impl<T: Clone + NumAssign> AddAssign for Vector2<T> {
/// The function `add_assign` adds the values of `other.x_` and `other.y_` to `self.x_` and
/// `self.y_` respectively.
///
/// Arguments:
///
/// * `other`: The "other" parameter is of type Self, which means it is a reference to another
/// instance of the same struct or class that the method is defined in. In this case, it
/// represents another instance of the struct or class that has the same fields or properties as
/// self.
///
/// # Example
///
/// ```
/// use physdes::vector2::Vector2;
/// use std::ops::AddAssign;
///
/// let mut v = Vector2::new(1, 2);
/// let v2 = Vector2::new(3, 4);
/// v.add_assign(v2);
/// assert_eq!(v, Vector2::new(4, 6));
/// ```
fn add_assign(&mut self, other: Self) {
self.x_ += other.x_;
self.y_ += other.y_;
}
}
impl<T: Clone + NumAssign> SubAssign for Vector2<T> {
/// The function subtracts the values of another object from the values of the current object.
///
/// Arguments:
///
/// * `other`: The parameter "other" is of type Self, which means it is a reference to another
/// instance of the same struct or class that the method is defined in. In this case, it is a
/// reference to another instance of the struct or class that has the same fields as self (x_
/// and y
///
/// # Example
///
/// ```
/// use physdes::vector2::Vector2;
/// use std::ops::SubAssign;
/// let mut v = Vector2::new(1, 2);
/// let v2 = Vector2::new(3, 4);
/// v.sub_assign(v2);
/// assert_eq!(v, Vector2::new(-2, -2));
/// ```
fn sub_assign(&mut self, other: Self) {
self.x_ -= other.x_;
self.y_ -= other.y_;
}
}
impl<T: Clone + NumAssign> MulAssign<T> for Vector2<T> {
/// The function multiplies the values of self.x_ and self.y_ by the value of other.
///
/// Arguments:
///
/// * `other`: The parameter `other` is of type `T`, which means it can be any type that
/// implements the `Clone` trait.
///
/// # Example
///
/// ```
/// use physdes::vector2::Vector2;
/// use std::ops::MulAssign;
///
/// let mut v = Vector2::new(1, 2);
/// v.mul_assign(3);
/// assert_eq!(v, Vector2::new(3, 6));
/// ```
fn mul_assign(&mut self, other: T) {
self.x_ *= other.clone();
self.y_ *= other;
}
}
impl<T: Clone + NumAssign> DivAssign<T> for Vector2<T> {
/// The function divides the values of self.x_ and self.y_ by the value of other.
///
/// Arguments:
///
/// * `other`: The parameter `other` is of type `T`, which means it can be any type that
/// implements the `Clone` trait.
///
/// # Example
///
/// ```
/// use physdes::vector2::Vector2;
/// use std::ops::DivAssign;
///
/// let mut v = Vector2::new(3, 6);
/// v.div_assign(3);
/// assert_eq!(v, Vector2::new(1, 2));
/// ```
fn div_assign(&mut self, other: T) {
self.x_ /= other.clone();
self.y_ /= other;
}
}
macro_rules! forward_op_assign1 {
(impl $imp:ident, $method:ident) => {
impl<'a, T: Clone + NumAssign> $imp<&'a Vector2<T>> for Vector2<T> {
#[inline]
fn $method(&mut self, other: &Self) {
self.$method(other.clone())
}
}
};
}
macro_rules! forward_op_assign2 {
(impl $imp:ident, $method:ident) => {
impl<'a, T: Clone + NumAssign> $imp<&'a T> for Vector2<T> {
#[inline]
fn $method(&mut self, other: &T) {
self.$method(other.clone())
}
}
};
}
forward_op_assign1!(impl AddAssign, add_assign);
forward_op_assign1!(impl SubAssign, sub_assign);
forward_op_assign2!(impl MulAssign, mul_assign);
forward_op_assign2!(impl DivAssign, div_assign);
}
impl<T: Clone + Num + Neg<Output = T>> Neg for Vector2<T> {
type Output = Self;
/// The `neg` function returns a new instance of the same type with the negated values of `x_` and
/// `y_`.
///
/// # Example
///
/// ```
/// use physdes::vector2::Vector2;
/// use std::ops::Neg;
///
/// let v = Vector2::new(1, 2);
/// assert_eq!(-v, Vector2::new(-1, -2));
/// ```
#[inline]
fn neg(self) -> Self::Output {
Self::Output::new(-self.x_, -self.y_)
}
}
impl<'a, T: Clone + Num + Neg<Output = T>> Neg for &'a Vector2<T> {
type Output = Vector2<T>;
#[inline]
fn neg(self) -> Self::Output {
-self.clone()
}
}
macro_rules! scalar_arithmetic {
(@forward $imp:ident::$method:ident for $($scalar:ident),*) => (
impl<'a, T: Clone + Num> $imp<&'a T> for Vector2<T> {
type Output = Vector2<T>;
#[inline]
fn $method(self, other: &T) -> Self::Output {
self.$method(other.clone())
}
}
impl<'a, T: Clone + Num> $imp<T> for &'a Vector2<T> {
type Output = Vector2<T>;
#[inline]
fn $method(self, other: T) -> Self::Output {
self.clone().$method(other)
}
}
impl<'a, 'b, T: Clone + Num> $imp<&'a T> for &'b Vector2<T> {
type Output = Vector2<T>;
#[inline]
fn $method(self, other: &T) -> Self::Output {
self.clone().$method(other.clone())
}
}
$(
impl<'a> $imp<&'a Vector2<$scalar>> for $scalar {
type Output = Vector2<$scalar>;
#[inline]
fn $method(self, other: &Vector2<$scalar>) -> Vector2<$scalar> {
self.$method(other.clone())
}
}
impl<'a> $imp<Vector2<$scalar>> for &'a $scalar {
type Output = Vector2<$scalar>;
#[inline]
fn $method(self, other: Vector2<$scalar>) -> Vector2<$scalar> {
self.clone().$method(other)
}
}
impl<'a, 'b> $imp<&'a Vector2<$scalar>> for &'b $scalar {
type Output = Vector2<$scalar>;
#[inline]
fn $method(self, other: &Vector2<$scalar>) -> Vector2<$scalar> {
self.clone().$method(other.clone())
}
}
)*
);
($($scalar:ident),*) => (
scalar_arithmetic!(@forward Mul::mul for $($scalar),*);
// scalar_arithmetic!(@forward Div::div for $($scalar),*);
// scalar_arithmetic!(@forward Rem::rem for $($scalar),*);
$(
impl Mul<Vector2<$scalar>> for $scalar {
type Output = Vector2<$scalar>;
#[inline]
fn mul(self, other: Vector2<$scalar>) -> Self::Output {
Self::Output::new(self * other.x_, self * other.y_)
}
}
)*
);
}
impl<T: Clone + Num> Mul<T> for Vector2<T> {
type Output = Vector2<T>;
#[inline]
fn mul(self, other: T) -> Self::Output {
Self::Output::new(self.x_ * other.clone(), self.y_ * other)
}
}
impl<T: Clone + Num> Div<T> for Vector2<T> {
type Output = Self;
#[inline]
fn div(self, other: T) -> Self::Output {
Self::Output::new(self.x_ / other.clone(), self.y_ / other)
}
}
impl<T: Clone + Num> Rem<T> for Vector2<T> {
type Output = Vector2<T>;
#[inline]
fn rem(self, other: T) -> Self::Output {
Self::Output::new(self.x_ % other.clone(), self.y_ % other)
}
}
scalar_arithmetic!(usize, u8, u16, u32, u64, u128, isize, i8, i16, i32, i64, i128, f32, f64);
// constants
impl<T: Clone + Num> Zero for Vector2<T> {
#[inline]
fn zero() -> Self {
Self::new(Zero::zero(), Zero::zero())
}
#[inline]
fn is_zero(&self) -> bool {
self.x_.is_zero() && self.y_.is_zero()
}
#[inline]
fn set_zero(&mut self) {
self.x_.set_zero();
self.y_.set_zero();
}
}
#[cfg(test)]
fn hash<T: hash::Hash>(x: &T) -> u64 {
use std::collections::hash_map::RandomState;
use std::hash::{BuildHasher, Hasher};
let mut hasher = <RandomState as BuildHasher>::Hasher::new();
x.hash(&mut hasher);
hasher.finish()
}
#[cfg(test)]
mod test {
#![allow(non_upper_case_globals)]
use super::{hash, Vector2};
use core::f64;
use num_traits::Zero;
pub const _0_0v: Vector2<f64> = Vector2 { x_: 0.0, y_: 0.0 };
pub const _1_0v: Vector2<f64> = Vector2 { x_: 1.0, y_: 0.0 };
pub const _1_1v: Vector2<f64> = Vector2 { x_: 1.0, y_: 1.0 };
pub const _0_1v: Vector2<f64> = Vector2 { x_: 0.0, y_: 1.0 };
pub const _neg1_1v: Vector2<f64> = Vector2 { x_: -1.0, y_: 1.0 };
pub const _05_05v: Vector2<f64> = Vector2 { x_: 0.5, y_: 0.5 };
pub const all_consts: [Vector2<f64>; 5] = [_0_0v, _1_0v, _1_1v, _neg1_1v, _05_05v];
pub const _4_2v: Vector2<f64> = Vector2 { x_: 4.0, y_: 2.0 };
#[test]
fn test_consts() {
// check our constants are what Vector2::new creates
fn test(c: Vector2<f64>, r: f64, i: f64) {
assert_eq!(c, Vector2::new(r, i));
}
test(_0_0v, 0.0, 0.0);
test(_1_0v, 1.0, 0.0);
test(_1_1v, 1.0, 1.0);
test(_neg1_1v, -1.0, 1.0);
test(_05_05v, 0.5, 0.5);
assert_eq!(_0_0v, Zero::zero());
}
#[test]
fn test_scale_unscale() {
assert_eq!(_05_05v.scale(2.0), _1_1v);
assert_eq!(_1_1v.unscale(2.0), _05_05v);
for &c in all_consts.iter() {
assert_eq!(c.scale(2.0).unscale(2.0), c);
}
}
#[test]
fn test_hash() {
let a = Vector2::new(0i32, 0i32);
let b = Vector2::new(1i32, 0i32);
let c = Vector2::new(0i32, 1i32);
assert!(hash(&a) != hash(&b));
assert!(hash(&b) != hash(&c));
assert!(hash(&c) != hash(&a));
}
}