Sha256: 8474335129d4f1f7802fa52d88ebec5f3b5b3141d00deb739c46e1e0ab78f9be

Contents?: true

Size: 1.16 KB

Versions: 4

Compression:

Stored size: 1.16 KB

Contents

#!/usr/bin/env ruby

require 'term/ansicolor'
require 'tins/xt'
require "complex"
include Tins::GO

@width, @height = Tins::Terminal.cols, Tins::Terminal.lines

def color_random
  (1..3).map { rand(255) }
end

def draw_set(rx, ry)
  sx = (rx.end - rx.begin).abs / @width
  sy = (ry.end - ry.begin).abs / @height

  ac    = Term::ANSIColor
  steps = 16
  color = (5.times.map { color_random } + [ 0, 0, 0 ]).map { ac::Attribute[_1] }
  color = color[1..-1].inject(color[0,1]) { |c, x|
    c + c.last.gradient_to(x, steps:)
  }
  iters = color.size - 2

  text = ''
  for j in 0...@height
    for i in 0...@width
      n, z_n = 0, Complex(0, 0)
      c = Complex(sx * i + rx.begin, sy * j + ry.begin)
      while n <= iters
        break if z_n.abs > 2
        z_n = z_n ** 2 + c
        n += 1
      end
      text << ac.on_color(color[n]) << ' '
    end
    text << ac.reset << "\n"
  end
  puts text
end

opts = go 'x:y'

Term::ANSIColor.true_coloring = ENV['COLORTERM'] =~ /\A(truecolor|24bit)\z/

rx = opts[?x].full? { |r| Range.new(*(r.split('..', 2).map(&:to_f))) } || (-2.0..1.0)
ry = opts[?y].full? { |r| Range.new(*(r.split('..', 2).map(&:to_f))) } || (-1.0..1.0)

draw_set rx, ry

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
term-ansicolor-1.10.2 bin/term_mandel
term-ansicolor-1.10.1 bin/term_mandel
term-ansicolor-1.10.0 bin/term_mandel
term-ansicolor-1.9.0 bin/term_mandel