Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to elm-0.19.1 #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 64 additions & 48 deletions Bitmap.elm
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
module Bitmap
exposing
( Bitmap
, FloatPoint
, Pixel(..)
, Point
, circle
, create
, curve
, line
, set
, toggle
)
module Bitmap exposing
( Bitmap
, FloatPoint
, Pixel(..)
, Point
, circle
, create
, curve
, line
, set
, toggle
)

import Array exposing (Array)
import Color
import List


Expand Down Expand Up @@ -57,38 +55,40 @@ create length defaultFill =


{-| colIndex => x-coordinate
rowIndex => y-coordinate
rowIndex => y-coordinate
-}
set : Pixel -> Int -> Int -> Bitmap -> Bitmap
set newFill colIndex rowIndex bitmap =
let
set arr =
set_ arr =
Array.set colIndex newFill arr

newRow =
Maybe.map set (Array.get rowIndex bitmap)
Maybe.map set_ (Array.get rowIndex bitmap)
in
case newRow of
Just row ->
Array.set rowIndex row bitmap
case newRow of
Just row ->
Array.set rowIndex row bitmap

Nothing ->
bitmap
Nothing ->
bitmap


{-| colIndex => x-coordinate
rowIndex => y-coordinate
rowIndex => y-coordinate
-}
get : Int -> Int -> Bitmap -> Maybe Pixel
get colIndex rowIndex bitmap =
Maybe.andThen (Array.get rowIndex bitmap) (Array.get colIndex)
Maybe.andThen (Array.get colIndex) (Array.get rowIndex bitmap)


{-| Swap between two pixel values (aPixel and bPixel). Does not modify the pixel
if it is neither value.
if it is neither value.

colIndex => x - coordinate

rowIndex => y - coordinate

colIndex => x-coordinate
rowIndex => y-coordinate
-}
toggle : Pixel -> Pixel -> Int -> Int -> Bitmap -> Bitmap
toggle aPixel bPixel colIndex rowIndex bitmap =
Expand All @@ -99,17 +99,19 @@ toggle aPixel bPixel colIndex rowIndex bitmap =
alternate pixel =
if pixel == aPixel then
bPixel

else if pixel == bPixel then
aPixel

else
pixel
in
case curPixel of
Just pixel ->
set (alternate pixel) colIndex rowIndex bitmap
case curPixel of
Just pixel ->
set (alternate pixel) colIndex rowIndex bitmap

Nothing ->
bitmap
Nothing ->
bitmap


closedRange : Int -> Int -> Array Int
Expand All @@ -122,15 +124,16 @@ closedRange from to =
sign =
if from <= to then
(+)

else
(-)
in
Array.initialize quantity identity |> Array.map (sign from)
Array.initialize quantity identity |> Array.map (sign from)


{-| This function is designed to be used in a fold/reduce call. It should be
curried with some parameters based on the octant the function is operating
in.
curried with some parameters based on the octant the function is operating
in.

The Bresenham line algorithm works by folding/iterating over consecutive
numbers on an axis. The challenge then is to figure out when a point should
Expand Down Expand Up @@ -171,6 +174,7 @@ closedRange from to =
Fold across the x-axis, such that `b` is our y-component.
Note that Bitmap.set takes rowIndex before colIndex (y before x)
bresenhamLinePlot (dy/dx) 1 -1 (\a b -> set pixel b a bitmap)

-}
bresenhamLinePlot : Float -> Int -> Float -> (Int -> Int -> Bitmap -> Bitmap) -> Int -> ( Bitmap, Float, Int ) -> ( Bitmap, Float, Int )
bresenhamLinePlot slope delta correction plot a ( bitmap, error, b ) =
Expand All @@ -184,10 +188,11 @@ bresenhamLinePlot slope delta correction plot a ( bitmap, error, b ) =
( nextError, nextB ) =
if shouldAdvance then
( error + slope + correction, b + delta )

else
( error + slope, b )
in
( nextBitmap, nextError, nextB )
( nextBitmap, nextError, nextB )


line : Pixel -> Point -> Point -> Bitmap -> Bitmap
Expand All @@ -206,19 +211,19 @@ line pixel origin endpoint bitmap =
y2 - y1

m =
(toFloat dy) / (toFloat dx)
toFloat dy / toFloat dx

-- We cannot use the traditional slope (y per x) if we plot through
-- ys instead of xs. We would need to use the reciprocal slope, the
-- units x per unit y.
rM =
(toFloat dx) / (toFloat dy)
toFloat dx / toFloat dy

foldingAcrossX a b bitmap =
set pixel a b bitmap
foldingAcrossX a b bitmap_ =
set pixel a b bitmap_

foldingAcrossY a b bitmap =
set pixel b a bitmap
foldingAcrossY a b bitmap_ =
set pixel b a bitmap_

( plotFunc, interval, start ) =
if 0 <= m && m <= 1 && x1 < x2 then
Expand All @@ -227,48 +232,56 @@ line pixel origin endpoint bitmap =
, closedRange x1 x2
, y1
)

else if 1 < m && y1 < y2 then
-- Second octant.
( bresenhamLinePlot rM 1 -1 foldingAcrossY
, closedRange y1 y2
, x1
)

else if -1 > m && y1 < y2 then
-- Third octant.
( bresenhamLinePlot rM 1 1 foldingAcrossY
, closedRange y2 y1
, x2
)

