46 lines
1.2 KiB
Standard ML
46 lines
1.2 KiB
Standard ML
structure Grid =
|
|
struct
|
|
type pixel = {r: int, g: int, b: int, a: int}
|
|
|
|
type t = pixel vector vector
|
|
|
|
val emptyPixel = {r = 0, g = 0, b = 0, a = 0}
|
|
|
|
fun isBlank ({a, ...}: pixel) = a = 0
|
|
|
|
fun changeGridSize maxSide grid =
|
|
Vector.tabulate (maxSide, fn i =>
|
|
if i < Vector.length grid then
|
|
let
|
|
val yAxis = Vector.sub (grid, i)
|
|
in
|
|
Vector.tabulate (maxSide, fn ii =>
|
|
if ii < Vector.length yAxis then Vector.sub (yAxis, ii)
|
|
else emptyPixel)
|
|
end
|
|
else
|
|
Vector.tabulate (maxSide, fn _ => emptyPixel))
|
|
|
|
fun updateGrid (grid, newX, newY, pixel) =
|
|
let
|
|
val yAxis = Vector.sub (grid, newX)
|
|
val yAxis = Vector.update (yAxis, newY, pixel)
|
|
in
|
|
Vector.update (grid, newX, yAxis)
|
|
end
|
|
|
|
fun makeEmpty maxSide =
|
|
Vector.tabulate (maxSide, fn _ =>
|
|
Vector.tabulate (maxSide, fn _ => emptyPixel))
|
|
|
|
fun flipHorizontally (xAxis: t) =
|
|
Vector.mapi
|
|
(fn (xIdx, yAxis) =>
|
|
let
|
|
val flippedXIdx = Vector.length xAxis - 1 - xIdx
|
|
val flippedYAxis = Vector.sub (xAxis, flippedXIdx)
|
|
in
|
|
Vector.mapi (fn (yIdx, _) => Vector.sub (flippedYAxis, yIdx)) yAxis
|
|
end) xAxis
|
|
end
|