add Image::poly

This commit is contained in:
bendn 2023-09-29 07:04:26 +07:00
parent c33594e995
commit e405062947
No known key found for this signature in database
GPG key ID: 0D9D3A2A3B2A93D6
4 changed files with 67 additions and 4 deletions

View file

@ -1,6 +1,6 @@
[package] [package]
name = "fimg" name = "fimg"
version = "0.4.9" version = "0.4.10"
authors = ["bend-n <bend.n@outlook.com>"] authors = ["bend-n <bend.n@outlook.com>"]
license = "MIT" license = "MIT"
edition = "2021" edition = "2021"

View file

@ -1,6 +1,7 @@
//! draw polygons //! draw polygons
use std::{ use std::{
cmp::{max, min}, cmp::{max, min},
f32::consts::TAU,
ops::DerefMut, ops::DerefMut,
}; };
@ -83,4 +84,62 @@ impl<T: DerefMut<Target = [u8]>, const CHANNELS: usize> Image<T, CHANNELS> {
) { ) {
self.points(&[a, b, c, d, a], col); self.points(&[a, b, c, d, a], col);
} }
/// Draws a regular convex polygon with a specified number of sides, a radius, and a rotation (radians).
/// Calls into [`Image::tri`] and [`Image::quad`].
/// ```
/// # use fimg::Image;
/// let mut i = Image::alloc(300, 300);
/// // draw a enneagon
/// // at x150, y150 │ unrotated white
/// // with a radius of ─┼──╮ │ │
/// i.poly((150., 150.), 9, 100.0, 0.0, [255]);
/// # assert_eq!(i.buffer(), include_bytes!("../../tdata/enneagon.imgbuf"));
/// ```
pub fn poly(
&mut self,
(x, y): (f32, f32),
sides: usize,
radius: f32,
rotation: f32,
c: [u8; CHANNELS],
) {
let trans = |a: f32| (a.cos() * radius, a.sin() * radius);
let r = |(a, b): (f32, f32)| (a.round() as i32, b.round() as i32);
let add = |(a, b)| (a + x, b + y);
match sides {
3 => {
let space = TAU / 3.0;
self.tri(
add(trans(space + rotation)),
add(trans(rotation)),
add(trans(space * 2.0 + rotation)),
c,
);
}
_ => {
let space = TAU / sides as f32;
for i in (0..sides - 1).step_by(2).map(|i| i as f32) {
self.quad(
r((x, y)),
r(add(trans(space * i + rotation))),
r(add(trans(space * (i + 1.) + rotation))),
r(add(trans(space * (i + 2.) + rotation))),
c,
);
}
if sides % 2 != 0 && sides > 4 {
let i = (sides - 1) as f32;
// the missing piece
self.tri(
(x, y),
add(trans(space * i + rotation)),
add(trans(space * (i + 1.) + rotation)),
c,
);
}
}
}
}
} }

View file

@ -8,9 +8,13 @@ impl<T: DerefMut<Target = [u8]>, const CHANNELS: usize> Image<T, CHANNELS> {
/// ``` /// ```
/// # use fimg::*; /// # use fimg::*;
/// let mut a = Image::alloc(10, 10); /// let mut a = Image::alloc(10, 10);
/// // draw a triangle from point a v point b v point c v /// // draw a triangle
/// // with color white /// a.as_mut().tri(
/// a.as_mut().tri((3.0, 2.0), (8.0, 7.0), (1.0, 8.0), [255]); /// (3.0, 2.0), // point a
/// (8.0, 7.0), // point b
/// (1.0, 8.0), // point c
/// [255] // white
/// );
/// # assert_eq!(a.buffer(), b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"); /// # assert_eq!(a.buffer(), b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00");
/// ``` /// ```
pub fn tri( pub fn tri(

BIN
tdata/enneagon.imgbuf Normal file

Binary file not shown.