Skip to content

Commit

Permalink
Add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
jeslie0 committed Jun 9, 2024
1 parent 38ea631 commit 4ec93b3
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/Descartes/Internal/Foreign/Window.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ import Foreign.C.Types (CInt (..))

foreign import capi unsafe "descartes/descartes.h SetColour" setColour :: CInt -> IO ()

-- | Clear all data from graphics window.
foreign import capi unsafe "descartes/descartes.h Clear" clear :: IO ()

-- | Open the graphics window. It is 500px by 500px with (0, 0) at the
-- bottom left and (500, 500) at the top right.
foreign import capi unsafe "descartes/descartes.h OpenGraphics" openGraphics :: IO ()

-- | Close the graphics window.
foreign import capi unsafe "descartes/descartes.h CloseGraphics" closeGraphics :: IO ()
5 changes: 5 additions & 0 deletions src/Descartes/Internal/Point.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ import Descartes.Internal.Foreign.Point (Point_t, pointXCoord, pointYCoord)
import Foreign (ForeignPtr, withForeignPtr)
import System.IO.Unsafe (unsafePerformIO)

-- | A point on the x-y plane. While there is no hard limit on the
-- bounds, the graphics window is bound in both the x and y direction
-- by 500px.
newtype Point = Point (ForeignPtr Point_t)

-- | Extract the x-coordinate from the point.
x :: Point -> Int
x (Point frnPtr) =
unsafePerformIO . withForeignPtr frnPtr $ return . fromIntegral . pointXCoord

-- | Extract the y-coordinate from the point.
y :: Point -> Int
y (Point frnPtr) =
unsafePerformIO . withForeignPtr frnPtr $ return . fromIntegral . pointYCoord
Expand Down
10 changes: 8 additions & 2 deletions src/Descartes/LineSeg.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,41 @@ import Foreign (ForeignPtr, newForeignPtr, withForeignPtr)
import Foreign.C.Types (CDouble (..))
import System.IO.Unsafe (unsafePerformIO)

-- | A straight line segment, defined by two points.
newtype LineSeg = LineSeg (ForeignPtr LineSeg_t)

instance Eq LineSeg where
l1 == l2 =
Descartes.LineSeg.initialPoint l1 == Descartes.LineSeg.initialPoint l2
&& Descartes.LineSeg.finalPoint l1 == Descartes.LineSeg.finalPoint l2

&& Descartes.LineSeg.finalPoint l1 == Descartes.LineSeg.finalPoint l2

-- | A smart constructor for a line segment. Takes an initial and
-- final point and generates the line between them.
lineSeg :: Point -> Point -> LineSeg
lineSeg (Point frnP1) (Point frnP2) =
LineSeg . unsafePerformIO . withForeignPtr frnP1 $
\p1 -> withForeignPtr frnP2 $
\p2 -> mask_ . newForeignPtr lineSegDelete $ lineSegNew p1 p2

-- | Extract the initial point of a line.
initialPoint :: LineSeg -> Point
initialPoint (LineSeg frnLine) =
Point . unsafePerformIO . withForeignPtr frnLine $
\ptr -> mask_ . newForeignPtr pointDelete $ Descartes.Internal.Foreign.LineSeg.initialPoint ptr

-- | Extract the final point of a line.
finalPoint :: LineSeg -> Point
finalPoint (LineSeg frnLine) =
Point . unsafePerformIO . withForeignPtr frnLine $
\ptr -> mask_ . newForeignPtr pointDelete $ Descartes.Internal.Foreign.LineSeg.finalPoint ptr

-- | Compute the length of a line.
length :: LineSeg -> Double
length (LineSeg frnLine) =
unsafePerformIO . withForeignPtr frnLine $
\ptr -> return . coerce . lineLength $ ptr

-- | Render a line in the active graphics window.
draw :: LineSeg -> IO ()
draw (LineSeg frnLine) =
withForeignPtr frnLine drawLineSeg
4 changes: 4 additions & 0 deletions src/Descartes/Point.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ import Descartes.Internal.Point (Point (..), x, y)
import Foreign (newForeignPtr)
import System.IO.Unsafe (unsafePerformIO)

-- | Smart constructor for a point. It takes an x value and y value
-- and returns the point.
point :: Int -> Int -> Point
point xVal yVal =
Point . unsafePerformIO . mask_ $ newForeignPtr pointDelete (pointNew (fromIntegral xVal) (fromIntegral yVal))

-- | Wait until the user clicks the mouse and return the point that
-- the user indicated.
getPoint :: IO Point
getPoint = do
p <- Descartes.Internal.Foreign.Point.getPoint
Expand Down
8 changes: 8 additions & 0 deletions src/Descartes/Rectangle.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,41 @@ import Descartes.Internal.Point (Point (..))
import Foreign (ForeignPtr, newForeignPtr, withForeignPtr)
import System.IO.Unsafe (unsafePerformIO)

-- | A rectangle is specified by its bottom left and top right
-- corners.
newtype Rectangle = Rectangle (ForeignPtr Rectangle_t)

instance Eq Rectangle where
r1 == r2 =
Descartes.Rectangle.bottomLeft r1 == Descartes.Rectangle.bottomLeft r2
&& Descartes.Rectangle.topRight r1 == Descartes.Rectangle.topRight r2

-- | A smart constructor for a rectangle. Takes the bottom left and
-- top right points, returning the rectangle.
rectangle :: Point -> Point -> Rectangle
rectangle (Point frnP1) (Point frnP2) =
Rectangle . unsafePerformIO . withForeignPtr frnP1 $
\p1 -> withForeignPtr frnP2 $
\p2 -> mask_ . newForeignPtr rectangleDelete $ rectangleNew p1 p2

-- | Extract the bottom left corner as a point, from the rectangle.
bottomLeft :: Rectangle -> Point
bottomLeft (Rectangle frnRect) =
Point . unsafePerformIO . withForeignPtr frnRect $
\ptr -> mask_ . newForeignPtr pointDelete $ Descartes.Internal.Foreign.Rectangle.bottomLeft ptr

-- | Extract the top right corner as a point, from the rectangle.
topRight :: Rectangle -> Point
topRight (Rectangle frnRect) =
Point . unsafePerformIO . withForeignPtr frnRect $
\ptr -> mask_ . newForeignPtr pointDelete $ Descartes.Internal.Foreign.Rectangle.topRight ptr

-- | Render the rectangle on the active graphics window.
fillRectangle :: Rectangle -> IO ()
fillRectangle (Rectangle frnRect) =
withForeignPtr frnRect Descartes.Internal.Foreign.Rectangle.fillRectangle

-- | Clear the specified rectangle from the active graphics window.
clearRectangle :: Rectangle -> IO ()
clearRectangle (Rectangle frnRect) =
withForeignPtr frnRect Descartes.Internal.Foreign.Rectangle.clearRectangle
2 changes: 2 additions & 0 deletions test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ main = D.withGraphics $ do

putStrLn "Click twice to draw a line."
p1 <- D.getPoint
print p1
p2 <- D.getPoint
print p2
D.draw $ D.lineSeg p1 p2

when (p1 == p2) $ putStrLn "The two points are equal!"
Expand Down

0 comments on commit 4ec93b3

Please sign in to comment.