Projective

Trait Projective 

Source
pub trait Projective<T>
where Self: Sized, T: Real,
{
Show 15 methods // Required method fn transform(&mut self, matrix: &[&T; 9]); // Provided methods fn translate(&mut self, x: &T, y: &T) { ... } fn translate_x(&mut self, x: &T) { ... } fn translate_y(&mut self, y: &T) { ... } fn rotate(&mut self, angle: &T) { ... } fn rotate_point(&mut self, point: &[&T; 2], angle: &T) { ... } fn scale(&mut self, x: &T, y: &T) { ... } fn scale_x(&mut self, x: &T) { ... } fn scale_y(&mut self, y: &T) { ... } fn reflect(&mut self, x: &T, y: &T) { ... } fn reflect_x(&mut self) { ... } fn reflect_y(&mut self) { ... } fn skew(&mut self, a: &T, b: &T) { ... } fn skew_x(&mut self, a: &T) { ... } fn skew_y(&mut self, b: &T) { ... }
}
Expand description

2D projective transformations

§Supported 2D operations

OperationDescription
transform———–
translate———–
rotate———–
scale———–

§Examples

use projective::Projective;

#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Point(f64, f64);

#[rustfmt::skip]
impl Projective<f64> for Point {
    fn transform(&self, matrix: &[&f64; 9]) -> Self {
        Point(
             matrix[0] * self.0 + matrix[1] * self.1+ matrix[2],
             matrix[3] * self.0 + matrix[4] * self.1 +matrix[5],
        )
    }
}

#[test]
fn test_transform() {
    let p0 = Point(1.0, 2.0);
    assert_eq!(p0.translate(&2.0, &1.0), Point(3.0, 3.0));
    assert_eq!(p0.scale(&2.0, &3.0), Point(2.0, 6.0));
    // floating precision error, implement rotate manually to reduce errors
    assert_eq!(p0.rotate(&std::f64::consts::PI), Point(-0.9999999999999998, -2.0));
}

Required Methods§

Source

fn transform(&mut self, matrix: &[&T; 9])

Transform by a 3×3 matrix.

§Matrix
\begin{bmatrix}
    m_0 & m_1 & m_2 \\
    m_3 & m_4 & m_5 \\
    m_6 & m_7 & m_8 \\
\end{bmatrix}

Provided Methods§

Source

fn translate(&mut self, x: &T, y: &T)

Transform by length $\delta x, \delta y$.

§Matrix
\begin{bmatrix}
    1 & 0 & x \\
    0 & 1 & y \\
    0 & 0 & 1 \\
\end{bmatrix}
Source

fn translate_x(&mut self, x: &T)

Transform by length $\delta x$.

§Matrix
\begin{bmatrix}
    1 & 0 & x \\
    0 & 1 & 0 \\
    0 & 0 & 1 \\
\end{bmatrix}
Source

fn translate_y(&mut self, y: &T)

Transform by length $\delta y$.

§Matrix
\begin{bmatrix}
    1 & 0 & 0 \\
    0 & 1 & y \\
    0 & 0 & 1 \\
\end{bmatrix}
Source

fn rotate(&mut self, angle: &T)

Transform by rotate degree $\alpha$.

§Matrix
\begin{bmatrix}
    \cos\alpha & \sin\alpha & 0 \\
   -\sin\alpha & \cos\alpha & 0 \\
    0          & 0          & 1 \\
\end{bmatrix}
Source

fn rotate_point(&mut self, point: &[&T; 2], angle: &T)

Transform by rotate with a point $\alpha$.

§Matrix
\begin{aligned}
    x =& (x_0 - p_x)\cos α - (y_0 - p_y)\sin α + p_x \\
    y =& (x_0 - p_x)\sin α + (y_0 - p_y)\cos α + p_y \\
\end{aligned}
Source

fn scale(&mut self, x: &T, y: &T)

Transform by scale $x, y$.

§Matrix
\begin{bmatrix}
    x & 0 & 0 \\
    0 & y & 0 \\
    0 & 0 & 1 \\
\end{bmatrix}
Source

fn scale_x(&mut self, x: &T)

Transform by scale $x$.

§Matrix
\begin{bmatrix}
    x & 0 & 0 \\
    0 & 1 & 0 \\
    0 & 0 & 1 \\
\end{bmatrix}
Source

fn scale_y(&mut self, y: &T)

Transform by scale $y$.

§Matrix
\begin{bmatrix}
    1 & 0 & 0 \\
    0 & y & 0 \\
    0 & 0 & 1 \\
\end{bmatrix}
Source

fn reflect(&mut self, x: &T, y: &T)

Transform by skew $x, y$.

Source

fn reflect_x(&mut self)

Transform by skew $x$.

Source

fn reflect_y(&mut self)

Transform by skew $y$.

Source

fn skew(&mut self, a: &T, b: &T)

Transform by angle $\alpha, \beta$.

§Matrix
\begin{bmatrix}
    1         & \tan\alpha & 0 \\
    \tan\beta & 1          & 0 \\
    0         & 0          & 1 \\
\end{bmatrix}
Source

fn skew_x(&mut self, a: &T)

Transform by angle $\alpha$.

§Matrix
\begin{bmatrix}
    1         & \tan\alpha & 0 \\
    \tan\beta & 1          & 0 \\
    0         & 0          & 1 \\
\end{bmatrix}
Source

fn skew_y(&mut self, b: &T)

Transform by angle $\beta$.

§Matrix
\begin{vmatrix}
    1         & \tan\alpha & 0 \\
    \tan\beta & 1          & 0 \\
    0         & 0          & 1 \\
\end{vmatrix}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§