fractal 0.1.9 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/bin/fractal +22 -13
- data/lib/fractal.rb +19 -18
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 0c043fdee5986a21bc328754149982e0435ab815933ad075a653332605e8475a
|
4
|
+
data.tar.gz: f76814af1d2f281da912ba797ca7d5db4cbda220f222b2d4caeacf490a4fdcb3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fd86626a204a0ad115b9a2120835d643d6995c26e94969e48951ef4e3cccc31876dbcc2c07215f0cf7a1d0ed7ec69918b86ca964056682a6e2025631d599fa2e
|
7
|
+
data.tar.gz: 20e83dca134b2bc920438a5040c4b9328e269eebecdc2cd7660902b532daa8a43ef902d21401e1e31a74362921f8b2bb4f23db44aa79a071805ba7397574a349
|
data/bin/fractal
CHANGED
@@ -24,6 +24,13 @@ if args.key? 'help'
|
|
24
24
|
- `--scale=`, (optional), the zoom level of the image,
|
25
25
|
a higher value corresponds to a taller imaginary number line
|
26
26
|
thus a smaller fractal is seen. e.g. `--scale=2.25`
|
27
|
+
- `--offset=x,y` (optional), the offset will make you able
|
28
|
+
to position yourself where you like on the plan.
|
29
|
+
(offsetting where the centre is, before (0,0) was
|
30
|
+
the centre of the image, but now your offset is the centre).
|
31
|
+
Make sure you give an x and y value as numerics,
|
32
|
+
(ints and floats work both and don't leave any spaces
|
33
|
+
between the comma).
|
27
34
|
- `--complex=` (*only needed for the Julia set*)
|
28
35
|
this sets a complex coordinate for the Julia set,
|
29
36
|
in the form of `a±bi` ('a' and 'b' real numbers and
|
@@ -43,13 +50,13 @@ EOF
|
|
43
50
|
exit 0
|
44
51
|
end
|
45
52
|
|
46
|
-
|
47
|
-
|
53
|
+
allowed_fractals = ['mandelbrot', 'julia']
|
54
|
+
fractal_type = String.new
|
48
55
|
|
49
56
|
o = String.new
|
50
57
|
ARGV.each do |arg|
|
51
|
-
|
52
|
-
|
58
|
+
allowed_fractals.each do |option|
|
59
|
+
fractal_type = arg.downcase if arg.downcase == option
|
53
60
|
end
|
54
61
|
|
55
62
|
o = arg if arg[arg.length - 4, 4].downcase == '.png'
|
@@ -62,7 +69,7 @@ if o.include? '~'
|
|
62
69
|
end
|
63
70
|
|
64
71
|
|
65
|
-
if
|
72
|
+
if fractal_type.empty?
|
66
73
|
puts "Error: Please provide a fractal type.\nType `fractal --help` for help."
|
67
74
|
exit 1
|
68
75
|
end
|
@@ -85,35 +92,37 @@ if args.key? 'complex'
|
|
85
92
|
cb = cb.to_f
|
86
93
|
end
|
87
94
|
|
88
|
-
if
|
89
|
-
puts "Error: fractal type: '#{
|
95
|
+
if fractal_type == 'julia' && (ca.nil? || cb.nil?)
|
96
|
+
puts "Error: fractal type: '#{fractal_type}' requires complex coordinate,\n in form of '±c₁±c₂i', for example: -0.416+0.8i"
|
90
97
|
exit 1
|
91
98
|
end
|
92
99
|
|
93
100
|
png = ChunkyPNG::Image.new width, height
|
94
101
|
|
95
|
-
case
|
102
|
+
case fractal_type
|
96
103
|
when 'mandelbrot'
|
97
104
|
fractal = Fractals::Mandelbrot.new png
|
98
105
|
when 'julia'
|
99
106
|
fractal = Fractals::Julia.new png
|
100
107
|
fractal.real = ca
|
101
|
-
fractal.
|
108
|
+
fractal.imaginary = cb
|
102
109
|
else
|
103
110
|
fractal = Fractals::Mandelbrot.new png
|
104
111
|
end
|
105
112
|
|
106
113
|
|
107
|
-
fractal.
|
108
|
-
fractal.
|
114
|
+
fractal.colouring = args['color'] if args.key? 'color' # --color=mono
|
115
|
+
fractal.colouring = args['mode'] if args.key? 'mode' # --mode=mono
|
109
116
|
|
110
117
|
definition, scale = 255, 2.0
|
111
118
|
definition = args['def'].to_i if args.key? 'def' # --def=100
|
112
119
|
scale = args['scale'].to_f if args.key? 'scale' # --scale=1.5
|
120
|
+
offset = [0, 0]
|
121
|
+
offset = args['offset'].split(',').map(&:to_f) if args.key? 'offset'
|
113
122
|
|
114
123
|
unless o.empty?
|
115
|
-
fractal.draw(definition, scale).save(o)
|
124
|
+
fractal.draw(definition, scale, offset).save(o)
|
116
125
|
exit 0
|
117
126
|
end
|
118
127
|
|
119
|
-
fractal.draw(definition, scale).save("#{
|
128
|
+
fractal.draw(definition, scale, offset).save("#{fractal_type}-fractal.png")
|
data/lib/fractal.rb
CHANGED
@@ -8,10 +8,8 @@ def drag pos, pos_min, pos_max, out_min, out_max
|
|
8
8
|
end # drag(x, x_min_val, x_max_val, mapped_to_min_val, mapped_to_max_val), the same as the popular map(...) function.
|
9
9
|
|
10
10
|
module Fractals
|
11
|
-
I = Complex 'i'
|
12
|
-
|
13
11
|
class Mandelbrot
|
14
|
-
attr_accessor :
|
12
|
+
attr_accessor :colouring
|
15
13
|
|
16
14
|
def initialize image
|
17
15
|
@width, @height = image.width, image.height
|
@@ -28,35 +26,35 @@ module Fractals
|
|
28
26
|
return [a, b]
|
29
27
|
end
|
30
28
|
|
31
|
-
def draw definition=255, scale=2.0
|
29
|
+
def draw definition=255, scale=2.0, offset=[0,0]
|
32
30
|
scaleWidth = scale
|
33
31
|
scaleHeight = scale.to_f * (@height.to_f / @width.to_f)
|
34
32
|
definition = definition.to_f
|
35
33
|
(0..@width - 1).each do |x|
|
36
34
|
(0..@height - 1).each do |y|
|
37
|
-
a = ca = drag
|
38
|
-
b = cb = drag
|
35
|
+
a = ca = drag x, 0, @width, -scaleWidth + offset[0], scaleWidth + offset[0]
|
36
|
+
b = cb = drag y, 0, @height, -scaleHeight - offset[1], scaleHeight - offset[1]
|
39
37
|
|
40
38
|
snap = 0
|
41
39
|
while snap < definition
|
42
|
-
a, b = calculate
|
40
|
+
a, b = calculate a, b, [ca, cb]
|
43
41
|
if a * a + b * b > 16
|
44
42
|
break
|
45
43
|
end
|
46
44
|
snap += 1
|
47
45
|
end
|
48
46
|
|
49
|
-
case @
|
47
|
+
case @colouring
|
50
48
|
when 'multichromatic', 'multi', 'rainbow'
|
51
49
|
brightness = 1
|
52
50
|
brightness = 0 if snap == definition
|
53
|
-
hue = drag
|
54
|
-
hue = drag(Math.sqrt
|
51
|
+
hue = drag snap, 0, definition, 0, 1
|
52
|
+
hue = drag (Math.sqrt hue), 0, 1, 0, 360
|
55
53
|
|
56
54
|
@image[x, y] = ChunkyPNG::Color.from_hsv hue, 1, brightness
|
57
55
|
else
|
58
|
-
shade = drag
|
59
|
-
shade = drag(Math.sqrt
|
56
|
+
shade = drag snap, 0, definition, 0, 1
|
57
|
+
shade = drag (Math.sqrt shade), 0, 1, 0, 255
|
60
58
|
r, g, b = [shade.round.to_i] * 3
|
61
59
|
|
62
60
|
@image[x, y] = ChunkyPNG::Color.rgb r, g, b
|
@@ -67,17 +65,20 @@ module Fractals
|
|
67
65
|
end
|
68
66
|
end
|
69
67
|
|
70
|
-
class Julia < Mandelbrot
|
68
|
+
class Julia < Mandelbrot
|
69
|
+
# Since the Julia set only has a slightly modified calculation
|
71
70
|
attr_accessor :real
|
72
|
-
attr_accessor :
|
71
|
+
attr_accessor :imaginary
|
73
72
|
|
74
|
-
def calculate a, b, c_arr
|
73
|
+
def calculate a, b, c_arr
|
74
|
+
# c_arr is irrelevant as c is now constant,
|
75
|
+
# however the #draw() still supplies it and
|
76
|
+
# I don't want to rewrite draw when it already exists.
|
75
77
|
left = a * a - b * b
|
76
78
|
right = 2 * a * b
|
77
|
-
a = left + @real
|
78
|
-
b = right + @
|
79
|
+
a = left + @real # z^2 + c
|
80
|
+
b = right + @imaginary
|
79
81
|
return [a, b]
|
80
82
|
end
|
81
83
|
end
|
82
|
-
# module space
|
83
84
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fractal
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Demonstrandum
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-07-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: chunky_png
|
@@ -30,7 +30,7 @@ dependencies:
|
|
30
30
|
- - ">="
|
31
31
|
- !ruby/object:Gem::Version
|
32
32
|
version: 1.3.8
|
33
|
-
description: Creates PNG images of fractals.
|
33
|
+
description: Creates PNG images of complex plane fractals.
|
34
34
|
email: knutsen@jetspace.co
|
35
35
|
executables:
|
36
36
|
- fractal
|
@@ -59,7 +59,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
59
59
|
version: '0'
|
60
60
|
requirements: []
|
61
61
|
rubyforge_project:
|
62
|
-
rubygems_version: 2.
|
62
|
+
rubygems_version: 2.7.3
|
63
63
|
signing_key:
|
64
64
|
specification_version: 4
|
65
65
|
summary: Draws fractal PNG images.
|