Skip to content

Latest commit

 

History

History
42 lines (28 loc) · 1.67 KB

drawing_polygons.md

File metadata and controls

42 lines (28 loc) · 1.67 KB

Drawing Polygons

Rectangles in the previous tutorial has fixed number of corners. To draw a more flexible shape, we can use draw_polygon_mut. It needs all the corners (each is described by a Point) of the shape

use imageproc::{drawing, image, point::Point};

fn main() {
    let mut buf = image::ImageBuffer::new(100, 100);

    let points = [
        Point::new(50, 25),
        Point::new(75, 42),
        Point::new(65, 75),
        Point::new(35, 75),
        Point::new(25, 42),
    ];
    drawing::draw_polygon_mut(&mut buf, &points, image::Rgb::from([128u8, 255u8, 64u8]));

    buf.save("polygon.png").unwrap();
}

polygon.png:

polygon

To only draw the border, we can use draw_hollow_polygon_mut.

polygon_hollow

We can also draw an antialiased polygon by draw_antialiased_polygon_mut.

antialiased_polygon

To draw on a copied image, we can use draw_polygon, draw_hollow_polygon and draw_antialiased_polygon.

➡️ Next: Drawing Circles

📘 Back: Table of contents