{-# LANGUAGE OverloadedStrings #-}
module Core.Utils
( evenOddSplit
, addXmlns
, (.:)
, distance
, horizontalMirrorMatrix
, verticalMirrorMatrix
, frame
) where
import Data.Char
import Text.Blaze.Svg11 ((!))
import Text.Blaze.Svg11 as S
import Text.Blaze.Svg11.Attributes as A
evenOddSplit :: [a] -> ([a], [a])
evenOddSplit :: forall a. [a] -> ([a], [a])
evenOddSplit [] = ([], [])
evenOddSplit (a
x:[a]
xs) = (a
xforall a. a -> [a] -> [a]
:[a]
o, [a]
e)
where ([a]
e,[a]
o) = forall a. [a] -> ([a], [a])
evenOddSplit [a]
xs
addXmlns :: Svg -> Svg
addXmlns :: Svg -> Svg
addXmlns Svg
svg =
Svg
svg
forall h. Attributable h => h -> Attribute -> h
! Tag -> AttributeValue -> Attribute
customAttribute Tag
"xmlns" AttributeValue
"http://www.w3.org/2000/svg"
forall h. Attributable h => h -> Attribute -> h
! Tag -> AttributeValue -> Attribute
customAttribute Tag
"xmlns:xlink" AttributeValue
"http://www.w3.org/1999/xlink"
infixl 5 .:
(.:) :: (AttributeValue -> Attribute ) -> Float -> Attribute
AttributeValue -> Attribute
f .: :: (AttributeValue -> Attribute) -> Float -> Attribute
.: Float
x = AttributeValue -> Attribute
f forall a b. (a -> b) -> a -> b
$ forall a. ToValue a => a -> AttributeValue
S.toValue Float
x
distance :: (Float, Float) -> (Float, Float) -> Float
distance :: (Float, Float) -> (Float, Float) -> Float
distance (Float
ax,Float
ay) (Float
bx,Float
by) =
forall a. Floating a => a -> a
sqrt forall a b. (a -> b) -> a -> b
$ (Float
bx forall a. Num a => a -> a -> a
- Float
ax)forall a. Floating a => a -> a -> a
**Float
2 forall a. Num a => a -> a -> a
+ (Float
by forall a. Num a => a -> a -> a
- Float
ay)forall a. Floating a => a -> a -> a
**Float
2
horizontalMirrorMatrix :: AttributeValue
horizontalMirrorMatrix :: AttributeValue
horizontalMirrorMatrix =
forall a. Show a => a -> a -> a -> a -> a -> a -> AttributeValue
matrix (-Integer
1) Integer
0 Integer
0 Integer
1 Integer
0 Integer
0
verticalMirrorMatrix :: AttributeValue
verticalMirrorMatrix :: AttributeValue
verticalMirrorMatrix =
forall a. Show a => a -> a -> a -> a -> a -> a -> AttributeValue
matrix Integer
1 Integer
0 Integer
0 (-Integer
1) Integer
0 Integer
0
frame :: Float -> Float -> Float -> Float -> S.Svg
frame :: Float -> Float -> Float -> Float -> Svg
frame Float
x Float
y Float
w Float
h =
Svg
S.path
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.fill AttributeValue
"none"
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.stroke AttributeValue
"black"
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.strokeWidth AttributeValue
"0.002"
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.d AttributeValue
frameDirs
where
frameDirs :: AttributeValue
frameDirs = Path -> AttributeValue
mkPath forall a b. (a -> b) -> a -> b
$ do
forall a. Show a => a -> a -> Path
m Float
x Float
y
forall a. Show a => a -> a -> Path
l Float
x (Float
y forall a. Num a => a -> a -> a
+ Float
h)
forall a. Show a => a -> a -> Path
l (Float
x forall a. Num a => a -> a -> a
+ Float
w) (Float
y forall a. Num a => a -> a -> a
+ Float
h)
forall a. Show a => a -> a -> Path
l (Float
x forall a. Num a => a -> a -> a
+ Float
w) Float
y
Path
S.z
forall a. Show a => a -> a -> Path
m (Float
x forall a. Num a => a -> a -> a
+ Float
wforall a. Fractional a => a -> a -> a
/Float
2) Float
y
forall a. Show a => a -> a -> Path
l (Float
x forall a. Num a => a -> a -> a
+ Float
wforall a. Fractional a => a -> a -> a
/Float
2) (Float
y forall a. Num a => a -> a -> a
+ Float
h)
forall a. Show a => a -> a -> Path
m Float
x (Float
y forall a. Num a => a -> a -> a
+ Float
hforall a. Fractional a => a -> a -> a
/Float
2)
forall a. Show a => a -> a -> Path
l (Float
x forall a. Num a => a -> a -> a
+ Float
w) (Float
y forall a. Num a => a -> a -> a
+ Float
hforall a. Fractional a => a -> a -> a
/Float
2)
cleanDecimals :: Int -> String -> String
cleanDecimals :: Int -> String -> String
cleanDecimals Int
n String
s =
String -> String -> String -> String
f [] [] String
s
where
f :: String -> String -> String -> String
f String
_ String
acc [] = forall a. [a] -> [a]
reverse String
acc
f String
aux String
acc (Char
c:String
cs) =
if Char
c forall a. Eq a => a -> a -> Bool
== Char
'.'
then String -> String -> String -> String
f String
"." String
acc String
cs
else if String
aux forall a. Eq a => a -> a -> Bool
== []
then String -> String -> String -> String
f [] (Char
c forall a. a -> [a] -> [a]
: String
acc) String
cs
else if (Bool -> Bool
not forall a b. (a -> b) -> a -> b
$ Char -> Bool
isDigit Char
c)
then String -> String -> String -> String
f [] (Char
c forall a. a -> [a] -> [a]
: String
aux forall a. [a] -> [a] -> [a]
++ String
acc) String
cs
else if (forall (t :: * -> *) a. Foldable t => t a -> Int
length String
aux forall a. Ord a => a -> a -> Bool
< Int
n)
then String -> String -> String -> String
f (Char
c forall a. a -> [a] -> [a]
: String
aux) String
acc String
cs
else String -> String -> String -> String
f String
aux String
acc String
cs