Class: UnicodePlot::Canvas
- Inherits:
-
Object
- Object
- UnicodePlot::Canvas
- Includes:
- BorderPrinter
- Defined in:
- lib/unicode_plot/canvas.rb
Direct Known Subclasses
Constant Summary
Constants included from StyledPrinter
StyledPrinter::COLOR_DECODE, StyledPrinter::COLOR_ENCODE, StyledPrinter::DISABLE_TEXT_STYLE, StyledPrinter::TEXT_COLORS
Instance Attribute Summary collapse
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#origin_x ⇒ Object
readonly
Returns the value of attribute origin_x.
-
#origin_y ⇒ Object
readonly
Returns the value of attribute origin_y.
-
#pixel_height ⇒ Object
readonly
Returns the value of attribute pixel_height.
-
#pixel_width ⇒ Object
readonly
Returns the value of attribute pixel_width.
-
#plot_height ⇒ Object
readonly
Returns the value of attribute plot_height.
-
#plot_width ⇒ Object
readonly
Returns the value of attribute plot_width.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
-
#x_pixel_per_char ⇒ Object
readonly
Returns the value of attribute x_pixel_per_char.
-
#y_pixel_per_char ⇒ Object
readonly
Returns the value of attribute y_pixel_per_char.
Class Method Summary collapse
Instance Method Summary collapse
- #char_at(x, y) ⇒ Object
- #color_at(x, y) ⇒ Object
- #index_at(x, y) ⇒ Object
-
#initialize(width, height, pixel_width, pixel_height, fill_char, origin_x: 0, origin_y: 0, plot_width: 1, plot_height: 1, x_pixel_per_char: 1, y_pixel_per_char: 1) ⇒ Canvas
constructor
A new instance of Canvas.
-
#line!(x1, y1, x2, y2, color) ⇒ Object
digital differential analyzer algorithm.
- #lines!(x, y, color = :normal) ⇒ Object
- #point!(x, y, color) ⇒ Object
- #points!(x, y, color = :normal) ⇒ Object
- #print(out) ⇒ Object
- #show(out) ⇒ Object
Methods included from BorderPrinter
#print_border_bottom, #print_border_top
Methods included from StyledPrinter
#color?, #print_color, #print_styled
Constructor Details
#initialize(width, height, pixel_width, pixel_height, fill_char, origin_x: 0, origin_y: 0, plot_width: 1, plot_height: 1, x_pixel_per_char: 1, y_pixel_per_char: 1) ⇒ Canvas
Returns a new instance of Canvas.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/unicode_plot/canvas.rb', line 20 def initialize(width, height, pixel_width, pixel_height, fill_char, origin_x: 0, origin_y: 0, plot_width: 1, plot_height: 1, x_pixel_per_char: 1, y_pixel_per_char: 1) @width = width @height = height @pixel_width = check_positive(pixel_width, :pixel_width) @pixel_height = check_positive(pixel_height, :pixel_height) @origin_x = origin_x @origin_y = origin_y @plot_width = plot_width @plot_height = plot_height @x_pixel_per_char = x_pixel_per_char @y_pixel_per_char = y_pixel_per_char @grid = Array.new(@width * @height, fill_char) @colors = Array.new(@width * @height, COLOR_ENCODE[:normal]) end |
Instance Attribute Details
#height ⇒ Object (readonly)
Returns the value of attribute height.
42 43 44 |
# File 'lib/unicode_plot/canvas.rb', line 42 def height @height end |
#origin_x ⇒ Object (readonly)
Returns the value of attribute origin_x.
45 46 47 |
# File 'lib/unicode_plot/canvas.rb', line 45 def origin_x @origin_x end |
#origin_y ⇒ Object (readonly)
Returns the value of attribute origin_y.
46 47 48 |
# File 'lib/unicode_plot/canvas.rb', line 46 def origin_y @origin_y end |
#pixel_height ⇒ Object (readonly)
Returns the value of attribute pixel_height.
44 45 46 |
# File 'lib/unicode_plot/canvas.rb', line 44 def pixel_height @pixel_height end |
#pixel_width ⇒ Object (readonly)
Returns the value of attribute pixel_width.
43 44 45 |
# File 'lib/unicode_plot/canvas.rb', line 43 def pixel_width @pixel_width end |
#plot_height ⇒ Object (readonly)
Returns the value of attribute plot_height.
48 49 50 |
# File 'lib/unicode_plot/canvas.rb', line 48 def plot_height @plot_height end |
#plot_width ⇒ Object (readonly)
Returns the value of attribute plot_width.
47 48 49 |
# File 'lib/unicode_plot/canvas.rb', line 47 def plot_width @plot_width end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
41 42 43 |
# File 'lib/unicode_plot/canvas.rb', line 41 def width @width end |
#x_pixel_per_char ⇒ Object (readonly)
Returns the value of attribute x_pixel_per_char.
49 50 51 |
# File 'lib/unicode_plot/canvas.rb', line 49 def x_pixel_per_char @x_pixel_per_char end |
#y_pixel_per_char ⇒ Object (readonly)
Returns the value of attribute y_pixel_per_char.
50 51 52 |
# File 'lib/unicode_plot/canvas.rb', line 50 def y_pixel_per_char @y_pixel_per_char end |
Class Method Details
.create(canvas_type, width, height, **kw) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/unicode_plot/canvas.rb', line 5 def self.create(canvas_type, width, height, **kw) case canvas_type when :ascii AsciiCanvas.new(width, height, **kw) when :braille BrailleCanvas.new(width, height, **kw) when :density DensityCanvas.new(width, height, **kw) when :dot DotCanvas.new(width, height, **kw) else raise ArgumentError, "unknown canvas type: #{canvas_type}" end end |
Instance Method Details
#char_at(x, y) ⇒ Object
74 75 76 |
# File 'lib/unicode_plot/canvas.rb', line 74 def char_at(x, y) @grid[index_at(x, y)] end |
#color_at(x, y) ⇒ Object
78 79 80 |
# File 'lib/unicode_plot/canvas.rb', line 78 def color_at(x, y) @colors[index_at(x, y)] end |
#index_at(x, y) ⇒ Object
82 83 84 85 |
# File 'lib/unicode_plot/canvas.rb', line 82 def index_at(x, y) return nil unless 0 <= x && x < width && 0 <= y && y < height y * width + x end |
#line!(x1, y1, x2, y2, color) ⇒ Object
digital differential analyzer algorithm
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/unicode_plot/canvas.rb', line 115 def line!(x1, y1, x2, y2, color) if (x1 < origin_x && x2 < origin_x) || (x1 > origin_x + plot_width && x2 > origin_x + plot_width) return color end if (y1 < origin_y && y2 < origin_y) || (y1 > origin_y + plot_height && y2 > origin_y + plot_height) return color end toff = x1 - origin_x px1 = toff.fdiv(plot_width) * pixel_width toff = x2 - origin_x px2 = toff.fdiv(plot_width) * pixel_width toff = y1 - origin_y py1 = pixel_height - toff.fdiv(plot_height) * pixel_height toff = y2 - origin_y py2 = pixel_height - toff.fdiv(plot_height) * pixel_height dx = px2 - px1 dy = py2 - py1 nsteps = dx.abs > dy.abs ? dx.abs : dy.abs inc_x = dx.fdiv(nsteps) inc_y = dy.fdiv(nsteps) cur_x = px1 cur_y = py1 pixel!(cur_x.floor, cur_y.floor, color) 1.upto(nsteps) do |i| cur_x += inc_x cur_y += inc_y pixel!(cur_x.floor, cur_y.floor, color) end color end |
#lines!(x, y, color = :normal) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/unicode_plot/canvas.rb', line 152 def lines!(x, y, color = :normal) if x.length != y.length raise ArgumentError, "x and y must be the same length" end unless x.length > 0 raise ArgumentError, "x and y must not be empty" end (0 ... (x.length - 1)).each do |i| line!(x[i], y[i], x[i+1], y[i+1], color) end end |
#point!(x, y, color) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/unicode_plot/canvas.rb', line 87 def point!(x, y, color) unless origin_x <= x && x <= origin_x + plot_width && origin_y <= y && y <= origin_y + plot_height return color end plot_offset_x = x - origin_x pixel_x = plot_offset_x.fdiv(plot_width) * pixel_width plot_offset_y = y - origin_y pixel_y = pixel_height - plot_offset_y.fdiv(plot_height) * pixel_height pixel!(pixel_x.floor, pixel_y.floor, color) end |
#points!(x, y, color = :normal) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/unicode_plot/canvas.rb', line 102 def points!(x, y, color = :normal) if x.length != y.length raise ArgumentError, "x and y must be the same length" end unless x.length > 0 raise ArgumentError, "x and y must not be empty" end (0 ... x.length).each do |i| point!(x[i], y[i], color) end end |
#print(out) ⇒ Object
67 68 69 70 71 72 |
# File 'lib/unicode_plot/canvas.rb', line 67 def print(out) (0 ... height).each do |row_index| print_row(out, row_index) out.puts if row_index < height - 1 end end |
#show(out) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/unicode_plot/canvas.rb', line 52 def show(out) b = BorderMaps::BORDER_SOLID border_length = width print_border_top(out, "", border_length, :solid, color: :light_black) out.puts (0 ... height).each do |row_index| print_styled(out, b[:l], color: :light_black) print_row(out, row_index) print_styled(out, b[:r], color: :light_black) out.puts end print_border_bottom(out, "", border_length, :solid, color: :light_black) end |