Module: AsciiParadise::Heart
- Includes:
- Math
- Defined in:
- lib/ascii_paradise/static_ascii/heart.rb
Constant Summary collapse
- N =
"\n"
- RED =
The red colour.
"\033[31m"
- HEART =
#
HEART
#
RED + "\u2665"
- REVERT =
#
REVERT
#
"\e[0;37m"
- DEFAULT_SIZE =
#
DEFAULT_SIZE
#
25
Class Method Summary collapse
-
.build_heart(size = DEFAULT_SIZE) ⇒ Object
# === Heart.build_heart.
-
.draw_heart(size = DEFAULT_SIZE) ⇒ Object
# === Heart.draw_heart.
-
.restore_default_colour ⇒ Object
# === AsciiParadise.restore_default_colour ========================================================================= #.
-
.use_colours? ⇒ Boolean
# === Heart.use_colours? ========================================================================= #.
Instance Method Summary collapse
-
#draw_heart(size = DEFAULT_SIZE) ⇒ Object
# === draw_heart ========================================================================= #.
Class Method Details
.build_heart(size = DEFAULT_SIZE) ⇒ Object
#
Heart.build_heart
This method will return a String that will look like a heart.
#
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/ascii_paradise/static_ascii/heart.rb', line 60 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) ⇒ Object
#
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.
#
132 133 134 135 136 137 |
# File 'lib/ascii_paradise/static_ascii/heart.rb', line 132 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 ⇒ Object
#
AsciiParadise.restore_default_colour
#
51 52 53 |
# File 'lib/ascii_paradise/static_ascii/heart.rb', line 51 def self.restore_default_colour REVERT end |
.use_colours? ⇒ Boolean
#
Heart.use_colours?
#
44 45 46 |
# File 'lib/ascii_paradise/static_ascii/heart.rb', line 44 def self.use_colours? RED end |
Instance Method Details
#draw_heart(size = DEFAULT_SIZE) ⇒ Object
#
draw_heart
#
117 118 119 |
# File 'lib/ascii_paradise/static_ascii/heart.rb', line 117 def draw_heart(size = DEFAULT_SIZE) Heart.draw_heart(size) end |