csci5607/assignment-0/src/vec2.rs

26 lines
406 B
Rust
Raw Normal View History

2023-01-21 08:03:11 +00:00
use std::ops::Mul;
2023-01-21 08:55:19 +00:00
/// A generic 2-dimensional point class.
2023-01-21 08:22:22 +00:00
#[derive(Copy, Clone)]
2023-01-21 08:03:11 +00:00
pub struct Vec2<T = f64> {
2023-01-21 08:22:22 +00:00
pub x: T,
pub y: T,
2023-01-21 08:03:11 +00:00
}
impl<T> Vec2<T> {
2023-01-21 08:22:22 +00:00
pub fn new(x: T, y: T) -> Self {
Self { x, y }
}
2023-01-21 08:03:11 +00:00
}
impl<S: Copy, T: Mul<S>> Mul<S> for Vec2<T> {
2023-01-21 08:22:22 +00:00
type Output = Vec2<T::Output>;
2023-01-21 08:03:11 +00:00
2023-01-21 08:22:22 +00:00
fn mul(self, rhs: S) -> Self::Output {
Vec2 {
x: self.x * rhs,
y: self.y * rhs,
2023-01-21 08:03:11 +00:00
}
2023-01-21 08:22:22 +00:00
}
2023-01-21 08:03:11 +00:00
}