Ensure polygon has at least three points

master
Simon Bruder 2022-12-27 23:16:57 +01:00
parent b586e7ed6b
commit eacbfc4c3b
Signed by: simon
GPG Key ID: 8D3C82F9F309F8EC
1 changed files with 7 additions and 5 deletions

View File

@ -44,18 +44,20 @@ instance Show PolygonPath where
show (PolygonPath []) = ""
-- | 'regularPolygonMod' @n@ @rf@ @pos@
-- creates a regular @n@-gon
-- creates a regular @n@-gon (@n@ must be greater than or equal to 3)
-- with the centre at @pos@
-- that is modified in the sense that it takes @rf@,
-- which is used to set the radius for each individual point.
regularPolygonMod :: Integer -> (Integer -> Double) -> Vertex -> PolygonPath
regularPolygonMod n rf pos = PolygonPath (map (\step -> move pos $ scale (rf step) $ fromAngle (fromInteger step * 2 * pi / fromInteger n)) [0 .. (n - 1)])
regularPolygonMod :: Integer -> (Integer -> Double) -> Vertex -> Maybe PolygonPath
regularPolygonMod n rf pos
| n < 3 = Nothing
| otherwise = Just (PolygonPath (map (\step -> move pos $ scale (rf step) $ fromAngle (fromInteger step * 2 * pi / fromInteger n)) [0 .. (n - 1)]))
-- | 'regularPolygon' @n@ @r@ @pos@
-- creates a regular polygon
-- with the radius @r@
-- and the centre at @pos@.
regularPolygon :: Integer -> Double -> Vertex -> PolygonPath
regularPolygon :: Integer -> Double -> Vertex -> Maybe PolygonPath
regularPolygon n r = regularPolygonMod n (const r)
-- | 'star' @n@ @r1@ @r2@ @pos@
@ -64,7 +66,7 @@ regularPolygon n r = regularPolygonMod n (const r)
-- the inner radius @r1@,
-- the outer radius @r2@
-- and the centre @pos@.
star :: Integer -> Double -> Double -> Vertex -> PolygonPath
star :: Integer -> Double -> Double -> Vertex -> Maybe PolygonPath
star n r1 r2 = regularPolygonMod (n * 2) ((take (fromInteger n * 2) (cycle [r1, r2]) !!) . fromInteger)
main :: IO ()