fractal 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/bin/fractal +42 -4
  3. data/lib/fractal.rb +19 -10
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 45b8f6fa7a6f441266b58e9ef31a977d506184ca
4
- data.tar.gz: 16d849e8562d36b027ff10e0cf14b4939b5a07c5
3
+ metadata.gz: 99a86a0f26d2335d74151a20aaabfe6da583299b
4
+ data.tar.gz: 82cf591b8628ac0f466063c87cbf8cf8a251ff60
5
5
  SHA512:
6
- metadata.gz: 812a196b7ae1eaef40e896821e21b67f4a514d9535a48d5f7050866bd0c09be2c2a75e7066593c9b629050b0b92ca5248c2cb79951c777efabeeb0910d4b5d30
7
- data.tar.gz: 21b4e68b948b0a2471048a3011d501353d21a5a7a20a6d3950c46a941b1a0a3b8abc3474fbbe4770cb40c17c4df09d852ecc6bc79233ce7d87179063566055e1
6
+ metadata.gz: 79b069848c22308b1bb06f7f4eb80a6b600dd88a50d56fac2b394027e9d2ef950e63868134093b73cb28579614ef61e3ab9096a5e1bdb152d1137da8efbf9530
7
+ data.tar.gz: 8bc30cd1bd68b7c91849a7d9953a77674048bebf611bb0e6550676a4c7bb9d2857ee88b547bf805f5c61c60b210b6606f274606d825e0498ba5472d0343e9229
@@ -1,8 +1,46 @@
1
1
  #!/usr/bin/env ruby
2
2
  require 'fractal'
3
3
 
4
- png = ChunkyPNG::Image.new ARGV[0].to_i, ARGV[1].to_i
5
- fractal = Fractals::Mandelbrot.new png
6
- fractal.colorMode = 'rgb'
4
+ fractals = ['mandelbrot']
5
+ fractalType = String.new
7
6
 
8
- fractal.draw(ARGV[2].to_i, ARGV[3].to_i).save('mandelbrot-fractal.png')
7
+ ARGV.each do |arg|
8
+ fractals.each do |option|
9
+ fractalType = arg.downcase if arg.downcase == option
10
+ end
11
+ end
12
+
13
+ if fractalType.empty?
14
+ puts "Please provide a fractal type."
15
+ exit 1
16
+ end
17
+
18
+ width = height = 0
19
+ args = Hash[ARGV.join(' ').scan(/--?([^=\s]+)(?:=(\S+))?/)]
20
+ width = args['w'].to_i if args.key? 'w' # -w=2000
21
+ height = args['h'].to_i if args.key? 'h' # -h=1500
22
+
23
+ if width <= 0 || height <= 0
24
+ puts "Warning, width and/or height not provided.\nSetting to default: 300x300"
25
+ width = height = 300
26
+ end
27
+
28
+ png = ChunkyPNG::Image.new width, height
29
+ fractal = (
30
+ case fractalType
31
+ when 'mandelbrot'
32
+ Fractals::Mandelbrot.new png
33
+ else
34
+ Fractals::Mandelbrot.new png
35
+ end
36
+ )
37
+ fractal.colorMode = 'mono'
38
+ # TODO: Use colorMode option for multichromatic images.
39
+ fractal.colorMode = args['color'] if args.key? 'color' # --color=mono
40
+ fractal.colorMode = args['mode'] if args.key? 'mode' # --mode=mono
41
+
42
+ definition, scale = 255, 2
43
+ definition = args['def'].to_i if args.key? 'def' # --def=100
44
+ scale = args['scale'].to_i if args.key? 'scale' # --scale=1.5
45
+
46
+ fractal.draw(definition, scale).save("#{fractalType}-fractal.png")
@@ -11,33 +11,42 @@ module Fractals
11
11
  I = Complex 'i'
12
12
 
13
13
  class Mandelbrot
14
- attr_accessor :colorMode
14
+ attr_accessor :colorType
15
15
 
16
16
  def initialize image
17
17
  @width, @height = image.width, image.height
18
18
  @image = image
19
19
  end
20
20
 
21
- def draw definition=100, scale=2
22
- scale = scale.to_f
21
+ def calculate a, b, c_arr
22
+ ca, cb = c_arr
23
+ left = a * a - b * b
24
+ right = 2 * a * b
25
+ a = left + ca
26
+ b = right + cb
27
+
28
+ return [a, b]
29
+ end
30
+
31
+ def draw definition=255, scale=2
32
+ scaleWidth = scale.to_f
33
+ scaleHeight = scale.to_f * (@height.to_f / @width.to_f)
23
34
  definition = definition.to_f
24
35
  (0..@width - 1).each do |x|
25
36
  (0..@height - 1).each do |y|
26
- a = ca = drag(x, 0, @width, -scale, scale)
27
- b = cb = drag(y, 0, @height, -scale, scale)
37
+ a = ca = drag(x, 0, @width, -scaleWidth, scaleWidth)
38
+ b = cb = drag(y, 0, @height, -scaleHeight, scaleHeight)
28
39
 
29
40
  snap = 0
30
41
  while snap < definition
31
- left = a * a - b * b
32
- right = 2 * a * b
33
- a = left + ca
34
- b = right + cb
35
-
42
+ a, b = calculate(a, b, [ca, cb])
36
43
  if a * a + b * b > 16
37
44
  break
38
45
  end
39
46
  snap += 1
40
47
  end
48
+ # TODO: Use colorType as option non-monochromatic images
49
+ # (allow colourful fractals), currently only greyscale
41
50
  shade = drag(snap, 0, definition, 0, 1)
42
51
  shade = drag(Math.sqrt(shade), 0, 1, 0, 255)
43
52
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fractal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Demonstrandum