sfml_types/
vector2.rs

1/*
2* Rust-SFML - Copyright (c) 2013 Letang Jeremy.
3*
4* The original software, SFML library, is provided by Laurent Gomila.
5*
6* This software is provided 'as-is', without any express or implied warranty.
7* In no event will the authors be held liable for any damages arising from
8* the use of this software.
9*
10* Permission is granted to anyone to use this software for any purpose,
11* including commercial applications, and to alter it and redistribute it
12* freely, subject to the following restrictions:
13*
14* 1. The origin of this software must not be misrepresented; you must not claim
15*    that you wrote the original software. If you use this software in a product,
16*    an acknowledgment in the product documentation would be appreciated but is
17*    not required.
18*
19* 2. Altered source versions must be plainly marked as such, and must not be
20*    misrepresented as being the original software.
21*
22* 3. This notice may not be removed or altered from any source distribution.
23*/
24
25//! Utility Class providing 2 dimensional vectors for i32, u32, and f32.
26
27use std::ops::{Add, Sub, Mul, Div};
28
29/// Implementation of Vector2i
30#[repr(C)]
31#[derive(Clone, PartialOrd, Ord, PartialEq, Eq, Debug, Copy)]
32pub struct Vector2<T> {
33    /// X coordinate of the vector.
34    pub x: T,
35    /// Y coordinate of the vector.
36    pub y: T
37}
38
39/// export Vector2<i32> as Vector2i
40pub type Vector2i = Vector2<i32>;
41/// export Vector2<u32> as Vector2u
42pub type Vector2u = Vector2<u32>;
43/// export Vector2<f32> as Vector2f
44pub type Vector2f = Vector2<f32>;
45
46impl<T> Vector2<T> {
47    /// Build a new Vector2<T>
48    pub fn new(x: T, y: T) -> Vector2<T> {
49        Vector2 {
50            x: x,
51            y: y
52        }
53    }
54}
55
56impl<T: Add + Copy> Add<T> for Vector2<T> {
57    type Output = Vector2<T::Output>;
58
59    fn add(self, rhs: T) -> Vector2<T::Output> {
60        Vector2 {
61            x: self.x + rhs,
62            y: self.y + rhs
63        }
64    }
65}
66
67impl<T: Sub + Copy> Sub<T> for Vector2<T> {
68    type Output = Vector2<T::Output>;
69
70    fn sub(self, rhs: T) -> Vector2<T::Output> {
71        Vector2 {
72            x: self.x - rhs,
73            y: self.y - rhs
74        }
75    }
76}
77
78impl<T: Mul + Copy> Mul<T> for Vector2<T> {
79    type Output = Vector2<T::Output>;
80
81    fn mul(self, rhs: T) -> Vector2<T::Output> {
82        Vector2 {
83            x: self.x * rhs,
84            y: self.y * rhs
85        }
86    }
87}
88
89impl<T: Div + Copy> Div<T> for Vector2<T> {
90    type Output = Vector2<T::Output>;
91
92    fn div(self, rhs: T) -> Vector2<T::Output> {
93        Vector2 {
94            x: self.x / rhs,
95            y: self.y / rhs
96        }
97    }
98}
99
100
101impl<T: Add> Add for Vector2<T> {
102    type Output = Vector2<T::Output>;
103
104    fn add(self, rhs: Vector2<T>) -> Vector2<T::Output> {
105        Vector2 {
106            x: self.x + rhs.x,
107            y: self.y + rhs.y
108        }
109    }
110}
111
112impl<T: Sub> Sub for Vector2<T> {
113    type Output = Vector2<T::Output>;
114
115    fn sub(self, rhs: Vector2<T>) -> Vector2<T::Output> {
116        Vector2 {
117            x: self.x - rhs.x,
118            y: self.y - rhs.y
119        }
120    }
121}
122
123impl<T: Mul> Mul for Vector2<T> {
124    type Output = Vector2<T::Output>;
125
126    fn mul(self, rhs: Vector2<T>) -> Vector2<T::Output> {
127        Vector2 {
128            x: self.x * rhs.x,
129            y: self.y * rhs.y
130        }
131    }
132}
133
134impl<T: Div> Div for Vector2<T> {
135    type Output = Vector2<T::Output>;
136
137    fn div(self, rhs: Vector2<T>) -> Vector2<T::Output> {
138        Vector2 {
139            x: self.x / rhs.x,
140            y: self.y / rhs.y
141        }
142    }
143}
144
145
146/// Utility trait to convert a Vector2 on another type
147pub trait ToVec {
148    /// Convert the current Vector2 to a Vector2f
149    fn to_vector2f(&self) -> Vector2f;
150    /// Convert the current Vector2 to a Vector2i
151    fn to_vector2i(&self) -> Vector2i;
152    /// Convert the current Vector2f to a Vector2u
153    fn to_vector2u(&self) -> Vector2u;
154}
155
156impl ToVec for Vector2f {
157    fn to_vector2f(&self) -> Vector2f {
158        self.clone()
159    }
160
161    fn to_vector2i(&self) -> Vector2i {
162        Vector2i {
163            x: self.x as i32,
164            y: self.y as i32
165        }
166    }
167
168    fn to_vector2u(&self) -> Vector2u {
169        Vector2u {
170            x: self.x as u32,
171            y: self.y as u32
172        }
173    }
174}
175
176impl ToVec for Vector2i {
177    fn to_vector2f(&self) -> Vector2f {
178        Vector2f {
179            x: self.x as f32,
180            y: self.y as f32
181        }
182    }
183
184    fn to_vector2i(&self) -> Vector2i {
185         self.clone()
186    }
187
188    fn to_vector2u(&self) -> Vector2u {
189        Vector2u {
190            x: self.x as u32,
191            y: self.y as u32
192        }
193    }
194}
195
196impl ToVec for Vector2u {
197    fn to_vector2f(&self) -> Vector2f {
198        Vector2f {
199            x: self.x as f32,
200            y: self.y as f32
201        }
202    }
203
204    fn to_vector2i(&self) -> Vector2i {
205        Vector2i {
206            x: self.x as i32,
207            y: self.y as i32
208        }
209    }
210
211    fn to_vector2u(&self) -> Vector2u {
212        self.clone()
213    }
214}