Skip to content

Commit

Permalink
Can construct a straight line
Browse files Browse the repository at this point in the history
  • Loading branch information
camelpunch committed Oct 31, 2024
1 parent 42f11b5 commit ab738f8
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 14 deletions.
10 changes: 10 additions & 0 deletions lib/mudbrick/content_stream/l.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
defmodule Mudbrick.ContentStream.L do
@enforce_keys :coords
defstruct [:coords]

defimpl Mudbrick.Object do
def from(%Mudbrick.ContentStream.L{coords: {x, y}}) do
Enum.map_intersperse([x, y, "l"], " ", &to_string/1)
end
end
end
10 changes: 10 additions & 0 deletions lib/mudbrick/content_stream/m.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
defmodule Mudbrick.ContentStream.M do
@enforce_keys :coords
defstruct [:coords]

defimpl Mudbrick.Object do
def from(%Mudbrick.ContentStream.M{coords: {x, y}}) do
Enum.map_intersperse([x, y, "m"], " ", &to_string/1)
end
end
end
9 changes: 9 additions & 0 deletions lib/mudbrick/content_stream/s.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule Mudbrick.ContentStream.S do
defstruct []

defimpl Mudbrick.Object do
def from(_op) do
["S"]
end
end
end
20 changes: 11 additions & 9 deletions lib/mudbrick/drawing.ex
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
defmodule Mudbrick.Drawing do
defstruct lines: []

def new() do
struct!(__MODULE__, [])
end
defstruct paths: []

defmodule Path do
@enforce_keys [:from, :to]
defstruct [:from, :to]

def new(drawing, opts) do
Map.update!(drawing, :lines, fn lines ->
[struct!(__MODULE__, opts) | lines]
end)
def new(opts) do
struct!(__MODULE__, opts)
end
end

def new() do
struct!(__MODULE__, [])
end

def path(drawing, opts) do
%{drawing | paths: [Path.new(opts) | drawing.paths]}
end
end
20 changes: 18 additions & 2 deletions lib/mudbrick/drawing/output.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
defmodule Mudbrick.Drawing.Output do
defstruct operations: []

def from(_) do
%__MODULE__{}
alias Mudbrick.ContentStream.{
L,
M,
S
}

def from(%Mudbrick.Drawing{} = drawing) do
for path <- Enum.reverse(drawing.paths), reduce: %__MODULE__{} do
acc ->
acc
|> add(%M{coords: path.from})
|> add(%L{coords: path.to})
|> add(%S{})
end
end

def add(%__MODULE__{} = output, op) do
%{output | operations: [op | output.operations]}
end
end
36 changes: 33 additions & 3 deletions test/mudbrick/drawing_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,48 @@ defmodule Mudbrick.DrawingTest do
import Mudbrick.TestHelper, only: [output: 2]

alias Mudbrick.Drawing
alias Mudbrick.Drawing.Path

test "can construct a path" do
import Drawing

drawing =
new()
|> path(from: {0, 0}, to: {50, 50})

assert drawing.paths == [
Path.new(from: {0, 0}, to: {50, 50})
]
end

test "can make an empty drawing" do
import Drawing

test "can make an empty line drawing" do
assert [] =
output(fn ->
Drawing.new()
new()
end)
|> operations()
end

test "can draw one path" do
import Drawing

assert [
"0 50 m",
"60 50 l",
"S"
] =
output(fn ->
new()
|> path(from: {0, 50}, to: {60, 50})
end)
|> operations()
end

defp output(f), do: output(fn _ -> f.() end, Mudbrick.Drawing.Output)

defp operations(ops) do
Enum.map(ops, &Mudbrick.TestHelper.show/1)
Enum.map(ops, &Mudbrick.TestHelper.show/1) |> Enum.reverse()
end
end

0 comments on commit ab738f8

Please sign in to comment.