aboutsummaryrefslogtreecommitdiff
path: root/src/core/vector2.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/vector2.rs')
-rw-r--r--src/core/vector2.rs55
1 files changed, 38 insertions, 17 deletions
diff --git a/src/core/vector2.rs b/src/core/vector2.rs
index 7e40394..0e0165e 100644
--- a/src/core/vector2.rs
+++ b/src/core/vector2.rs
@@ -2,7 +2,7 @@
//!
//! This is implemented generictly with types that fit in the Number trait
use crate::{Float, Number};
-use std::ops::{Sub, Add, Mul};
+use std::ops::{Sub, Add, Mul, Div};
use std::fmt;
use std::cmp::min;
@@ -15,6 +15,7 @@ pub struct Vector2<T: Number> {
pub type Vector2f = Vector2<Float>;
pub type Vector2i = Vector2<i32>;
+
impl<T: Number> Vector2<T> {
pub fn new(initial: T) -> Vector2<T> {
Vector2 { x: initial, y: initial }
@@ -45,6 +46,16 @@ impl<T: Number> Add for Vector2<T> {
}
}
+impl<T: Number> Mul for Vector2<T> {
+ type Output = Self;
+ fn mul(self, op: Self) -> Self::Output {
+ Self::new_xy(
+ self.x * op.x,
+ self.y * op.y,
+ )
+ }
+}
+
impl<T: Number> Mul<T> for Vector2<T> {
type Output = Self;
fn mul(self, op: T) -> Self::Output {
@@ -55,25 +66,19 @@ impl<T: Number> Mul<T> for Vector2<T> {
}
}
-impl<T: Number> fmt::Display for Vector2<T> {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- f.write_fmt(format_args!("[{}, {}]", self.x, self.y))
- }
-}
-
-impl Vector2f {
- pub fn ceil(&self) -> Self {
+impl<T: Number> Div for Vector2<T> {
+ type Output = Self;
+ fn div(self, op: Self) -> Self::Output {
Self::new_xy(
- self.x.ceil(),
- self.y.ceil()
- )
+ self.x / op.x,
+ self.y / op.y,
+ )
}
+}
- pub fn floor(&self) -> Self {
- Self::new_xy(
- self.x.floor(),
- self.y.floor()
- )
+impl<T: Number> fmt::Display for Vector2<T> {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ f.write_fmt(format_args!("[{}, {}]", self.x, self.y))
}
}
@@ -96,6 +101,8 @@ impl From<Vector2f> for Vector2i {
}
impl Vector2i {
+ pub const ZERO: Self = Vector2i {x: 0, y: 0};
+
pub fn cap(&self, x: i32, y: i32) -> Self {
Self::new_xy(
min(self.x, x),
@@ -108,6 +115,20 @@ impl Vector2f {
pub fn len(&self) -> Float {
(self.x*self.x + self.y*self.y).sqrt()
}
+
+ pub fn ceil(&self) -> Self {
+ Self::new_xy(
+ self.x.ceil(),
+ self.y.ceil()
+ )
+ }
+
+ pub fn floor(&self) -> Self {
+ Self::new_xy(
+ self.x.floor(),
+ self.y.floor()
+ )
+ }
}