{-# LANGUAGE CPP #-}
module CodeWorld.Picture where
import CodeWorld.Color
import Data.Monoid ((<>))
import Data.Text (Text, pack)
import GHC.Stack
type Point = (Double, Double)
type Vector = (Double, Double)
vectorLength :: Vector -> Double
vectorLength (x, y) = sqrt (x ^ 2 + y ^ 2)
vectorDirection :: Vector -> Double
vectorDirection (x, y) = atan2 y x
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
= SolidPolygon CallStack
[Point]
| SolidClosedCurve CallStack
[Point]
| Polygon CallStack
[Point]
| ThickPolygon CallStack
[Point]
!Double
| Rectangle CallStack
!Double
!Double
| SolidRectangle CallStack
!Double
!Double
| ThickRectangle CallStack
!Double
!Double
!Double
| ClosedCurve CallStack
[Point]
| ThickClosedCurve CallStack
[Point]
!Double
| Polyline CallStack
[Point]
| ThickPolyline CallStack
[Point]
!Double
| Curve CallStack
[Point]
| ThickCurve CallStack
[Point]
!Double
| Circle CallStack
!Double
| SolidCircle CallStack
!Double
| ThickCircle CallStack
!Double
!Double
| Sector CallStack
!Double
!Double
!Double
| Arc CallStack
!Double
!Double
!Double
| ThickArc CallStack
!Double
!Double
!Double
!Double
| StyledText CallStack
!TextStyle
!Font
!Text
| Text CallStack
!Text
| Color CallStack
!Color
!Picture
| Translate CallStack
!Double
!Double
!Picture
| Scale CallStack
!Double
!Double
!Picture
| Rotate CallStack
!Double
!Picture
| CoordinatePlane CallStack
| Logo CallStack
| Pictures [Picture]
| Blank CallStack
data TextStyle
= Plain
| Bold
| Italic
data Font
= SansSerif
| Serif
| Monospace
| Handwriting
| Fancy
| NamedFont !Text
blank :: HasCallStack => Picture
blank = Blank callStack
polyline :: HasCallStack => [Point] -> Picture
polyline ps = Polyline callStack ps
path :: HasCallStack => [Point] -> Picture
path ps = Polyline callStack ps
{-# WARNING path "Please use polyline instead of path." #-}
thickPolyline :: HasCallStack => Double -> [Point] -> Picture
thickPolyline n ps = ThickPolygon callStack ps n
thickPath :: HasCallStack => Double -> [Point] -> Picture
thickPath n ps = ThickPolyline callStack ps n
{-# WARNING thickPath "Please used thickPolyline instead of thickPath." #-}
polygon :: HasCallStack => [Point] -> Picture
polygon ps = Polygon callStack ps
thickPolygon :: HasCallStack => Double -> [Point] -> Picture
thickPolygon n ps = ThickPolygon callStack ps n
solidPolygon :: HasCallStack => [Point] -> Picture
solidPolygon ps = SolidPolygon callStack ps
curve :: HasCallStack => [Point] -> Picture
curve ps = Curve callStack ps
thickCurve :: HasCallStack => Double -> [Point] -> Picture
thickCurve n ps = ThickCurve callStack ps n
closedCurve :: HasCallStack => [Point] -> Picture
closedCurve ps = ClosedCurve callStack ps
loop :: HasCallStack => [Point] -> Picture
loop ps = ClosedCurve callStack ps
{-# WARNING loop "Please use closedCurve instead of loop." #-}
thickClosedCurve :: HasCallStack => Double -> [Point] -> Picture
thickClosedCurve n ps = ThickClosedCurve callStack ps n
thickLoop :: HasCallStack => Double -> [Point] -> Picture
thickLoop n ps = ThickClosedCurve callStack ps n
{-# WARNING thickLoop "Please use thickClosedCurve instead of thickLoop." #-}
solidClosedCurve :: HasCallStack => [Point] -> Picture
solidClosedCurve ps = SolidClosedCurve callStack ps
solidLoop :: HasCallStack => [Point] -> Picture
solidLoop ps = SolidClosedCurve callStack ps
{-# WARNING solidLoop "Please use solidClosedCurve instead of solidLoop." #-}
rectangleVertices :: Double -> Double -> [Point]
rectangleVertices w h = [ (w / 2, h / 2), (w / 2, -h / 2), (-w / 2, -h / 2), (-w / 2, h / 2) ]
rectangle :: HasCallStack => Double -> Double -> Picture
rectangle w h = Rectangle callStack w h
solidRectangle :: HasCallStack => Double -> Double -> Picture
solidRectangle w h = SolidRectangle callStack w h
thickRectangle :: HasCallStack => Double -> Double -> Double -> Picture
thickRectangle lw w h = ThickRectangle callStack lw w h
circle :: HasCallStack => Double -> Picture
circle = Circle callStack
thickCircle :: HasCallStack => Double -> Double -> Picture
thickCircle = ThickCircle callStack
arc :: HasCallStack => Double -> Double -> Double -> Picture
arc b e r = Arc callStack b e r
thickArc :: HasCallStack => Double -> Double -> Double -> Double -> Picture
thickArc w b e r = ThickArc callStack b e r w
solidCircle :: HasCallStack => Double -> Picture
solidCircle = SolidCircle callStack
sector :: HasCallStack => Double -> Double -> Double -> Picture
sector = Sector callStack
text :: HasCallStack => Text -> Picture
text = Text callStack
styledText :: HasCallStack => TextStyle -> Font -> Text -> Picture
styledText = StyledText callStack
colored :: HasCallStack => Color -> Picture -> Picture
colored = Color callStack
coloured :: HasCallStack => Color -> Picture -> Picture
coloured = colored
translated :: HasCallStack => Double -> Double -> Picture -> Picture
translated = Translate callStack
scaled :: HasCallStack => Double -> Double -> Picture -> Picture
scaled = Scale callStack
dilated :: HasCallStack => Double -> Picture -> Picture
dilated k = scaled k k
rotated :: HasCallStack => Double -> Picture -> Picture
rotated = Rotate callStack
pictures :: [Picture] -> Picture
pictures = Pictures
#if MIN_VERSION_base(4,11,0)
instance Semigroup Picture where
a <> (Pictures bs) = Pictures (a : bs)
a <> b = Pictures [a, b]
#endif
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 :: HasCallStack => Picture
coordinatePlane = CoordinatePlane callStack
codeWorldLogo :: HasCallStack => Picture
codeWorldLogo = Logo callStack