Ensure polygon has at least three points

This commit is contained in:
Simon Bruder 2022-12-27 23:16:57 +01:00
parent b586e7ed6b
commit eacbfc4c3b
Signed by: simon
GPG key ID: 8D3C82F9F309F8EC

View file

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