Skip to main content

Polygon

Struct Polygon 

Source
pub struct Polygon<I> {
    pub sides_fn: IndexFn<I, u8>,
    pub size_fn: IndexFn<I, f64>,
    pub rotation_fn: IndexFn<I, Angle>,
    pub color_fn: IndexFn<I, Color>,
    pub cv_fn: Option<IndexFn<I, f64>>,
}
Expand description

A polygonal shape with rounded corners

Its fields are defined as functions of Index. This allows the polygon to be rendered dynamically depending on where it is in the image.

Fields§

§sides_fn: IndexFn<I, u8>

The number of sides in the polygon

§size_fn: IndexFn<I, f64>

The size of the polygon

This is the diameter of the circle that the vertices of the polygon would lie on.

§rotation_fn: IndexFn<I, Angle>

The rotation of the polygon

With no rotation, the shape is rendered “pointy side up”.

§color_fn: IndexFn<I, Color>

The color of the polygon

§cv_fn: Option<IndexFn<I, f64>>

The coefficient of variance used when randomizing the shape of the polygon

Implementations§

Source§

impl<I> Polygon<I>

Source

pub fn builder() -> PolygonBuilder<I>

Create an instance of Polygon using the builder syntax

Examples found in repository?
examples/gradation.rs (line 22)
8fn main() {
9    let background_color = "#ddd".try_into().unwrap();
10    let mut canvas_builder = Canvas::builder()
11        .size(2560.0, 1440.0)
12        .background_color(background_color)
13        .points(
14            Lattice::rectangular_builder()
15                .grid_size(7, 5)
16                .len_a(300.0)
17                .len_b(200.0)
18                .build(),
19        );
20
21    canvas_builder.add_shape(
22        Polygon::builder()
23            .sides_fn(|Index { u, .. }| *u as u8 + 3)
24            .size_fn(|Index { v, .. }| *v as f64 * 35.0 + 80.0)
25            .color_fn(|Index { u, v }| Color::new((*u + 1) as u8 * 25, 0, (*v + 1) as u8 * 25, 255))
26            .build(),
27    );
28
29    let canvas = canvas_builder.build();
30    let document = canvas.render(|_| true);
31    svg::save("examples/gradation.svg", &document).unwrap();
32}
More examples
Hide additional examples
examples/kanoko.rs (line 17)
4fn main() {
5    let background_color = "#002e4e".try_into().unwrap();
6    let mut canvas_builder = Canvas::builder()
7        .size(2560.0, 1440.0)
8        .background_color(background_color)
9        .points(
10            Lattice::diamond_builder()
11                .grid_size(19, 16)
12                .len_a(90.0)
13                .build(),
14        );
15
16    canvas_builder.add_shape(
17        Polygon::builder()
18            .sides(4)
19            .size(70.0)
20            .color("#f5f5fa".try_into().unwrap())
21            .build(),
22    );
23    canvas_builder.add_shape(
24        Polygon::builder()
25            .sides(4)
26            .size(35.0)
27            .color(background_color)
28            .build(),
29    );
30
31    let canvas = canvas_builder.build();
32    let document = canvas.render(|_| true);
33    svg::save("examples/kanoko.svg", &document).unwrap();
34}
examples/sunflower.rs (line 16)
4fn main() {
5    let points = Vogel::golden_builder()
6        .seeds(256)
7        .scaling_factor(50.0)
8        .build();
9    let mut canvas = Canvas::builder()
10        .size(2560.0, 1440.0)
11        .background_color("#fcba03".try_into().unwrap())
12        .points(points)
13        .build();
14
15    let polygon_builder = || {
16        Polygon::builder()
17            .sides(4)
18            .rotation_fn(move |n: &usize| Angle::Radian(*n as f64 * points.angle.to_radian()))
19    };
20    canvas.add_shape(
21        polygon_builder()
22            .size(100.0)
23            .color("#3d2f06".try_into().unwrap())
24            .build(),
25    );
26    canvas.add_shape(
27        polygon_builder()
28            .size(20.0)
29            .color("#dedbd5".try_into().unwrap())
30            .build(),
31    );
32
33    let document = canvas.render(|_| true);
34    svg::save("examples/sunflower.svg", &document).unwrap();
35}
examples/placemat.rs (line 24)
5fn main() {
6    let mut rng = rand::rng();
7
8    let lattice = Lattice::builder()
9        .grid_size(rng.random_range(5..50), rng.random_range(1..50))
10        .len_a(rng.random_range(100.0..400.0))
11        .len_b(rng.random_range(100.0..400.0))
12        .theta(Angle::Degree(rng.random_range(0.0..90.0)))
13        .build();
14    let mut canvas_builder = Canvas::builder()
15        .size(2560.0, 1440.0)
16        .background_color("#e0d8d1".try_into().unwrap())
17        .points(lattice);
18
19    let size = rng
20        .random_range(100.0..lattice.len_a)
21        .min(rng.random_range(100.0..lattice.len_b) * lattice.theta.to_radian().sin());
22
23    canvas_builder.add_shape(
24        Polygon::builder()
25            .sides_fn(|_| rand::rng().random_range(3..8))
26            .size(size)
27            .color_fn(|_| {
28                vec![
29                    "#6f6e6a", "#b3b4af", "#b09e90", "#bea24e", "#d9bdb9", "#9a9ba0",
30                ]
31                .choose(&mut rand::rng())
32                .copied()
33                .unwrap()
34                .try_into()
35                .unwrap()
36            })
37            .cv_fn(|_| rand::rng().random_range(0.1..0.5) / 6.0)
38            .build(),
39    );
40
41    let canvas = canvas_builder.build();
42
43    let document = canvas.render(|_| rand::rng().random_bool(0.9));
44    svg::save("examples/placemat.svg", &document).unwrap();
45}
examples/nazar.rs (line 18)
5fn main() {
6    let size = 144.0;
7    let mut canvas_builder = Canvas::builder()
8        .size(2560.0, 1440.0)
9        .background_color("#fff".try_into().unwrap())
10        .points(
11            PoissonDisk::builder()
12                .size(2560.0, 1440.0)
13                .r(size)
14                .k(30)
15                .build(),
16        );
17
18    let polygon_builder = || Polygon::builder().sides(7).cv(0.05);
19    canvas_builder.add_shape(
20        polygon_builder()
21            .size(size)
22            .color("#070d97".try_into().unwrap())
23            .build(),
24    );
25    canvas_builder.add_shape(
26        polygon_builder()
27            .size(size * 3.0 / 4.0)
28            .color("#fff".try_into().unwrap())
29            .build(),
30    );
31    canvas_builder.add_shape(
32        polygon_builder()
33            .size(size / 2.0)
34            .color_fn(|_| {
35                let colors = vec!["#73bff1", "#daa520"];
36                let weights = vec![15, 1];
37
38                let dist = WeightedIndex::new(&weights).unwrap();
39
40                colors[dist.sample(&mut rand::rng())].try_into().unwrap()
41            })
42            .build(),
43    );
44    canvas_builder.add_shape(
45        polygon_builder()
46            .size(size / 4.0)
47            .color("#000".try_into().unwrap())
48            .build(),
49    );
50
51    let canvas = canvas_builder.build();
52    let document = canvas.render(|_| true);
53    svg::save("examples/nazar.svg", &document).unwrap();
54}
Source§

