Ensure polygon has at least three points
This commit is contained in:
parent
b586e7ed6b
commit
eacbfc4c3b
|
@ -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 ()
|
||||
|
|
Reference in a new issue