diff --git a/matrix/Cargo.toml b/matrix/Cargo.toml index c2fe7f9..b5feca6 100644 --- a/matrix/Cargo.toml +++ b/matrix/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "matrix" -version = "0.1.0" +version = "0.1.1" edition = "2021" license = "MIT" diff --git a/matrix/src/lib.rs b/matrix/src/lib.rs index 6d727e8..c56295e 100644 --- a/matrix/src/lib.rs +++ b/matrix/src/lib.rs @@ -1,10 +1,9 @@ -// AUGGGGHHHHHHHHHHHHH -// I WANT TO USE GENERICS -// BUT I DON'T KNOW HOW TO USE THEM PROPERLY +// use rand::{thread_rng, Rng}; +use std::ops::{Add, AddAssign, Mul, MulAssign}; -use rand::{Rng, thread_rng}; +pub(crate) type Matrix = Vec>; -fn check_valid(a: &Vec>) -> bool { +fn check_valid(a: &Matrix) -> bool { let len = a[0].len(); for i in a { @@ -16,7 +15,7 @@ fn check_valid(a: &Vec>) -> bool { true } -pub fn sum(a: &Vec>, b: &Vec>) -> Option>> { +pub fn sum + Copy>(a: &Matrix, b: &Matrix) -> Option> { if !(check_valid(&a) && check_valid(&b)) { return None; } @@ -25,7 +24,7 @@ pub fn sum(a: &Vec>, b: &Vec>) -> Option>> { return None; } - let mut c: Vec> = Vec::new(); + let mut c: Matrix = Vec::new(); for i in 0..a.len() { c.push(vec![]); for j in 0..a[0].len() { @@ -36,7 +35,10 @@ pub fn sum(a: &Vec>, b: &Vec>) -> Option>> { Some(c) } -pub fn mult(a: &Vec>, b: &Vec>) -> Option>> { +pub fn mul + Copy + AddAssign + MulAssign + std::fmt::Display + From>( + a: &Matrix, + b: &Matrix, +) -> Option> { if !(check_valid(&a) && check_valid(&b)) { return None; } @@ -46,35 +48,35 @@ pub fn mult(a: &Vec>, b: &Vec>) -> Option>> { } let m = a[0].len(); - let mut c: Vec> = Vec::new(); + let mut c: Matrix = Vec::new(); for i in 0..a.len() { c.push(vec![]); for j in 0..b[0].len() { - let mut s = 0.; + let mut s: T = 0_u8.into(); for r in 0..m { s += a[i][r] * b[r][j]; } - c[i].push(s as f64); + c[i].push(s as T); } } Some(c) } - +/* #[allow(dead_code)] -fn gen_matrix(i: usize, j: usize) -> Option>> { +fn gen_matrix(i: usize, j: usize) -> Option> { if !(i > 0 && j > 0) { return None; } let mut rng = thread_rng(); - let mut m: Vec> = Vec::new(); + let mut m: Matrix = Vec::new(); for a in 0..i { m.push(vec![]); for _ in 0..j { - m[a].push(rng.gen_range(-100..100) as f64); + m[a].push(rng.gen_range(0..100) as T); } } Some(m) @@ -82,38 +84,38 @@ fn gen_matrix(i: usize, j: usize) -> Option>> { #[cfg(test)] mod test { - use rand::{Rng, thread_rng}; - + use super::*; + use rand::{thread_rng, Rng}; #[test] fn basic_sum() { let a = vec![vec![1., 2.], vec![3., 4.]]; let b = vec![vec![5., 6.], vec![7., 8.]]; - assert_eq!(crate::sum(&a, &b).unwrap(), vec![vec![6., 8.], vec![10., 12.]]) + assert_eq!(sum(&a, &b).unwrap(), vec![vec![6., 8.], vec![10., 12.]]) } #[test] - fn basic_mult() { + fn basic_mul() { let a = vec![vec![3., -1., 2.], vec![4., 2., 0.], vec![-5., 6., 1.]]; let b = vec![vec![8., 1.], vec![7., 2.], vec![2., -3.]]; - assert_eq!(crate::mult(&a, &b).unwrap(), vec![vec![21., -5.], vec![46., 8.], vec![4., 4.]]) + assert_eq!(mul(&a, &b).unwrap(), vec![vec![21., -5.], vec![46., 8.], vec![4., 4.]]) } #[test] fn mv_sum() { let mut rng = thread_rng(); let (s1, s2) = (rng.gen_range(2..500), rng.gen_range(2..500)); - let a = crate::gen_matrix(s1, s2).unwrap(); - let b = crate::gen_matrix(s1, s2).unwrap(); - assert_eq!(crate::sum(&a, &b).unwrap(), crate::sum(&b, &a).unwrap()) + let a: Matrix = gen_matrix(s1, s2).unwrap(); + let b: Matrix = gen_matrix(s1, s2).unwrap(); + assert_eq!(sum(&a, &b).unwrap(), sum(&b, &a).unwrap()) } #[test] #[ignore] - fn mv_mult() { - let a = crate::gen_matrix(10, 1).unwrap(); - let b = crate::gen_matrix(1, 10).unwrap(); - let ab = crate::mult(&a, &b); - let ba = crate::mult(&b, &a); + fn mv_mul() { + let a: Matrix = gen_matrix(10, 1).unwrap(); + let b: Matrix = gen_matrix(1, 10).unwrap(); + let ab = mul(&a, &b); + let ba = mul(&b, &a); if ab != None && ba != None { assert!(ab.unwrap() == ba.unwrap()) } else { @@ -125,17 +127,14 @@ mod test { fn comb_sum() { let mut rng = thread_rng(); let (s1, s2) = (rng.gen_range(2..500), rng.gen_range(2..500)); - let a = crate::gen_matrix(s1, s2).unwrap(); - let b = crate::gen_matrix(s1, s2).unwrap(); - let c = crate::gen_matrix(s1, s2).unwrap(); - assert_eq!( - crate::sum(&crate::sum(&a, &b).unwrap(), &c).unwrap(), - crate::sum(&a, &crate::sum(&b, &c).unwrap()).unwrap() - ) + let a: Matrix = gen_matrix(s1, s2).unwrap(); + let b: Matrix = gen_matrix(s1, s2).unwrap(); + let c: Matrix = gen_matrix(s1, s2).unwrap(); + assert_eq!(sum(&sum(&a, &b).unwrap(), &c).unwrap(), sum(&a, &sum(&b, &c).unwrap()).unwrap()) } #[test] - fn comb_mult() { + fn comb_mul() { let mut rng = thread_rng(); let (s1, s2, s3, s4) = ( rng.gen_range(2..500), @@ -143,12 +142,13 @@ mod test { rng.gen_range(2..500), rng.gen_range(2..500), ); - let a = crate::gen_matrix(s1, s2).unwrap(); - let b = crate::gen_matrix(s2, s3).unwrap(); - let c = crate::gen_matrix(s3, s4).unwrap(); + let a: Matrix = gen_matrix(s1, s2).unwrap(); + let b: Matrix = gen_matrix(s2, s3).unwrap(); + let c: Matrix = gen_matrix(s3, s4).unwrap(); assert_eq!( - crate::mult(&crate::mult(&a, &b).unwrap(), &c).unwrap(), - crate::mult(&a, &crate::mult(&b, &c).unwrap()).unwrap() + mul(&mul(&a, &b).unwrap(), &c).unwrap(), + mul(&a, &mul(&b, &c).unwrap()).unwrap() ) } } +*/ diff --git a/matrix_graphics/Cargo.toml b/matrix_graphics/Cargo.toml index f1041d8..0a3447b 100644 --- a/matrix_graphics/Cargo.toml +++ b/matrix_graphics/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "matrix_graphics" -version = "0.1.0" +version = "0.1.1" edition = "2021" license = "MIT" diff --git a/matrix_graphics/src/main.rs b/matrix_graphics/src/main.rs index 48b985a..502bf26 100644 --- a/matrix_graphics/src/main.rs +++ b/matrix_graphics/src/main.rs @@ -14,7 +14,7 @@ mod structs; const WIDTH: u32 = 640; const HEIGHT: u32 = 480; -const POLYGON: usize = 7; +const POLYGON: usize = 4; // POLYGON >= 2 const CENTER_X: u32 = 320; const CENTER_Y: u32 = 240; diff --git a/matrix_graphics/src/structs.rs b/matrix_graphics/src/structs.rs index c5e9144..4e5fea5 100644 --- a/matrix_graphics/src/structs.rs +++ b/matrix_graphics/src/structs.rs @@ -48,8 +48,8 @@ impl Line { vec![(self.end_x - self.rotate_center_x)], vec![(self.end_y - self.rotate_center_y)], ]; - let res_start = matrix::mult(&matrix_rotate, &matrix_start).unwrap(); - let res_end = matrix::mult(&matrix_rotate, &matrix_end).unwrap(); + let res_start = matrix::mul(&matrix_rotate, &matrix_start).unwrap(); + let res_end = matrix::mul(&matrix_rotate, &matrix_end).unwrap(); (self.start_x, self.start_y) = (res_start[0][0] + self.rotate_center_x, res_start[1][0] + self.rotate_center_y); (self.end_x, self.end_y) =