Initial commit
This commit is contained in:
221
rdraught-ui/src/geo2d.rs
Normal file
221
rdraught-ui/src/geo2d.rs
Normal file
@@ -0,0 +1,221 @@
|
||||
use rmath::SMatrix;
|
||||
use std::clone::Clone;
|
||||
use std::cmp::Eq;
|
||||
use std::cmp::PartialEq;
|
||||
use std::fmt::Display;
|
||||
use std::marker::Copy;
|
||||
use std::ops::Add;
|
||||
use std::ops::Div;
|
||||
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>);
|
||||
|
||||
impl Point {
|
||||
pub fn x(&self) -> f64 {
|
||||
self.0[(0, 0)]
|
||||
}
|
||||
|
||||
pub fn y(&self) -> f64 {
|
||||
self.0[(0, 1)]
|
||||
}
|
||||
|
||||
pub fn new(x: f64, y: f64) -> Point {
|
||||
Point(SMatrix::new(|pos| match pos {
|
||||
(0, 0) => x,
|
||||
(0, 1) => y,
|
||||
(0, 2) => 1f64,
|
||||
_ => 0f64,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
impl Add<&Point> for &Point {
|
||||
fn add(self, rhs: &Point) -> Self::Output {
|
||||
self * &xlate(rhs.x(), rhs.y())
|
||||
}
|
||||
type Output = Point;
|
||||
}
|
||||
|
||||
impl Sub<&Point> for &Point {
|
||||
fn sub(self, rhs: &Point) -> Self::Output {
|
||||
self * &xlate(-rhs.x(), -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)
|
||||
}
|
||||
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)
|
||||
}
|
||||
type Output = Point;
|
||||
}
|
||||
|
||||
impl Neg for &Point {
|
||||
fn neg(self) -> Self::Output {
|
||||
Point::new(-self.x(), -self.y())
|
||||
}
|
||||
|
||||
type Output = Point;
|
||||
}
|
||||
|
||||
impl PartialEq for Point {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.0.eq(&other.0)
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
impl Copy for Point {}
|
||||
|
||||
impl Display for Point {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
self.0.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
#[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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user