fractal 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/bin/fractal +42 -4
- data/lib/fractal.rb +19 -10
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 99a86a0f26d2335d74151a20aaabfe6da583299b
|
4
|
+
data.tar.gz: 82cf591b8628ac0f466063c87cbf8cf8a251ff60
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 79b069848c22308b1bb06f7f4eb80a6b600dd88a50d56fac2b394027e9d2ef950e63868134093b73cb28579614ef61e3ab9096a5e1bdb152d1137da8efbf9530
|
7
|
+
data.tar.gz: 8bc30cd1bd68b7c91849a7d9953a77674048bebf611bb0e6550676a4c7bb9d2857ee88b547bf805f5c61c60b210b6606f274606d825e0498ba5472d0343e9229
|
data/bin/fractal
CHANGED
@@ -1,8 +1,46 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
require 'fractal'
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
fractal.colorMode = 'rgb'
|
4
|
+
fractals = ['mandelbrot']
|
5
|
+
fractalType = String.new
|
7
6
|
|
8
|
-
|
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")
|
data/lib/fractal.rb
CHANGED
@@ -11,33 +11,42 @@ module Fractals
|
|
11
11
|
I = Complex 'i'
|
12
12
|
|
13
13
|
class Mandelbrot
|
14
|
-
attr_accessor :
|
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
|
22
|
-
|
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, -
|
27
|
-
b = cb = drag(y, 0, @height, -
|
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
|
-
|
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
|
|