diff --git a/src/pixie/paths.nim b/src/pixie/paths.nim index deb3d4d..97efba7 100644 --- a/src/pixie/paths.nim +++ b/src/pixie/paths.nim @@ -632,13 +632,14 @@ proc polygon*( path: Path, x, y, size: float32, sides: int ) {.raises: [PixieError].} = ## Adds an n-sided regular polygon at (x, y) with the parameter size. - if sides <= 0: + ## Polygons "face" north. + if sides <= 2: raise newException(PixieError, "Invalid polygon sides value") - path.moveTo(x + size * cos(0.0), y + size * sin(0.0)) - for side in 0 .. sides: + path.moveTo(x + size * sin(0.0), y - size * cos(0.0)) + for side in 1 .. sides: path.lineTo( - x + size * cos(side.float32 * 2.0 * PI / sides.float32), - y + size * sin(side.float32 * 2.0 * PI / sides.float32) + x + size * sin(side.float32 * 2.0 * PI / sides.float32), + y - size * cos(side.float32 * 2.0 * PI / sides.float32) ) proc polygon*( diff --git a/tests/paths/polygon3.png b/tests/paths/polygon3.png new file mode 100644 index 0000000..0fdd5aa Binary files /dev/null and b/tests/paths/polygon3.png differ diff --git a/tests/paths/polygon4.png b/tests/paths/polygon4.png new file mode 100644 index 0000000..d761edc Binary files /dev/null and b/tests/paths/polygon4.png differ diff --git a/tests/paths/polygon5.png b/tests/paths/polygon5.png new file mode 100644 index 0000000..029669f Binary files /dev/null and b/tests/paths/polygon5.png differ diff --git a/tests/paths/polygon6.png b/tests/paths/polygon6.png new file mode 100644 index 0000000..c16c400 Binary files /dev/null and b/tests/paths/polygon6.png differ diff --git a/tests/paths/polygon7.png b/tests/paths/polygon7.png new file mode 100644 index 0000000..cb54322 Binary files /dev/null and b/tests/paths/polygon7.png differ diff --git a/tests/paths/polygon8.png b/tests/paths/polygon8.png new file mode 100644 index 0000000..8358b88 Binary files /dev/null and b/tests/paths/polygon8.png differ diff --git a/tests/test_paths.nim b/tests/test_paths.nim index 626fb7e..482d29d 100644 --- a/tests/test_paths.nim +++ b/tests/test_paths.nim @@ -690,3 +690,12 @@ block: mask = newMask(100, 100) pathStr = "M0 0 L0 1 L0 0 Z" mask.fillPath(pathStr) + +block: + # Test different polygons. + for i in 3 .. 8: + let path = newPath() + path.polygon(vec2(50, 50), 30, i) + let mask = newMask(100, 100) + mask.fillPath(path) + mask.writeFile(&"tests/paths/polygon{i}.png")