removed dependency from rmath

This commit is contained in:
2025-06-25 10:23:00 +08:00
parent 3cfbe4d87a
commit b2d7b3449e
3 changed files with 94 additions and 170 deletions

View File

@@ -1,4 +1,3 @@
use rmath::SMatrix;
use std::clone::Clone;
use std::cmp::Eq;
use std::cmp::PartialEq;
@@ -10,54 +9,56 @@ use std::ops::Mul;
use std::ops::Neg;
use std::ops::Sub;
pub type Xform = SMatrix<f64, 3, 3>;
#[derive(Debug)]
pub struct Point(SMatrix<f64, 1, 3>);
pub struct Point {
x: f64,
y: f64,
}
impl Point {
pub fn x(&self) -> f64 {
self.0[(0, 0)]
self.x
}
pub fn y(&self) -> f64 {
self.0[(0, 1)]
self.y
}
pub fn new(x: f64, y: f64) -> Point {
Point(SMatrix::new(|pos| match pos {
(0, 0) => x,
(0, 1) => y,
(0, 2) => 1f64,
_ => 0f64,
}))
Point { x, y }
}
}
impl Add<&Point> for &Point {
fn add(self, rhs: &Point) -> Self::Output {
self * &xlate(rhs.x(), rhs.y())
Point {
x: self.x() + rhs.x(),
y: self.y() + rhs.y(),
}
}
type Output = Point;
}
impl Sub<&Point> for &Point {
fn sub(self, rhs: &Point) -> Self::Output {
self * &xlate(-rhs.x(), -rhs.y())
Point {
x: self.x() - rhs.x(),
y: self.y() - rhs.y(),
}
}
type Output = Point;
}
impl Mul<f64> for &Point {
fn mul(self, rhs: f64) -> Self::Output {
Point::new(self.0[(0, 0)] * rhs, self.0[(0, 1)] * rhs)
Point::new(self.x * rhs, self.y * rhs)
}
type Output = Point;
}
impl Div<f64> for &Point {
fn div(self, rhs: f64) -> Self::Output {
Point::new(self.0[(0, 0)] / rhs, self.0[(0, 1)] / rhs)
Point::new(self.x / rhs, self.y / rhs)
}
type Output = Point;
}
@@ -72,20 +73,12 @@ impl Neg for &Point {
impl PartialEq for Point {
fn eq(&self, other: &Self) -> bool {
self.0.eq(&other.0)
self.x.eq(&other.x) && self.y.eq(&other.y)
}
}
impl Eq for Point {}
impl Mul<&Xform> for &Point {
fn mul(self, rhs: &Xform) -> Self::Output {
Point(self.0 * rhs)
}
type Output = Point;
}
impl Clone for Point {
fn clone(&self) -> Self {
*self
@@ -96,126 +89,8 @@ impl Copy for Point {}
impl Display for Point {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
write!(f, "Point({}, {})", self.x, self.y)
}
}
#[derive(Debug, Clone)]
pub struct Rect2d {
tl: Point,
br: Point,
}
impl Rect2d {
pub fn new(tl: Point, br: Point) -> Rect2d {
Rect2d { tl, br }
}
pub fn center(&self) -> Point {
&(&self.tl + &self.br) / 2.0
}
pub fn tl(&self) -> Point {
self.tl
}
pub fn br(&self) -> Point {
self.br
}
pub fn width(&self) -> f64 {
(self.br.x() - self.tl.x()).abs()
}
pub fn height(&self) -> f64 {
(self.br.y() - self.tl.y()).abs()
}
pub fn contains(&self, point: &Point) -> bool {
self.tl().x() < point.x()
&& self.tl().y() < point.y()
&& self.br().x() > point.x()
&& self.br().y() > point.y()
}
}
impl Mul<&Xform> for &Rect2d {
fn mul(self, rhs: &Xform) -> Self::Output {
Rect2d {
tl: Point(self.tl.0 * rhs),
br: Point(self.br.0 * rhs),
}
}
type Output = Rect2d;
}
pub fn rot(alpha: f64) -> Xform {
let sa = alpha.sin();
let ca = alpha.cos();
Xform::new(|position| match position {
(0, 0) => ca,
(1, 1) => ca,
(1, 0) => -sa,
(0, 1) => sa,
(2, 2) => 1f64,
_ => 0f64,
})
}
impl Point {}
pub fn scale(x: f64, y: f64) -> Xform {
Xform::new(|position| match position {
(0, 0) => x,
(1, 1) => y,
(2, 2) => 1f64,
_ => 0f64,
})
}
pub fn xlate(x: f64, y: f64) -> Xform {
Xform::new(|position| match position {
(0, 0) => 1f64,
(1, 1) => 1f64,
(2, 2) => 1f64,
(2, 0) => x,
(2, 1) => y,
_ => 0f64,
})
}
#[cfg(test)]
mod tests {
use std::f64::consts::PI;
use super::Point;
use super::rot;
use super::scale;
use super::xlate;
#[test]
fn test_xlate() {
let p = Point::new(1.0, 3.0);
let xform = xlate(-1.0, 0.0);
let p2 = &(&(&p * &xform) * &rot(-PI / 2.0)) * &xlate(-2.0, 3.0);
assert!(p == p2);
}
#[test]
fn test_rotate() {
let p = Point::new(0.0, 3.0);
let p2 = &p * &rot(-PI / 2.0);
assert!((p2.x() - 3.0).abs() < 1e-3);
assert!((p2.y() - 0.0).abs() < 1e-3);
}
#[test]
fn test_scale() {
let p = Point::new(1.0, 3.0);
let p2 = &p * &scale(2.0, 3.0);
assert!((p2.x() - 2.0).abs() < 1e-3);
assert!((p2.y() - 9.0).abs() < 1e-3);
}
}