{-# LANGUAGE OverloadedStrings #-}
module Core.Geometry
( regularPolygon
, starPolygonFirstSpecies
, starOutline
, starFat
, starRegular
) where
import Text.Blaze.Svg11 ((!))
import Text.Blaze.Svg11 as S
import Text.Blaze.Svg11.Attributes as A
import Core.Utils
regularPolygon
:: Int
-> Float
-> (Float , Float)
-> Svg
regularPolygon :: Int -> Float -> (Float, Float) -> Svg
regularPolygon Int
n Float
r (Float
x0,Float
y0) =
Svg
S.path
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.d AttributeValue
directions
where
α :: Float
α = Float
2 forall a. Num a => a -> a -> a
* forall a. Floating a => a
pi forall a. Fractional a => a -> a -> a
/ (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n)
draw :: Float -> Path
draw Float
k =
forall a. Show a => a -> a -> Path
l (Float
x0 forall a. Num a => a -> a -> a
+ Float
r forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
sin (Float
kforall a. Num a => a -> a -> a
*Float
α))
(Float
y0 forall a. Num a => a -> a -> a
- Float
r forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
cos (Float
kforall a. Num a => a -> a -> a
*Float
α))
directions :: AttributeValue
directions =
Path -> AttributeValue
mkPath forall a b. (a -> b) -> a -> b
$ do
forall a. Show a => a -> a -> Path
m Float
x0 (Float
y0 forall a. Num a => a -> a -> a
- Float
r)
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Float -> Path
draw forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral) [Int
1..Int
n]
Path
S.z
starPolygonFirstSpecies
:: Int
-> Float
-> (Float , Float)
-> Svg
starPolygonFirstSpecies :: Int -> Float -> (Float, Float) -> Svg
starPolygonFirstSpecies Int
n Float
r (Float
c1,Float
c2) =
Svg
S.path
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.d AttributeValue
directions
where
α :: Float
α = Float
2 forall a. Num a => a -> a -> a
* forall a. Floating a => a
pi forall a. Fractional a => a -> a -> a
/ (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n)
vertice :: p -> (Float, Float)
vertice p
k' =
let k :: Float
k = forall a b. (Integral a, Num b) => a -> b
fromIntegral p
k'
in
(,) (Float
c1 forall a. Num a => a -> a -> a
+ Float
r forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
sin (Float
kforall a. Num a => a -> a -> a
*Float
α))
(Float
c2 forall a. Num a => a -> a -> a
- Float
r forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
cos (Float
kforall a. Num a => a -> a -> a
*Float
α))
verticesList :: [(Float, Float)]
verticesList = forall a b. (a -> b) -> [a] -> [b]
map forall {p}. Integral p => p -> (Float, Float)
vertice [Int
0 .. (Int
nforall a. Num a => a -> a -> a
-Int
1)]
directions :: AttributeValue
directions =
if forall a. Integral a => a -> Bool
even Int
n
then
Path -> AttributeValue
mkPath forall a b. (a -> b) -> a -> b
$ do
forall a. Show a => a -> a -> Path
m (forall a b. (a, b) -> a
fst forall a b. (a -> b) -> a -> b
$ forall a. [a] -> a
head [(Float, Float)]
verticesList) (forall a b. (a, b) -> b
snd forall a b. (a -> b) -> a -> b
$ forall a. [a] -> a
head [(Float, Float)]
verticesList)
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry forall a. Show a => a -> a -> Path
S.l) (forall a b. (a, b) -> a
fst forall a b. (a -> b) -> a -> b
$ forall a. [a] -> ([a], [a])
evenOddSplit [(Float, Float)]
verticesList)
Path
S.z
forall a. Show a => a -> a -> Path
m (forall a b. (a, b) -> a
fst forall a b. (a -> b) -> a -> b
$ [(Float, Float)]
verticesList forall a. [a] -> Int -> a
!! Int
1) (forall a b. (a, b) -> b
snd forall a b. (a -> b) -> a -> b
$ [(Float, Float)]
verticesList forall a. [a] -> Int -> a
!! Int
1)
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry forall a. Show a => a -> a -> Path
S.l) (forall a b. (a, b) -> b
snd forall a b. (a -> b) -> a -> b
$ forall a. [a] -> ([a], [a])
evenOddSplit [(Float, Float)]
verticesList)
Path
S.z
else
Path -> AttributeValue
mkPath forall a b. (a -> b) -> a -> b
$ do
forall a. Show a => a -> a -> Path
m (forall a b. (a, b) -> a
fst forall a b. (a -> b) -> a -> b
$ forall a. [a] -> a
head [(Float, Float)]
verticesList) (forall a b. (a, b) -> b
snd forall a b. (a -> b) -> a -> b
$ forall a. [a] -> a
head [(Float, Float)]
verticesList)
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry forall a. Show a => a -> a -> Path
S.l) (forall a. [a] -> [a]
tail forall a b. (a -> b) -> a -> b
$ forall a b. (a, b) -> a
fst forall a b. (a -> b) -> a -> b
$ forall a. [a] -> ([a], [a])
evenOddSplit forall a b. (a -> b) -> a -> b
$ [(Float, Float)]
verticesList forall a. [a] -> [a] -> [a]
++ [(Float, Float)]
verticesList)
Path
S.z
starOutline
:: Int
-> Float
-> Float
-> (Float , Float)
-> Svg
starOutline :: Int -> Float -> Float -> (Float, Float) -> Svg
starOutline Int
n Float
r1 Float
r2 (Float
c1,Float
c2) =
Svg
S.path
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.strokeMiterlimit AttributeValue
"100"
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.d AttributeValue
directions
where
β :: Float
β = Float
2 forall a. Num a => a -> a -> a
* forall a. Floating a => a
pi forall a. Fractional a => a -> a -> a
/ (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n)
outerV :: Float -> (Float, Float)
outerV Float
k = (,)
(Float
c1 forall a. Num a => a -> a -> a
+ Float
r1 forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
sin (Float
kforall a. Num a => a -> a -> a
*Float
β))
(Float
c2 forall a. Num a => a -> a -> a
- Float
r1 forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
cos (Float
kforall a. Num a => a -> a -> a
*Float
β))
innerV :: Float -> (Float, Float)
innerV Float
k = (,)
(Float
c1 forall a. Num a => a -> a -> a
+ Float
r2 forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
sin (Float
kforall a. Num a => a -> a -> a
*Float
β forall a. Num a => a -> a -> a
+ Float
βforall a. Fractional a => a -> a -> a
/Float
2))
(Float
c2 forall a. Num a => a -> a -> a
- Float
r2 forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
cos (Float
kforall a. Num a => a -> a -> a
*Float
β forall a. Num a => a -> a -> a
+ Float
βforall a. Fractional a => a -> a -> a
/Float
2))
vertices :: [(Float, Float)]
vertices =
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr
(\Float
k [(Float, Float)]
acc -> (Float -> (Float, Float)
outerV Float
k) forall a. a -> [a] -> [a]
: (Float -> (Float, Float)
innerV Float
k) forall a. a -> [a] -> [a]
: [(Float, Float)]
acc)
[]
(forall a b. (a -> b) -> [a] -> [b]
map forall a b. (Integral a, Num b) => a -> b
fromIntegral [Int
0 .. (Int
nforall a. Num a => a -> a -> a
-Int
1)])
directions :: AttributeValue
directions = Path -> AttributeValue
mkPath forall a b. (a -> b) -> a -> b
$ do
forall a. Show a => a -> a -> Path
m (forall a b. (a, b) -> a
fst forall a b. (a -> b) -> a -> b
$ forall a. [a] -> a
head [(Float, Float)]
vertices) (forall a b. (a, b) -> b
snd forall a b. (a -> b) -> a -> b
$ forall a. [a] -> a
head [(Float, Float)]
vertices)
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry forall a. Show a => a -> a -> Path
S.l) (forall a. [a] -> [a]
tail [(Float, Float)]
vertices)
Path
S.z
starFat ::
Int -> Float -> (Float , Float) -> Svg
starFat :: Int -> Float -> (Float, Float) -> Svg
starFat Int
n Float
r1 (Float
c1,Float
c2) =
Int -> Float -> Float -> (Float, Float) -> Svg
starOutline Int
n Float
r1 Float
r2 (Float
c1,Float
c2)
where
β :: Float
β = Float
2 forall a. Num a => a -> a -> a
* forall a. Floating a => a
pi forall a. Fractional a => a -> a -> a
/ (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n)
r2 :: Float
r2 = Float
r1 forall a. Num a => a -> a -> a
* (Float
1 forall a. Num a => a -> a -> a
- forall a. Floating a => a -> a
sin(Float
βforall a. Fractional a => a -> a -> a
/Float
2)forall a. Num a => a -> a -> a
*forall a. Floating a => a -> a
tan(Float
βforall a. Fractional a => a -> a -> a
/Float
2))
starRegular ::
Int -> Float -> (Float , Float) -> Svg
starRegular :: Int -> Float -> (Float, Float) -> Svg
starRegular Int
n Float
r1 (Float
c1,Float
c2) =
Int -> Float -> Float -> (Float, Float) -> Svg
starOutline Int
n Float
r1 Float
r2 (Float
c1,Float
c2)
where
β :: Float
β = Float
2 forall a. Num a => a -> a -> a
* forall a. Floating a => a
pi forall a. Fractional a => a -> a -> a
/ (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n)
r2 :: Float
r2 = Float
r1 forall a. Num a => a -> a -> a
* (Float
2forall a. Num a => a -> a -> a
*forall a. Floating a => a -> a
cos(Float
βforall a. Fractional a => a -> a -> a
/Float
2) forall a. Num a => a -> a -> a
- Float
1forall a. Fractional a => a -> a -> a
/forall a. Floating a => a -> a
cos(Float
βforall a. Fractional a => a -> a -> a
/Float
2))