fractalsrb 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/FractalsRB.gemspec +17 -0
- data/README.md +51 -0
- data/lib/Canvas.rb +41 -0
- data/lib/Similar.rb +35 -0
- data/lib/fractalsrb.rb +3 -0
- metadata +62 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d912f05a2a984e71ac652e48baa996fb343213d3e61f57f3a336bac531f72bc9
|
4
|
+
data.tar.gz: a5f2e4b5be65f36ca32ceef69eef19ef0543a3ca3908cb27397291691528ea06
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f1d9355f28fe473dd6d6959bc6ce54902be003f05315c047c93ee7942cfc71aebc10983132ba66767823a4c7d70829e92cadabf8949a6b08ca28860ff4f0830b
|
7
|
+
data.tar.gz: 81a9a7a35de576643a690c196a668f8eb6a0323b39f609c0eca7ad10499a169260bdd7940db962dfe677177836ffe4b0fdd2b319217aa15c1d0d4e0e50726a19
|
data/FractalsRB.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# lib = File.expand_path("../lib", __FILE__)
|
2
|
+
# $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "fractalsrb"
|
6
|
+
s.version = "0.0.1"
|
7
|
+
s.authors = ["Stefano Martin"]
|
8
|
+
s.email = ["stefano.martin87@gmail.com"]
|
9
|
+
s.homepage = "https://github.com/StefanoMartin/FractalsRB"
|
10
|
+
s.license = "MIT"
|
11
|
+
s.summary = "A gem to create fractals"
|
12
|
+
s.description = "A gem to create fractals"
|
13
|
+
s.platform = Gem::Platform::RUBY
|
14
|
+
s.require_paths = ["lib"]
|
15
|
+
s.files = ["lib/*", "FractalsRB.gemspec", "LICENSE.md", "README.md"].map {|f| `git ls-files #{f}`.split("\n") }.to_a.flatten
|
16
|
+
s.add_runtime_dependency "chunky_png", ">= 1.3.11"
|
17
|
+
end
|
data/README.md
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
FractalsRB
|
2
|
+
=======================================================================0
|
3
|
+
|
4
|
+
Fractals are something nice and fantastic to explore.
|
5
|
+
This is a small gem to create fractals similar to the [Pithagoras tree](https://en.wikipedia.org/wiki/Pythagoras_tree_(fractal)).
|
6
|
+
|
7
|
+
The main idea is that these fractals are iterations of a translation, a rotation and a resizing of the original object.
|
8
|
+
|
9
|
+
To install FinnhubRB: `gem install fractalsrb`
|
10
|
+
|
11
|
+
To use it in your application: `require "fractalsrb"`
|
12
|
+
|
13
|
+
## How to use it
|
14
|
+
|
15
|
+
``` ruby
|
16
|
+
similar = Fractal::Similar.new(repetition: 8, size_square: 200.0) # Create an instance with 7 repetition and starting size_square of 100px
|
17
|
+
similar.add(x: 0, y: 200, rotation: 45, size: 0.72) # Add a square that reproduce itself by translating by 40px on the x-axis, by 50px on the y-axis, rotate of 45° and each iteration will be half bigger than the previous one
|
18
|
+
similar.add(x: 100, y: 300, rotation: 315, size: 0.72) # Add another iteration
|
19
|
+
similar.build # Build the fractal
|
20
|
+
similar.points # Retrieve the points (each 4 points is a square)
|
21
|
+
```
|
22
|
+
|
23
|
+
You can obtain directly a picture by using the following way:
|
24
|
+
|
25
|
+
``` ruby
|
26
|
+
canvas = Fractal::Canvas.new(width: 3000, height: 3000, size_square: 200.0, repetition: 7,
|
27
|
+
fractals: [{x: 0, y: 200, rotation: 45, size: 0.72}, {x: 100, y: 300, rotation: 315, size: 0.72}])
|
28
|
+
# Create a canvas instance. Width and height will be the size of the resulted png in pixels
|
29
|
+
canvas.build # Build the canvas
|
30
|
+
canvas.picture # Return the file
|
31
|
+
```
|
32
|
+
|
33
|
+
## How to test it
|
34
|
+
|
35
|
+
The best way to see the potential of this small gem is by using the small website provided inside of the Github repository (not in the gem).
|
36
|
+
|
37
|
+
The steps are simple:
|
38
|
+
|
39
|
+
1. Clone this repository
|
40
|
+
2. Use ```bundle install```
|
41
|
+
3. Run ```rake``` (port 4567 will be freed)
|
42
|
+
|
43
|
+
This will start a small server on port 4567 and it will open your browser for a testing website.
|
44
|
+
Keep in mind that it can be very expensive, expecially if you use a high number of repetitions (> 10) and iterations.
|
45
|
+
|
46
|
+
The results in my opinion can be astonishing. Some examples:
|
47
|
+
|
48
|
+
![4](picture/4.png)
|
49
|
+
![3](picture/3.png)
|
50
|
+
![2](picture/2.png)
|
51
|
+
![1](picture/1.png)
|
data/lib/Canvas.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
module Fractal
|
2
|
+
class Canvas
|
3
|
+
def initialize(width: 1000, height: 1000, repetition:, size_square: 100.0,
|
4
|
+
fractals:)
|
5
|
+
@width = width
|
6
|
+
@height = height
|
7
|
+
fractal = Fractal::Similar.new(repetition: repetition, size_square: size_square)
|
8
|
+
fractals.each{|f| fractal.add(**f)}
|
9
|
+
fractal.build
|
10
|
+
@points = fractal.points
|
11
|
+
@canvas = ChunkyPNG::Canvas.new(width, height, ChunkyPNG::Color::WHITE)
|
12
|
+
build
|
13
|
+
picture = @canvas.to_image
|
14
|
+
picture.save("#{__dir__}/filename.png", :interlace => true)
|
15
|
+
end
|
16
|
+
|
17
|
+
def picture
|
18
|
+
File.open("#{__dir__}/filename.png")
|
19
|
+
end
|
20
|
+
|
21
|
+
def build
|
22
|
+
i = -1
|
23
|
+
half_width = @width/2
|
24
|
+
half_height = @height/2
|
25
|
+
|
26
|
+
@points.each do |point|
|
27
|
+
i += 1
|
28
|
+
next if i % 4 != 0
|
29
|
+
|
30
|
+
square = [
|
31
|
+
ChunkyPNG::Point.new(@points[i][0]+half_width, -@points[i][1]+half_height),
|
32
|
+
ChunkyPNG::Point.new(@points[i+1][0]+half_width, -@points[i+1][1]+half_height),
|
33
|
+
ChunkyPNG::Point.new(@points[i+2][0]+half_width, -@points[i+2][1]+half_height),
|
34
|
+
ChunkyPNG::Point.new(@points[i+3][0]+half_width, -@points[i+3][1]+half_height)
|
35
|
+
]
|
36
|
+
|
37
|
+
@canvas.polygon(square)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/lib/Similar.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
module Fractal
|
2
|
+
class Similar
|
3
|
+
INITIAL_POINT = [[0.0,0.0], [1.0,0.0], [1.0,1.0], [0.0,1.0]].freeze
|
4
|
+
|
5
|
+
def initialize(repetition:, size_square: 100.0)
|
6
|
+
@new_points = INITIAL_POINT.map{|t| [t[0]*size_square, t[1]*size_square]}
|
7
|
+
@points = @new_points.dup
|
8
|
+
@repetition = repetition
|
9
|
+
@iteration = []
|
10
|
+
end
|
11
|
+
|
12
|
+
def add(x:, y:, rotation:, size:)
|
13
|
+
rotation_temp = rotation* Math::PI / 180
|
14
|
+
@iteration << [[x,y], [Math.cos(rotation_temp), Math.sin(rotation_temp)], size]
|
15
|
+
end
|
16
|
+
|
17
|
+
attr_reader :points
|
18
|
+
|
19
|
+
def build
|
20
|
+
@repetition.times do
|
21
|
+
new_points = []
|
22
|
+
@iteration.each do |i|
|
23
|
+
@new_points.each do |po|
|
24
|
+
new_points << [
|
25
|
+
i[2] * (i[1][0]*po[0]-i[1][1]*po[1]) + i[0][0],
|
26
|
+
i[2] * (i[1][1]*po[0]+i[1][0]*po[1]) + i[0][1]
|
27
|
+
]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
@points += new_points
|
31
|
+
@new_points = new_points
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/fractalsrb.rb
ADDED
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fractalsrb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Stefano Martin
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-04-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: chunky_png
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.3.11
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.3.11
|
27
|
+
description: A gem to create fractals
|
28
|
+
email:
|
29
|
+
- stefano.martin87@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- FractalsRB.gemspec
|
35
|
+
- README.md
|
36
|
+
- lib/Canvas.rb
|
37
|
+
- lib/Similar.rb
|
38
|
+
- lib/fractalsrb.rb
|
39
|
+
homepage: https://github.com/StefanoMartin/FractalsRB
|
40
|
+
licenses:
|
41
|
+
- MIT
|
42
|
+
metadata: {}
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
requirements: []
|
58
|
+
rubygems_version: 3.0.6
|
59
|
+
signing_key:
|
60
|
+
specification_version: 4
|
61
|
+
summary: A gem to create fractals
|
62
|
+
test_files: []
|