Skip to content

Commit

Permalink
Add hexagon
Browse files Browse the repository at this point in the history
  • Loading branch information
jeslie0 committed Jun 9, 2024
1 parent 4ec93b3 commit 5cce18a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
Binary file added .img/hexagon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions Descartes.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ extra-doc-files: CHANGELOG.md
-- extra-source-files:

common warnings
ghc-options: -Wall -Werror
ghc-options: -Wall

library
-- Import common warning flags.
Expand All @@ -79,7 +79,7 @@ library
build-depends: base ^>=4.18.2.1

-- GHC options for the library
ghc-options: -O2
ghc-options: -O2 -Werror

-- Directories containing source files.
hs-source-dirs: src
Expand Down
17 changes: 17 additions & 0 deletions README.org
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#+title: Descartes
#+author: James Leslie
[[https://img.shields.io/github/license/jeslie0/descartes.svg]]

#+html: <img src=".img/hexagon.png" width="200px"/>

This is a Haskell wrapper around The University of Edinburgh, School of Informatics' [[https://www.inf.ed.ac.uk/teaching/courses/cp/descartes.html][Descartes Package]]. The Descartes Packages is a small C library that uses [[https://www.libsdl.org/][SDL]] to make a very simple graphics package. It provides a way to make and render points, lines and rectangles on a 500px by 500px grid. It is mainly useful for educational purposes (indeed, I used it in the introductory "Computer Programming" course, back in 2014).

The main reason I made this library was to get a better understanding of Haskell's Foreign Function Interface with C, which turns out to be quite nice and very useable. As such, this repository may be of more use for someone wanting to explore Haskell's FFI functionality, rather than as an actual library.

Note - I only wrote the Haskell code and C wrapper code. Everything in [[file:cbits/descartes/][descartes]] directory is from the Descartes library.

* Dependencies
The only dependency is SDL. This has been tested with SDL version 1.2.15.

* Modifications
I made a few minor modifications to the Descartes C library. I changed the title of the graphics window to "Descartes".
30 changes: 28 additions & 2 deletions test/Main.hs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
module Main (main) where

import Descartes qualified as D
import Control.Monad (when)
import Control.Monad (when, forM_)
import Data.Complex qualified as C
import Data.Functor ((<&>))

main :: IO ()
main = D.withGraphics $ do
D.setColour D.Red
D.setColour D.Blue

putStrLn "Click twice to draw a line."
p1 <- D.getPoint
Expand All @@ -23,12 +25,36 @@ main = D.withGraphics $ do

D.fillRectangle $ D.rectangle bl tr

D.setColour D.Green
forM_ makeHexagon D.draw

D.setColour D.Red
makePath Nothing



makePath :: Maybe D.Point -> IO ()
makePath Nothing = D.getPoint >>= \p -> makePath (Just p)
makePath (Just p) = do
p' <- D.getPoint
D.draw $ D.lineSeg p p'
makePath (Just p')

joinPoints :: [D.Point] -> [D.LineSeg]
joinPoints [] = []
joinPoints [p] = []
joinPoints (p1:p2:ps) = D.lineSeg p1 p2 : joinPoints (p2:ps)

makeHexagon :: [D.LineSeg]
makeHexagon =
let
scale f = round $ 166 * (f + 1.5)

-- We use 7 here so that the last point is equal to the first.
powers :: [C.Complex Float] =
take 7 $ iterate (\ z -> z * C.mkPolar 1 (pi / 3)) (1.0 C.:+ 0)

points :: [D.Point] =
powers <&> \ z -> D.point (scale . C.realPart $ z) (scale . C.imagPart $ z)

in joinPoints points

0 comments on commit 5cce18a

Please sign in to comment.