Matrix

Struct Matrix 

Source
pub struct Matrix { /* private fields */ }
Expand description

A transformation matrix for the pdf graphics state.

Matrixes can be created with numerous named constructors and combined by multiplication.

§Examples

canvas.concat(Matrix::translate(10.0, 24.0))?;

// Matrixes can be combined by multiplication:
canvas.concat(Matrix::translate(7.0, 0.0) * Matrix::rotate_deg(45.0))?;
// ... will be visualy identical to:
canvas.concat(Matrix::translate(7.0, 0.0))?;
canvas.concat(Matrix::rotate_deg(45.0))?;

Implementations§

Source§

impl Matrix

Source

pub fn translate(dx: f32, dy: f32) -> Self

Construct a matrix for translation

Examples found in repository?
examples/mandala.rs (line 19)
10fn main() {
11    // Open our pdf document.
12    let mut document = Pdf::create("mandala.pdf").expect("Create PDF file");
13    let mut args = env::args().skip(1);
14    let n: u8 = args.next().map(|s| s.parse().expect("number")).unwrap_or(7);
15
16    // Render a page with something resembling a mandala on it.
17    document
18        .render_page(600.0, 600.0, |c| {
19            c.concat(Matrix::translate(300., 300.))?;
20            c.set_stroke_color(Color::gray(0))?;
21            let segment = 2. * PI / n as f32;
22            for _i in 0..n {
23                c.move_to(0., 33.5)?;
24                c.line_to(0., 250.)?;
25                let r = 99.;
26                c.circle(0., r, r * 1.25 * segment)?;
27                let d = 141.4;
28                let rr = 36.;
29                c.circle(0., d + rr, rr)?;
30                c.stroke()?;
31                c.concat(Matrix::rotate(segment))?;
32            }
33            c.concat(Matrix::rotate(segment / 2.))?;
34            for _i in 0..n {
35                let mut r0 = 58.66;
36                let mut r = 0.7705 * r0 * segment;
37                for _j in 0..(n + 1) / 3 {
38                    c.circle(0., r0, r)?;
39                    let r2 = 1.058 * r;
40                    r0 = r0 + r + r2;
41                    r = r2;
42                }
43                c.stroke()?;
44                c.concat(Matrix::rotate(segment))?;
45            }
46
47            Ok(())
48        })
49        .unwrap();
50    document.finish().unwrap();
51}
Source

pub fn rotate(a: f32) -> Self

Construct a matrix for rotating by a radians.

Examples found in repository?
examples/mandala.rs (line 31)
10fn main() {
11    // Open our pdf document.
12    let mut document = Pdf::create("mandala.pdf").expect("Create PDF file");
13    let mut args = env::args().skip(1);
14    let n: u8 = args.next().map(|s| s.parse().expect("number")).unwrap_or(7);
15
16    // Render a page with something resembling a mandala on it.
17    document
18        .render_page(600.0, 600.0, |c| {
19            c.concat(Matrix::translate(300., 300.))?;
20            c.set_stroke_color(Color::gray(0))?;
21            let segment = 2. * PI / n as f32;
22            for _i in 0..n {
23                c.move_to(0., 33.5)?;
24                c.line_to(0., 250.)?;
25                let r = 99.;
26                c.circle(0., r, r * 1.25 * segment)?;
27                let d = 141.4;
28                let rr = 36.;
29                c.circle(0., d + rr, rr)?;
30                c.stroke()?;
31                c.concat(Matrix::rotate(segment))?;
32            }
33            c.concat(Matrix::rotate(segment / 2.))?;
34            for _i in 0..n {
35                let mut r0 = 58.66;
36                let mut r = 0.7705 * r0 * segment;
37                for _j in 0..(n + 1) / 3 {
38                    c.circle(0., r0, r)?;
39                    let r2 = 1.058 * r;
40                    r0 = r0 + r + r2;
41                    r = r2;
42                }
43                c.stroke()?;
44                c.concat(Matrix::rotate(segment))?;
45            }
46
47            Ok(())
48        })
49        .unwrap();
50    document.finish().unwrap();
51}
Source

pub fn rotate_deg(a: f32) -> Self

Construct a matrix for rotating by a degrees.

Source

pub fn scale(sx: f32, sy: f32) -> Self

Construct a matrix for scaling by factor sx in x-direction and by sy in y-direction.

Source

pub fn uniform_scale(s: f32) -> Self

Construct a matrix for scaling by the same factor, s in both directions.

Source

pub fn skew(a: f32, b: f32) -> Self

Construct a matrix for skewing.

Trait Implementations§

Source§

impl Display for Matrix

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Mul for Matrix

Source§

type Output = Matrix

The resulting type after applying the * operator.
Source§

fn mul(self, b: Self) -> Self

Performs the * operation. Read more

Auto Trait Implementations§

§

impl Freeze for Matrix

§

impl RefUnwindSafe for Matrix

§

impl Send for Matrix

§

impl Sync for Matrix

§

impl Unpin for Matrix

§

impl UnwindSafe for Matrix

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.