impl<I> Polygon<I>

Source

pub fn new( sides_fn: impl Fn(&I) -> u8 + 'static, size_fn: impl Fn(&I) -> f64 + 'static, rotation_fn: impl Fn(&I) -> Angle + 'static, color_fn: impl Fn(&I) -> Color + 'static, cv_fn: Option<impl Fn(&I) -> f64 + 'static>, ) -> Self

Define a new polygon

Source

pub fn new_static( sides: u8, size: f64, rotation: Angle, color: Color, cv: Option<f64>, ) -> Self

Define a new polygon with static values (no dependency on Index)

Trait Implementations§

Source§

impl<I: Copy> Shape for Polygon<I>

Source§

type Index = I

Source§

fn generate_path_and_bb(&self, index: &Self::Index) -> (Path, BoundingBox)

Generate an SVG path for a given index Read more

Auto Trait Implementations§

§

impl<I> !RefUnwindSafe for Polygon<I>

§

impl<I> !Send for Polygon<I>

§

impl<I> !Sync for Polygon<I>

§

impl<I> !UnwindSafe for Polygon<I>

§

impl<I> Forget for Polygon<I>

§

impl<I> Freeze for Polygon<I>
where Box<dyn Fn(&I) -> u8>: Freeze, Box<dyn Fn(&I) -> f64>: Freeze, Box<dyn Fn(&I) -> Angle>: Freeze, Box<dyn Fn(&I) -> Color>: Freeze, Option<Box<dyn Fn(&I) -> f64>>: Freeze,

§

impl<I> Move for Polygon<I>

§

impl<I> Unpin for Polygon<I>
where Box<dyn Fn(&I) -> u8>: Unpin, Box<dyn Fn(&I) -> f64>: Unpin, Box<dyn Fn(&I) -> Angle>: Unpin, Box<dyn Fn(&I) -> Color>: Unpin, Option<Box<dyn Fn(&I) -> f64>>: Unpin,

§

impl<I> UnsafeUnpin for Polygon<I>
where Box<dyn Fn(&I) -> u8>: UnsafeUnpin, Box<dyn Fn(&I) -> f64>: UnsafeUnpin, Box<dyn Fn(&I) -> Angle>: UnsafeUnpin, Box<dyn Fn(&I) -> Color>: UnsafeUnpin, Option<Box<dyn Fn(&I) -> f64>>: UnsafeUnpin,

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.