module AsciiParadise::Heart

Constants

DEFAULT_SIZE
#

DEFAULT_SIZE

#
HEART
#

HEART

#
N
RED
REVERT
#

REVERT

#

Public Class Methods

build_heart(size = DEFAULT_SIZE) click to toggle source
#

Heart.build_heart

This method will return a String that will look like a heart.

#
# File lib/ascii_paradise/static_ascii/heart.rb, line 55
def self.build_heart(size = DEFAULT_SIZE)
  size = size.first if size.is_a? Array
  if size.is_a? Hash and size.has_key?(:size)
    size = size.delete :size
  end
  size = size.to_i # Need an Integer.
  size = DEFAULT_SIZE unless size
  size = 3 if size < 3 # Minimum size is 3.
  # ======================================================================= #
  # Next, determine px, py and r.
  # ======================================================================= #
  px = size * 0.5
  py = size * 0.37
  r  = size * 0.625
  # ======================================================================= #
  # Determine y_nums next. Will be an Array starting from e. g. 100,
  # down to 1.
  # ======================================================================= #
  y_nums = (1..size).map {|m| m }.reverse
  # ======================================================================= #
  # Determine the matrix to use next. This matrix will have true and
  # false values.
  # ======================================================================= #
  array_matrix = y_nums.map { |y|
    (1..size).map { |x|
      if y > size * 0.7
        Math.sqrt( (px - x) ** 2 + (py - y) ** 2 ) < r
      else
        x < 1.7 * y
      end
    }
  }
  array_matrix[0][0] = false
  array_matrix[1][0] = false if array_matrix.size > 1
  # ======================================================================= #
  # Next, we replace the true and false values with '@' and ' '.
  # We use use_colours? method to put things into a red colour.
  # Red because a heart is red as well.
  # ======================================================================= #
  result = use_colours? + array_matrix.map { |row|
    row = row.dup.reverse + row.dup
    # ===================================================================== #
    # Two characters are possible: '@', and ' '.
    # ===================================================================== #
    row = row.map {|b| b ? '@' : ' ' }
    row[0] = ' '
    row[row.size - 1] = ' '
    row << N # Append a newline here.
    row.join # Make it a string again finally.
  }.join
  result << restore_default_colour
  return result
end
draw_heart(size = DEFAULT_SIZE) click to toggle source
#

Heart.draw_heart

This is the method that will draw the heart in question.

This can be invoked like so:

AsciiParadis.draw_heart size: 60
AsciiParadis.draw_heart 60

The above two ways are essentially synonymous.

#
# File lib/ascii_paradise/static_ascii/heart.rb, line 127
def self.draw_heart(size = DEFAULT_SIZE)
  if size.is_a? Array and size.empty?
    size = DEFAULT_SIZE # If it is empty then it is useless anyway.
  end
  puts Heart.build_heart(size)
end
restore_default_colour() click to toggle source
#

AsciiParadise.restore_default_colour

#
# File lib/ascii_paradise/static_ascii/heart.rb, line 46
def self.restore_default_colour
  REVERT
end
use_colours?() click to toggle source
#

Heart.use_colours?

#
# File lib/ascii_paradise/static_ascii/heart.rb, line 39
def self.use_colours?
  RED
end

Public Instance Methods

draw_heart(size = DEFAULT_SIZE) click to toggle source
#

draw_heart

#
# File lib/ascii_paradise/static_ascii/heart.rb, line 112
def draw_heart(size = DEFAULT_SIZE)
  Heart.draw_heart(size)
end