convert to using point...

instead of vectors for finding corners in bitmatrix
This commit is contained in:
Henry Schimke
2023-03-21 11:56:53 -05:00
parent 890fb4901f
commit 8a77e7b7ab
4 changed files with 43 additions and 41 deletions

View File

@@ -21,7 +21,7 @@
use std::fmt;
use crate::common::Result;
use crate::{Exceptions, Point};
use crate::{point, Exceptions, Point};
use super::BitArray;
@@ -561,7 +561,7 @@ impl BitMatrix {
*
* @return {@code x,y} coordinate of top-left-most 1 bit, or null if it is all white
*/
pub fn getTopLeftOnBit(&self) -> Option<Vec<u32>> {
pub fn getTopLeftOnBit(&self) -> Option<Point> {
let mut bitsOffset = 0;
while bitsOffset < self.bits.len() && self.bits[bitsOffset] == 0 {
bitsOffset += 1;
@@ -578,10 +578,10 @@ impl BitMatrix {
bit += 1;
}
x += bit;
Some(vec![x as u32, y as u32])
Some(point(x as f32, y as f32))
}
pub fn getBottomRightOnBit(&self) -> Option<[u32; 2]> {
pub fn getBottomRightOnBit(&self) -> Option<Point> {
let mut bitsOffset = self.bits.len() as i64 - 1;
while bitsOffset >= 0 && self.bits[bitsOffset as usize] == 0 {
bitsOffset -= 1;
@@ -600,7 +600,7 @@ impl BitMatrix {
}
x += bit;
Some([x as u32, y as u32])
Some(point(x as f32, y as f32))
}
/**

View File

@@ -27,6 +27,8 @@
// */
// public final class BitMatrixTestCase extends Assert {
use crate::point;
use super::BitMatrix;
static BIT_MATRIX_POINTS: [u32; 6] = [1, 2, 2, 0, 3, 1];
@@ -87,14 +89,14 @@ fn test_on_bit() {
assert!(matrix.getTopLeftOnBit().is_none());
assert!(matrix.getBottomRightOnBit().is_none());
matrix.setRegion(1, 1, 1, 1).expect("must set");
assert_eq!(vec![1, 1], matrix.getTopLeftOnBit().unwrap());
assert_eq!(vec![1, 1], matrix.getBottomRightOnBit().unwrap());
assert_eq!(point(1.0, 1.0), matrix.getTopLeftOnBit().unwrap());
assert_eq!(point(1.0, 1.0), matrix.getBottomRightOnBit().unwrap());
matrix.setRegion(1, 1, 3, 2).expect("must set");
assert_eq!(vec![1, 1], matrix.getTopLeftOnBit().unwrap());
assert_eq!(vec![3, 2], matrix.getBottomRightOnBit().unwrap());
assert_eq!(point(1.0, 1.0), matrix.getTopLeftOnBit().unwrap());
assert_eq!(point(3.0, 2.0), matrix.getBottomRightOnBit().unwrap());
matrix.setRegion(0, 0, 5, 5).expect("must set");
assert_eq!(vec![0, 0], matrix.getTopLeftOnBit().unwrap());
assert_eq!(vec![4, 4], matrix.getBottomRightOnBit().unwrap());
assert_eq!(point(0.0, 0.0), matrix.getTopLeftOnBit().unwrap());
assert_eq!(point(4.0, 4.0), matrix.getBottomRightOnBit().unwrap());
}
#[test]