-- Untitled by Alex Makarovitsch.
-- Published in Computer Graphics and Art 1977.

-- Detailed as part of the Recoded Project.
-- http://recodeproject.com/artwork/v2n4untitled-makarovitssch-alex



-- require Pequod, installed via LuaRocks 
require("pequod")

-- Set randomseed to get get different random values each time the file is run. 
math.randomseed(os.clock() * 9999999999)



-- set constants for the drawing
-- change
local margin = 8
local shape_size = 60
local shape_offset = 6
local number_of_lines = 5
local rows = 5
local columns = 5



-- Create a peice of paper with a width and height based on the constants above
local paper = Paper(
  shape_size * columns + shape_offset * (number_of_lines - 1) + margin * 2,
  shape_size * rows + shape_offset * (number_of_lines - 1) + margin * 2
)

-- Create a pen. Set it to draw on the above peice of paper.
-- Give it a thickness of 2 units and the color 'black'.
local black_pen = Pen(paper, 2, 'black')



-- Draw an "S" shape by adding points to a path
-- Set the pen to draw the path
local function draw_shape (point)
  return Path({
    point:clone(),
    point:clone():move(0, shape_size),
    point:clone():move(shape_size / 2, shape_size),
    point:clone():move(shape_size / 2, 0),
    point:clone():move(shape_size, 0),
    point:clone():move(shape_size, shape_size),
  }):draw_with(black_pen)
end

-- For each cell draw the shape. 
-- Flip or roate the shape if asked.
local function draw_cell (point, flip, rotate)
  for i = 0, number_of_lines - 1 do
    local shape = draw_shape(
      point:clone():move(i * shape_offset, i * shape_offset)
    )

    shape:scale(1, flip)

    shape:rotate(rotate)
  end
end

-- Draw the cell for the grid. 
-- Randomly decide to flip or rotate the cell.  
for c = 0, columns - 1 do
  for r = 0, rows - 1 do
    local flip = {-1, 1}
          flip = flip[math.random(1, 2)]
    local rotate = math.random(0, 1) * 90
    
    draw_cell(
      Point(margin + shape_size * c, margin + shape_size * r), flip, rotate
    )
  end
end



-- Save an SVG for the drawing.
paper:save_svg('output/alex_makarovitsch.svg')