| 1 |
|
| 2 |
|
| 3 |
require 'term/ansicolor' |
| 4 |
include Term::ANSIColor |
| 5 |
require 'tins/xt' |
| 6 |
include Tins::GO |
| 7 |
require "complex" |
| 8 |
|
| 9 |
$width, $height = Tins::Terminal.cols, Tins::Terminal.lines |
| 10 |
$height *= 2 |
| 11 |
|
| 12 |
def color_random |
| 13 |
(1..3).map { rand(255) } |
| 14 |
end |
| 15 |
|
| 16 |
def draw_set(rx, ry) |
| 17 |
sx = (rx.end - rx.begin).abs / $width |
| 18 |
sy = (ry.end - ry.begin).abs / $height |
| 19 |
|
| 20 |
steps = 16 |
| 21 |
color = (5.times.map { color_random } + [ 0, 0, 0 ]).map { Attribute[_1] } |
| 22 |
color = color[1..-1].inject(color[0,1]) { |c, x| |
| 23 |
c + c.last.gradient_to(x, steps:) |
| 24 |
} |
| 25 |
iters = color.size - 2 |
| 26 |
|
| 27 |
data = [ [] ] |
| 28 |
for j in 0...$height |
| 29 |
for i in 0...$width |
| 30 |
n, z_n = 0, Complex(0, 0) |
| 31 |
c = Complex(sx * i + rx.begin, sy * j + ry.begin) |
| 32 |
while n <= iters |
| 33 |
break if z_n.abs > 2 |
| 34 |
z_n = z_n ** 2 + c |
| 35 |
n += 1 |
| 36 |
end |
| 37 |
data.last << n |
| 38 |
end |
| 39 |
data << [] |
| 40 |
end |
| 41 |
screen = '' |
| 42 |
(0...$height).step(2) do |j| |
| 43 |
(0...$width).each do |i| |
| 44 |
screen << color(color[data[j][i]]) << |
| 45 |
on_color(color[data[j + 1][i]]) << ?▀ |
| 46 |
end |
| 47 |
end |
| 48 |
print move_home, screen, reset |
| 49 |
end |
| 50 |
|
| 51 |
opts = go 'x:y' |
| 52 |
|
| 53 |
Term::ANSIColor.true_coloring = ENV['COLORTERM'] =~ /\A(truecolor|24bit)\z/ |
| 54 |
|
| 55 |
rx = opts[?x].full? { |r| Range.new(*(r.split('..', 2).map(&:to_f))) } || (-2.0..1.0) |
| 56 |
ry = opts[?y].full? { |r| Range.new(*(r.split('..', 2).map(&:to_f))) } || (-1.0..1.0) |
| 57 |
|
| 58 |
draw_set rx, ry |