pub struct Shape { /* private fields */ }
Expand description
A representation of a regular polygon (all angles and sides are equal).
Implementations§
Source§impl Shape
impl Shape
Sourcepub fn new(sides: i32, fill: Color, stroke: Color) -> Result<Shape>
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
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}
Sourcepub fn adjacent(
&self,
sides: i32,
edge: usize,
fill: Color,
stroke: Color,
) -> Result<Shape>
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.
Sourcepub fn render_edge_labels(&self, context: &Context, margin: f64) -> Result<()>
pub fn render_edge_labels(&self, context: &Context, margin: f64) -> Result<()>
Renders the index of each edge as an edge label.
Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more