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
line :: [Point] -> Picture
line ps = Path ps 0 False False
thickLine :: Double -> [Point] -> Picture
thickLine 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 360
thickCircle :: Double -> Double -> Picture
thickCircle w = thickArc w 0 360
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 360
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 = Color
translated :: Double -> Double -> Picture -> Picture
translated = Translate
scaled :: Double -> Double -> Picture -> Picture
scaled = Scale
dilated :: Double -> Double -> Picture -> Picture
dilated = Scale
rotated :: Double -> Picture -> Picture
rotated = Rotate
pictures :: [Picture] -> Picture
pictures = Pictures
(&) :: Picture -> Picture -> Picture
infixr 0 &
a & Pictures bs = Pictures (a:bs)
a & b = Pictures [a, b]
instance Monoid Picture where
mempty = blank
mappend = (&)
mconcat = pictures
coordinatePlane :: Picture
coordinatePlane = axes & numbers & guidelines
where xline y = line [(10, y), (10, y)]
xaxis = colored (RGBA 0 0 0 0.75) (xline 0)
axes = xaxis <> rotated (pi/2) xaxis
xguidelines = pictures
[colored (RGBA 0 0 0 0.25) (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