Implement String.Chars for Shapes. W00T.
This commit is contained in:
parent
ecc8e0a37b
commit
7564e3bbe5
2 changed files with 32 additions and 1 deletions
25
lib/string/chars/vivid/shape.ex
Normal file
25
lib/string/chars/vivid/shape.ex
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
defmodule Vivid.ShapeToString do
|
||||||
|
alias Vivid.{Bounds, Frame, Transform, RGBA}
|
||||||
|
|
||||||
|
def to_string(shape) do
|
||||||
|
bounds = Bounds.bounds(shape)
|
||||||
|
width = Bounds.width(bounds) + 3 |> round
|
||||||
|
height = Bounds.height(bounds) + 3 |> round
|
||||||
|
frame = Frame.init(width, height, RGBA.white)
|
||||||
|
|
||||||
|
shape = shape
|
||||||
|
|> Transform.center(frame)
|
||||||
|
|> Transform.apply
|
||||||
|
|
||||||
|
frame
|
||||||
|
|> Frame.push(shape, RGBA.black)
|
||||||
|
|> Kernel.to_string
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
Enum.each(~w(Arc Box Circle Group Line Path Polygon), fn mod ->
|
||||||
|
mod = Module.concat(Vivid, mod)
|
||||||
|
defimpl String.Chars, for: mod do
|
||||||
|
def to_string(shape), do: Vivid.ShapeToString.to_string(shape)
|
||||||
|
end
|
||||||
|
end)
|
|
@ -57,7 +57,9 @@ defimpl Vivid.Rasterize, for: Vivid.Line do
|
||||||
|
|
||||||
reduce_points(points, steps, current_x, current_y, x_increment, y_increment)
|
reduce_points(points, steps, current_x, current_y, x_increment, y_increment)
|
||||||
end
|
end
|
||||||
|
|> pixel_round
|
||||||
|> clip(bounds)
|
|> clip(bounds)
|
||||||
|
|> Enum.into(MapSet.new)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp reduce_points(points, 0, _, _, _, _), do: points
|
defp reduce_points(points, 0, _, _, _, _), do: points
|
||||||
|
@ -73,10 +75,14 @@ defimpl Vivid.Rasterize, for: Vivid.Line do
|
||||||
defp choose_largest_of(a, b) when a > b, do: a
|
defp choose_largest_of(a, b) when a > b, do: a
|
||||||
defp choose_largest_of(_, b), do: b
|
defp choose_largest_of(_, b), do: b
|
||||||
|
|
||||||
|
defp pixel_round(points) do
|
||||||
|
points
|
||||||
|
|> Stream.map(&Point.round(&1))
|
||||||
|
end
|
||||||
|
|
||||||
defp clip(points, %Bounds{}=bounds) do
|
defp clip(points, %Bounds{}=bounds) do
|
||||||
points
|
points
|
||||||
|> Stream.filter(&Bounds.contains?(bounds,&1))
|
|> Stream.filter(&Bounds.contains?(bounds,&1))
|
||||||
|> Enum.into(MapSet.new)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
Loading…
Reference in a new issue