require 'matrix'

class Matrix
  def to_s
    "#{self[0, 0]} #{self[1, 0]}"
  end
end

def koch_curve(n, p1, p2)
  return if n.zero?
  angle = Math::PI / 3
  r = Matrix[[Math.cos(angle), -Math.sin(angle)],
             [Math.sin(angle), Math.cos(angle)]]
  s = (p1 * 2 + p2) / 3
  t = (p1 + p2 * 2) / 3
  points = t - s
  u = (r * points) + s
  koch_curve n - 1, p1, s
  puts s.to_s
  koch_curve n - 1, s, u
  puts u.to_s
  koch_curve n - 1, u, t
  puts t.to_s
  koch_curve n - 1, t, p2
end

n = gets.to_i
p1 = Matrix.column_vector([0.0, 0.0])
p2 = Matrix.column_vector([100.0, 0.0])
puts p1.to_s
koch_curve n, p1, p2
puts p2

