Added class matrix
This commit is contained in:
		
							parent
							
								
									eaefcd3272
								
							
						
					
					
						commit
						4da27c5777
					
				
							
								
								
									
										62
									
								
								src/engine/math/mod.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										62
									
								
								src/engine/math/mod.rs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,62 @@ | ||||
| use std::ops::{ | ||||
|     Index, | ||||
|     IndexMut | ||||
| }; | ||||
| 
 | ||||
| /// A generic matrix type, useful for levels.
 | ||||
| pub struct Matrix<T> { | ||||
| 
 | ||||
|     /// The number of rows of the matrix.
 | ||||
|     rows: usize, | ||||
| 
 | ||||
|     /// The number of cols of the matrix.
 | ||||
|     cols: usize, | ||||
| 
 | ||||
|     /// The contained data in the matrix.
 | ||||
|     data: Vec<T>, | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| impl<T> Matrix<T> where T: Clone { | ||||
|     /// Creates a matrix from an element and duplicates it.
 | ||||
|     pub fn from_size(rows: usize, cols: usize, element: T) -> Matrix<T> { | ||||
|         Matrix { | ||||
|             rows: rows, | ||||
|             cols: cols, | ||||
|             data: vec![element; rows * cols], | ||||
|         } | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| impl<T> Matrix<T> { | ||||
|     /// Converts an a pair corresponding to the row and columns into an integer.
 | ||||
|     pub fn to_usize(&self, (row, col): (usize, usize)) -> usize { | ||||
|         self.cols * col + row | ||||
|     } | ||||
| 
 | ||||
|     /// Returns the number of rows.
 | ||||
|     pub fn rows(&self) -> usize { | ||||
|         self.rows | ||||
|     } | ||||
| 
 | ||||
|     /// Returns the number of columns.
 | ||||
|     pub fn cols(&self) -> usize { | ||||
|         self.cols | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| impl<T> Index<(usize, usize)> for Matrix<T> { | ||||
|     type Output = T; | ||||
| 
 | ||||
|     fn index(&self, index: (usize, usize)) -> &T { | ||||
|         let index = self.to_usize(index); | ||||
|         &self.data[index] | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| impl<T> IndexMut<(usize, usize)> for Matrix<T> { | ||||
|     fn index_mut(&mut self, index: (usize, usize)) -> &mut T { | ||||
|         let index = self.to_usize(index); | ||||
|         &mut self.data[index] | ||||
|     } | ||||
| } | ||||
| @ -10,3 +10,6 @@ pub mod controls; | ||||
| 
 | ||||
| /// This module helps us dealing with textures.
 | ||||
| pub mod texture; | ||||
| 
 | ||||
| /// This module contains the math tools that will be used.
 | ||||
| pub mod math; | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user