Struct Shape

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

A representation of a regular polygon (all angles and sides are equal).

Implementations§

Source§

impl Shape

Source

pub fn new(sides: i32, fill: Color, stroke: Color) -> Result<Shape>

Returns a new shape, ensuring the number of sides is at least three.

Examples found in repository?
examples/examples.rs (line 46)
38    fn render(
39        background: Color,
40        stroke: Color,
41        fill_0: Color,
42        fill_1: Color,
43        fill_2: Color,
44    ) -> Result<()> {
45        let mut model = Model::new(WIDTH, HEIGHT, SCALE);
46        model.add(Shape::new(6, fill_1, stroke)?);
47        let a = model.add_multi(0..1, 0..6, Shape::new(3, fill_0, stroke)?)?;
48        let b = model.add_multi(a, 1..2, Shape::new(6, fill_1, stroke)?)?;
49        model.repeat(b)?;
50
51        model
52            .render(background, MARGIN, LINE_WIDTH, SHOW_LABELS)?
53            .write_to_png("3.6.3.6.png")?;
54        model
55            .render_dual(background, fill_0, stroke, MARGIN, LINE_WIDTH)?
56            .write_to_png("3.6.3.6-dual.png")?;
57
58        Ok(())
59    }
60}
61
62struct Ex33434();
63
64impl Example for Ex33434 {
65    fn render(
66        background: Color,
67        stroke: Color,
68        fill_0: Color,
69        fill_1: Color,
70        fill_2: Color,
71    ) -> Result<()> {
72        let mut model = Model::new(WIDTH, HEIGHT, SCALE);
73        model.add(Shape::new(4, fill_1, stroke)?);
74        let a = model.add_multi(0..1, 0..4, Shape::new(3, fill_2, stroke)?)?;
75        let b = model.add_multi(a, 1..2, Shape::new(4, fill_1, stroke)?)?;
76        let c = model.add_multi(b, 2..4, Shape::new(3, fill_2, stroke)?)?;
77        let d = model.add_multi(c, 2..3, Shape::new(4, fill_1, stroke)?)?;
78        model.repeat(d)?;
79
80        model
81            .render(background, MARGIN, LINE_WIDTH, SHOW_LABELS)?
82            .write_to_png("3.3.4.3.4.png")?;
83        model
84            .render_dual(background, fill_0, stroke, MARGIN, LINE_WIDTH)?
85            .write_to_png("3.3.4.3.4-dual.png")?;
86
87        Ok(())
88    }
89}
90
91struct Ex33336();
92
93impl Example for Ex33336 {
94    fn render(
95        background: Color,
96        stroke: Color,
97        fill_0: Color,
98        fill_1: Color,
99        fill_2: Color,
100    ) -> Result<()> {
101        let mut model = Model::new(WIDTH, HEIGHT, SCALE);
102        model.add(Shape::new(6, fill_2, stroke)?);
103        let a = model.add_multi(0..1, 0..6, Shape::new(3, fill_0, stroke)?)?;
104        let b = model.add_multi(a.clone(), 1..2, Shape::new(3, fill_0, stroke)?)?;
105        let c = model.add_multi(a.clone(), 2..3, Shape::new(3, fill_0, stroke)?)?;
106        let d = model.add_multi(c, 1..2, Shape::new(6, fill_2, stroke)?)?;
107        model.repeat(d)?;
108
109        model
110            .render(background, MARGIN, LINE_WIDTH, SHOW_LABELS)?
111            .write_to_png("3.3.3.3.6.png")?;
112        model
113            .render_dual(background, fill_0, stroke, MARGIN, LINE_WIDTH)?
114            .write_to_png("3.3.3.3.6-dual.png")?;
115
116        Ok(())
117    }
118}
119
120struct Ex333333();
121
122impl Example for Ex333333 {
123    fn render(
124        background: Color,
125        stroke: Color,
126        fill_0: Color,
127        fill_1: Color,
128        fill_2: Color,
129    ) -> Result<()> {
130        let mut model = Model::new(WIDTH, HEIGHT, SCALE);
131        model.add(Shape::new(3, fill_2, stroke)?);
132        let a = model.add_multi(0..1, 0..3, Shape::new(3, fill_1, stroke)?)?;
133        let b = model.add_multi(a, 1..3, Shape::new(3, fill_2, stroke)?)?;
134        model.repeat(b)?;
135
136        model
137            .render(background, MARGIN, LINE_WIDTH, SHOW_LABELS)?
138            .write_to_png("3.3.3.3.3.3.png")?;
139        model
140            .render_dual(background, fill_0, stroke, MARGIN, LINE_WIDTH)?
141            .write_to_png("3.3.3.3.3.3-dual.png")?;
142
143        Ok(())
144    }
More examples
Hide additional examples
examples/intro.rs (line 20)
3pub fn main() -> Result<()> {
4    let width = 1024;
5    let height = 1024;
6    let scale = 128.0;
7    let stroke = Color::new(242, 60, 60)?;
8    let fill_hexagon = Color::new(242, 194, 106)?;
9    let fill_square = Color::new(23, 216, 146)?;
10    let fill_triangle = Color::new(242, 209, 48)?;
11    let background = Color::new(242, 242, 242)?;
12    let margin = 0.1;
13    let show_labels = false;
14    let line_width = 0.1;
15
16    // create an empty model
17    let mut model = Model::new(width, height, scale);
18
19    // add a hexagon
20    model.add(Shape::new(6, fill_hexagon, stroke)?);
21
22    // attach a square to each side of the hexagon
23    let squares = model.add_multi(0..1, 0..6, Shape::new(4, fill_square, stroke)?)?;
24
25    // attach a triangle between the squares
26    let _ = model.add_multi(squares.clone(), 1..2, Shape::new(3, fill_triangle, stroke)?)?;
27
28    // attach a hexagon to the outer edge of each square
29    let hexagons = model.add_multi(squares.clone(), 2..3, Shape::new(6, fill_hexagon, stroke)?)?;
30
31    // fill the surface with the pattern
32    model.repeat(hexagons)?;
33
34    // render the tiling
35    let render = model.render(background, margin, line_width, show_labels)?;
36    render.write_to_png("intro.png")?;
37
38    // render the dual tiling
39    let render_dual = model.render_dual(background, fill_hexagon, stroke, margin, line_width)?;
40    render_dual.write_to_png("intro-dual.png")?;
41
42    Ok(())
43}
Source

pub fn sides(&self) -> i32

Returns the shape’s sides.

Source

pub fn point(&self) -> Point

Returns the shape’s point.

Source

pub fn rotation(&self) -> f64

Returns the shape’s rotation.

Source

pub fn fill(&self) -> Color

Returns the shape’s fill.

Source

pub fn stroke(&self) -> Color

Returns the shape’s stroke.

Source

pub fn adjacent( &self, sides: i32, edge: usize, fill: Color, stroke: Color, ) -> Result<Shape>

Returns the sides-sided shape adjacent to the edge with index edge.

Source

pub fn render_edge_labels(&self, context: &Context, margin: f64) -> Result<()>

Renders the index of each edge as an edge label.

Source

pub fn render_label(&self, context: &Context, text: &str) -> Result<()>

Renders text as the shape’s label.

Source

pub fn clone_at(&self, point: Point) -> Shape

Returns a copy of the shape centered at point.

Trait Implementations§

Source§

impl Clone for Shape

Source§

fn clone(&self) -> Shape

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Shape

Source§

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

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

impl Polygon for Shape

Source§

fn points(&self, margin: f64) -> Result<Vec<Point>>

Returns the polygon’s points.

Source§

fn render(&self, context: &Context, margin: f64) -> Result<()>

Renders the polygon.

Source§

impl Copy for Shape

Auto Trait Implementations§

§

impl Freeze for Shape

§

impl RefUnwindSafe for Shape

§

impl Send for Shape

§

impl Sync for Shape

§

impl Unpin for Shape

§

impl UnwindSafe for Shape

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.