File: term_mandel

package info (click to toggle)
ruby-term-ansicolor 1.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 1,104 kB
  • sloc: ruby: 1,269; makefile: 10
file content (52 lines) | stat: -rwxr-xr-x 1,669 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/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
  color =
    ac::Attribute[ color_random ].gradient_to(ac::Attribute[ color_random ], :steps => 16) +
    ac::Attribute[ color_random ].gradient_to(ac::Attribute[ color_random ], :steps => 16) +
    ac::Attribute[ color_random ].gradient_to(ac::Attribute[ color_random ], :steps => 16) +
    ac::Attribute[ color_random ].gradient_to(ac::Attribute[ color_random ], :steps => 16) +
    ac::Attribute[ color_random ].gradient_to(ac::Attribute[ color_random ], :steps => 16) +
    ac::Attribute[ color_random ].gradient_to(ac::Attribute[ color_random ], :steps => 16) +
    ac::Attribute[ color_random ].gradient_to(ac::Attribute[ color_random ], :steps => 16) +
    ac::Attribute[ color_random ].gradient_to(ac::Attribute['#000'], :steps => 16)
  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:'

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