pub trait Projective<T>{
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
§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§
Provided Methods§
Sourcefn translate(&mut self, x: &T, y: &T)
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}Sourcefn translate_x(&mut self, x: &T)
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}Sourcefn translate_y(&mut self, y: &T)
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}Sourcefn rotate(&mut self, angle: &T)
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}Sourcefn rotate_point(&mut self, point: &[&T; 2], angle: &T)
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}Sourcefn scale(&mut self, x: &T, y: &T)
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}Sourcefn skew(&mut self, a: &T, b: &T)
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}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.