module CodeWorld.Picture where
import CodeWorld.Color
import Data.Monoid ((<>))
import Data.Text (Text, pack)
type Point = (Double, Double)
type Vector = (Double, Double)
vectorSum :: Vector -> Vector -> Vector
vectorSum (x1,y1) (x2,y2) = (x1 + x2, y1 + y2)
vectorDifference :: Vector -> Vector -> Vector
vectorDifference (x1,y1) (x2,y2) = (x1 x2, y1 y2)
scaledVector :: Double -> Vector -> Vector
scaledVector k (x,y) = (k*x, k*y)
rotatedVector :: Double -> Vector -> Vector
rotatedVector angle (x,y) = (x * cos angle y * sin angle,
x * sin angle + y * cos angle)
dotProduct :: Vector -> Vector -> Double
dotProduct (x1, y1) (x2, y2) = x1 * x2 + y1 * y2
data Picture = Polygon [Point] !Bool
| Path [Point] !Double !Bool !Bool
| Sector !Double !Double !Double
| Arc !Double !Double !Double !Double
| Text !TextStyle !Font !Text
| Color !Color !Picture
| Translate !Double !Double !Picture
| Scale !Double !Double !Picture
| Rotate !Double !Picture
| Pictures [Picture]
| Logo
data TextStyle = Plain | Bold | Italic
data Font = SansSerif | Serif | Monospace | Handwriting | Fancy | NamedFont !Text
blank :: Picture
blank = Pictures []
path :: [Point] -> Picture
path ps = Path ps 0 False False
thickPath :: Double -> [Point] -> Picture
thickPath n ps = Path ps n False False
polygon :: [Point] -> Picture
polygon ps = Path ps 0 True False
thickPolygon :: Double -> [Point] -> Picture
thickPolygon n ps = Path ps n True False
solidPolygon :: [Point] -> Picture
solidPolygon ps = Polygon ps False
curve :: [Point] -> Picture
curve ps = Path ps 0 False True
thickCurve :: Double -> [Point] -> Picture
thickCurve n ps = Path ps n False True
loop :: [Point] -> Picture
loop ps = Path ps 0 True True
thickLoop :: Double -> [Point] -> Picture
thickLoop n ps = Path ps n True True
solidLoop :: [Point] -> Picture
solidLoop ps = Polygon ps True
rectangle :: Double -> Double -> Picture
rectangle w h = polygon [
(w/2, h/2), (w/2, h/2), (w/2, h/2), (w/2, h/2)
]
solidRectangle :: Double -> Double -> Picture
solidRectangle w h = solidPolygon [
(w/2, h/2), (w/2, h/2), (w/2, h/2), (w/2, h/2)
]
thickRectangle :: Double -> Double -> Double -> Picture
thickRectangle lw w h = thickPolygon lw [
(w/2, h/2), (w/2, h/2), (w/2, h/2), (w/2, h/2)
]
circle :: Double -> Picture
circle = arc 0 (2*pi)
thickCircle :: Double -> Double -> Picture
thickCircle w = thickArc w 0 (2*pi)
arc :: Double -> Double -> Double -> Picture
arc b e r = Arc b e r 0
thickArc :: Double -> Double -> Double -> Double -> Picture
thickArc w b e r = Arc b e r w
solidCircle :: Double -> Picture
solidCircle = sector 0 (2*pi)
sector :: Double -> Double -> Double -> Picture
sector = Sector
text :: Text -> Picture
text = Text Plain Serif
styledText :: TextStyle -> Font -> Text -> Picture
styledText = Text
colored :: Color -> Picture -> Picture
colored = Color
coloured :: Color -> Picture -> Picture
coloured = colored
translated :: Double -> Double -> Picture -> Picture
translated = Translate
scaled :: Double -> Double -> Picture -> Picture
scaled = Scale
dilated :: Double -> Double -> Picture -> Picture
dilated = scaled
rotated :: Double -> Picture -> Picture
rotated = Rotate
pictures :: [Picture] -> Picture
pictures = Pictures
instance Monoid Picture where
mempty = blank
mappend a (Pictures bs) = Pictures (a:bs)
mappend a b = Pictures [a, b]
mconcat = pictures
(&) :: Picture -> Picture -> Picture
infixr 0 &
(&) = mappend
coordinatePlane :: Picture
coordinatePlane = axes <> numbers <> guidelines
where xline y = thickPath 0.01 [(10, y), (10, y)]
xaxis = thickPath 0.03 [(10, 0), (10, 0)]
axes = xaxis <> rotated (pi/2) xaxis
xguidelines = pictures [ xline k | k <- [10, 9 .. 10] ]
guidelines = xguidelines <> rotated (pi/2) xguidelines
numbers = xnumbers <> ynumbers
xnumbers = pictures
[ translated (fromIntegral k) 0.3 (scaled 0.5 0.5 (text (pack (show k))))
| k <- [9, 8 .. 9], k /= 0 ]
ynumbers = pictures
[ translated 0.3 (fromIntegral k) (scaled 0.5 0.5 (text (pack (show k))))
| k <- [9, 8 .. 9], k /= 0 ]
codeWorldLogo :: Picture
codeWorldLogo = Logo