How to resolve the algorithm Draw a cuboid step by step in the Elixir programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Draw a cuboid step by step in the Elixir programming language
Table of Contents
Problem Statement
Draw a cuboid with relative dimensions of 2 × 3 × 4.
The cuboid can be represented graphically, or in ASCII art, depending on the language capabilities. To fulfill the criteria of being a cuboid, three faces must be visible. Either static or rotational projection is acceptable for this task.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Draw a cuboid step by step in the Elixir programming language
Source code in the elixir programming language
defmodule Cuboid do
@x 6
@y 2
@z 3
@dir %{-: {1,0}, |: {0,1}, /: {1,1}}
def draw(nx, ny, nz) do
IO.puts "cuboid #{nx} #{ny} #{nz}:"
{x, y, z} = {@x*nx, @y*ny, @z*nz}
area = Map.new
area = Enum.reduce(0..nz-1, area, fn i,acc -> draw_line(acc, x, 0, @z*i, :-) end)
area = Enum.reduce(0..ny, area, fn i,acc -> draw_line(acc, x, @y*i, z+@y*i, :-) end)
area = Enum.reduce(0..nx-1, area, fn i,acc -> draw_line(acc, z, @x*i, 0, :|) end)
area = Enum.reduce(0..ny, area, fn i,acc -> draw_line(acc, z, x+@y*i, @y*i, :|) end)
area = Enum.reduce(0..nz-1, area, fn i,acc -> draw_line(acc, y, x, @z*i, :/) end)
area = Enum.reduce(0..nx, area, fn i,acc -> draw_line(acc, y, @x*i, z, :/) end)
Enum.each(y+z..0, fn j ->
IO.puts Enum.map_join(0..x+y, fn i -> Map.get(area, {i,j}, " ") end)
end)
end
defp draw_line(area, n, sx, sy, c) do
{dx, dy} = Map.get(@dir, c)
draw_line(area, n, sx, sy, c, dx, dy)
end
defp draw_line(area, n, _, _, _, _, _) when n<0, do: area
defp draw_line(area, n, i, j, c, dx, dy) do
Map.update(area, {i,j}, c, fn _ -> :+ end)
|> draw_line(n-1, i+dx, j+dy, c, dx, dy)
end
end
Cuboid.draw(2,3,4)
Cuboid.draw(1,1,1)
Cuboid.draw(2,4,1)
Cuboid.draw(4,2,1)
You may also check:How to resolve the algorithm Euler method step by step in the Swift programming language
You may also check:How to resolve the algorithm MD5/Implementation step by step in the Modula-3 programming language
You may also check:How to resolve the algorithm User input/Graphical step by step in the REXX programming language
You may also check:How to resolve the algorithm Terminal control/Ringing the terminal bell step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm Cumulative standard deviation step by step in the Crystal programming language