else if 0 > m && m >= -1 && x2 < x1 then
-- Fourth octant.
( bresenhamLinePlot m -1 1 foldingAcrossX
, closedRange x2 x1
, y2
)

else if 0 < m && m <= 1 && x2 < x1 then
-- Fifth octant.
( bresenhamLinePlot m 1 -1 foldingAcrossX
, closedRange x2 x1
, y2
)

else if 1 < m && y2 < y1 then
-- Sixth octant.
( bresenhamLinePlot rM 1 -1 foldingAcrossY
, closedRange y2 y1
, x2
)

else if -1 > m && y2 < y1 then
-- Seventh octant.
( bresenhamLinePlot rM 1 1 foldingAcrossY
, closedRange y1 y2
, x1
)

else if 0 > m && m >= -1 && x1 < x2 then
-- Eighth octant.
( bresenhamLinePlot m -1 1 foldingAcrossX
, closedRange x1 x2
, y1
)

else
( bresenhamLinePlot m 1 -1 foldingAcrossX
, closedRange x1 x2
Expand All @@ -278,7 +291,7 @@ line pixel origin endpoint bitmap =
( newBitmap, _, _ ) =
Array.foldl plotFunc ( bitmap, 0, start ) interval
in
newBitmap
newBitmap


plotSymmetricalOctants : (Int -> Int -> Bitmap -> Bitmap) -> Bitmap -> Int -> Int -> Bitmap
Expand All @@ -298,6 +311,7 @@ bresenhamCirclePlot : (Int -> Int -> Bitmap -> Bitmap) -> Int -> Int -> Int -> I
bresenhamCirclePlot plot radius x y error bitmap =
if x < y then
bitmap

else
let
nextBitmap =
Expand All @@ -321,10 +335,11 @@ bresenhamCirclePlot plot radius x y error bitmap =
( nextX, nextError ) =
if shouldDecrementX then
( x - 1, error + 2 + 2 * (y + 1) - 2 * (x - 1) )

else
( x, error + 1 + 2 * (y + 1) )
in
bresenhamCirclePlot plot radius nextX (y + 1) nextError nextBitmap
bresenhamCirclePlot plot radius nextX (y + 1) nextError nextBitmap


circle : Pixel -> Point -> Int -> Bitmap -> Bitmap
Expand All @@ -339,7 +354,7 @@ circle pixel origin radius bitmap =
interval =
closedRange 0 radius
in
bresenhamCirclePlot plot radius radius 0 0 bitmap
bresenhamCirclePlot plot radius radius 0 0 bitmap


computeBezierPoint : Float -> FloatPoint -> FloatPoint -> FloatPoint
Expand All @@ -353,7 +368,7 @@ computeBezierPoints : Float -> List FloatPoint -> List FloatPoint
computeBezierPoints t points =
case points of
p0 :: p1 :: remainder ->
computeBezierPoint t p0 p1 :: (computeBezierPoints t (p1 :: remainder))
computeBezierPoint t p0 p1 :: computeBezierPoints t (p1 :: remainder)

_ ->
[]
Expand All @@ -368,6 +383,7 @@ plotCurve pixel points t bitmap =

_ ->
bitmap

else
plotCurve pixel (computeBezierPoints t points) t bitmap

Expand All @@ -379,6 +395,6 @@ curve pixel points bitmap =
100

tValues =
List.map (\i -> i / segments) [1..segments]
List.map (\i -> toFloat i / segments) (List.range 1 segments)
in
List.foldl (plotCurve pixel points) bitmap tValues
List.foldl (plotCurve pixel points) bitmap tValues
32 changes: 17 additions & 15 deletions Main.elm
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module Main exposing (..)

import Array
import Bitmap
import Debug exposing (log)
import GraphicSVG exposing (..)
import GraphicSVG.App exposing (..)
import List
import Array
import Debug exposing (log)


drawPixel rowIndex colIndex pixel =
Expand All @@ -13,15 +14,15 @@ drawPixel rowIndex colIndex pixel =
pixel

x =
(toFloat colIndex) * cellSize
toFloat colIndex * cellSize

y =
(toFloat rowIndex) * cellSize
toFloat rowIndex * cellSize
in
square cellSize
|> filled (rgba (toFloat r) (toFloat g) (toFloat b) a)
|> move ( x, y )
|> notifyTap (Toggle colIndex rowIndex)
square cellSize
|> filled (rgba (toFloat r) (toFloat g) (toFloat b) a)
|> move ( x, y )
|> notifyTap (Toggle colIndex rowIndex)


drawBitmap bitmap =
Expand All @@ -31,9 +32,9 @@ drawBitmap bitmap =
|> Array.toList
|> group
in
Array.indexedMap drawRow bitmap
|> Array.toList
|> group
Array.indexedMap drawRow bitmap
|> Array.toList
|> group


white =
Expand All @@ -58,6 +59,7 @@ main =
{ model = init
, view = view
, update = update
, title = "elm-bitmap"
}


Expand All @@ -77,10 +79,10 @@ view model =
center =
move ( cellSize * -32, cellSize * -32 )
in
collage 600
600
[ drawBitmap model.bitmap |> center
]
collage 600
600
[ drawBitmap model.bitmap |> center
]


update message model =
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
https://guide.elm-lang.org/install/elm.html then `elm reactor`.

You should see this in the browser:

![](/screenshot.png)
17 changes: 0 additions & 17 deletions elm-package.json

This file was deleted.

Loading