A bit more polygon docs

This commit is contained in:
Laurenz 2023-03-28 16:57:18 +02:00
parent 44165d09a9
commit d1ff94a3b5
5 changed files with 53 additions and 37 deletions

View File

@ -6,7 +6,10 @@ description: |
# Changelog # Changelog
## Unreleased ## Unreleased
- Reduced maximum function call depth from 256 to 64. - Added [`polygon`]($func/polygon) function
- Reduced maximum function call depth from 256 to 64
- CLI now returns with non-zero status code if there is an error
- CLI now watches the root directory instead of the current one
## March 28, 2023 ## March 28, 2023
- **Breaking:** Enumerations now require a space after their marker, that is, - **Breaking:** Enumerations now require a space after their marker, that is,

View File

@ -2,10 +2,10 @@
mod image; mod image;
mod line; mod line;
mod shape;
mod polygon; mod polygon;
mod shape;
pub use self::image::*; pub use self::image::*;
pub use self::line::*; pub use self::line::*;
pub use self::shape::*;
pub use self::polygon::*; pub use self::polygon::*;
pub use self::shape::*;

View File

@ -1,10 +1,19 @@
use crate::prelude::*; use crate::prelude::*;
/// A closed-path polygon. /// A closed polygon.
///
/// The polygon is defined by its corner points and is closed automatically.
/// ///
/// ## Example /// ## Example
/// ```example /// ```example
/// #polygon(fill: blue, (0pt, 0pt), (10pt, 0pt), (10pt, 10pt)) /// #polygon(
/// fill: red,
/// stroke: 2pt + black,
/// (0pt, 0pt),
/// (50%, 0pt),
/// (50%, 4cm),
/// (20%, 4cm),
/// )
/// ``` /// ```
/// ///
/// Display: Polygon /// Display: Polygon
@ -13,6 +22,9 @@ use crate::prelude::*;
pub struct PolygonElem { pub struct PolygonElem {
/// How to fill the polygon. See the /// How to fill the polygon. See the
/// [rectangle's documentation]($func/rect.fill) for more details. /// [rectangle's documentation]($func/rect.fill) for more details.
///
/// Currently all polygons are filled according to the
/// [non-zero winding rule](https://en.wikipedia.org/wiki/Nonzero-rule).
pub fill: Option<Paint>, pub fill: Option<Paint>,
/// How to stroke the polygon. See the [lines's /// How to stroke the polygon. See the [lines's
@ -21,7 +33,8 @@ pub struct PolygonElem {
#[fold] #[fold]
pub stroke: Option<PartialStroke>, pub stroke: Option<PartialStroke>,
/// The vertices of the polygon. The polygon automatically closes itself. /// The vertices of the polygon. Each point is specified as an array of two
/// [relative lengths]($type/relative-length).
#[variadic] #[variadic]
pub vertices: Vec<Axes<Rel<Length>>>, pub vertices: Vec<Axes<Rel<Length>>>,
} }
@ -45,20 +58,19 @@ impl Layout for PolygonElem {
.collect(); .collect();
let size = points.iter().fold(Point::zero(), |max, c| c.max(max)).to_size(); let size = points.iter().fold(Point::zero(), |max, c| c.max(max)).to_size();
let target = regions.expand.select(regions.size, size); let target = regions.expand.select(regions.size, size);
let mut frame = Frame::new(target); let mut frame = Frame::new(target);
// only create a path if there is more than zero points. // Only create a path if there are more than zero points.
if points.len() > 0 { if points.len() > 0 {
let stroke = self.stroke(styles).map(|e| e.unwrap_or_default());
let fill = self.fill(styles); let fill = self.fill(styles);
let stroke = self.stroke(styles).map(PartialStroke::unwrap_or_default);
// construct a closed path given all points. // Construct a closed path given all points.
let mut path = Path::new(); let mut path = Path::new();
path.move_to(points[0]); path.move_to(points[0]);
for point in &points[1..] { for &point in &points[1..] {
path.line_to(*point); path.line_to(point);
} }
path.close_path(); path.close_path();

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.4 KiB

After

Width:  |  Height:  |  Size: 1.9 KiB

View File

@ -1,31 +1,32 @@
// Test polygons. // Test polygons.
--- ---
#set page(height: 220pt, width: 50pt) #set page(width: 50pt)
#box({ #set polygon(stroke: 0.75pt, fill: blue)
set polygon(stroke: 0.75pt, fill: blue)
polygon((0em, 0pt)) // These are not visible, but should also not give an error
// this should not give an error #polygon()
polygon() #polygon((0em, 0pt))
polygon((0pt, 0pt), (10pt, 0pt)) #polygon((0pt, 0pt), (10pt, 0pt))
polygon((5pt, 0pt), (0pt, 10pt), (10pt, 10pt))
polygon( #polygon((5pt, 0pt), (0pt, 10pt), (10pt, 10pt))
#polygon(
(0pt, 0pt), (5pt, 5pt), (10pt, 0pt), (0pt, 0pt), (5pt, 5pt), (10pt, 0pt),
(15pt, 5pt), (15pt, 5pt),
(5pt, 10pt) (5pt, 10pt)
) )
polygon(stroke: none, (5pt, 0pt), (0pt, 10pt), (10pt, 10pt)) #polygon(stroke: none, (5pt, 0pt), (0pt, 10pt), (10pt, 10pt))
polygon(stroke: 3pt, fill: none, (5pt, 0pt), (0pt, 10pt), (10pt, 10pt)) #polygon(stroke: 3pt, fill: none, (5pt, 0pt), (0pt, 10pt), (10pt, 10pt))
// relative size
polygon((0pt, 0pt), (100%, 5pt), (50%, 10pt)) // Relative size
// antiparallelogram #polygon((0pt, 0pt), (100%, 5pt), (50%, 10pt))
polygon((0pt, 5pt), (5pt, 0pt), (0pt, 10pt), (5pt, 15pt))
// self-intersections // Antiparallelogram
polygon((0pt, 10pt), (30pt, 20pt), (0pt, 30pt), (20pt, 0pt), (20pt, 35pt)) #polygon((0pt, 5pt), (5pt, 0pt), (0pt, 10pt), (5pt, 15pt))
})
// Self-intersections
#polygon((0pt, 10pt), (30pt, 20pt), (0pt, 30pt), (20pt, 0pt), (20pt, 35pt))
--- ---
// Test errors.
// Error: 10-17 point array must contain exactly two entries // Error: 10-17 point array must contain exactly two entries
#polygon((50pt,)) #polygon((50pt,))