noisy 0.1.0

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.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 2.3.0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.5.2"
12
+ gem "rcov", ">= 0"
13
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,28 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.2)
5
+ git (1.2.5)
6
+ jeweler (1.5.2)
7
+ bundler (~> 1.0.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rake (0.8.7)
11
+ rcov (0.9.9)
12
+ rspec (2.3.0)
13
+ rspec-core (~> 2.3.0)
14
+ rspec-expectations (~> 2.3.0)
15
+ rspec-mocks (~> 2.3.0)
16
+ rspec-core (2.3.1)
17
+ rspec-expectations (2.3.0)
18
+ diff-lcs (~> 1.1.2)
19
+ rspec-mocks (2.3.0)
20
+
21
+ PLATFORMS
22
+ ruby
23
+
24
+ DEPENDENCIES
25
+ bundler (~> 1.0.0)
26
+ jeweler (~> 1.5.2)
27
+ rcov
28
+ rspec (~> 2.3.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Geoffrey Byers
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,8 @@
1
+ = noisy
2
+
3
+ Gem to allow easy generation of perlin noise
4
+
5
+ == License
6
+
7
+ Published under the MIT License, see LICENSE.txt for further details.
8
+
data/Rakefile ADDED
@@ -0,0 +1,50 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "noisy"
16
+ gem.homepage = "http://github.com/gdcbyers/noisy"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{Perlin Noise generation for Ruby}
19
+ gem.description = %Q{Gem to allow easy generation of perlin noise}
20
+ gem.email = "geoff@seaandco.com"
21
+ gem.authors = ["Geoffrey Byers"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rspec/core'
30
+ require 'rspec/core/rake_task'
31
+ RSpec::Core::RakeTask.new(:spec) do |spec|
32
+ spec.pattern = FileList['spec/**/*_spec.rb']
33
+ end
34
+
35
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
36
+ spec.pattern = 'spec/**/*_spec.rb'
37
+ spec.rcov = true
38
+ end
39
+
40
+ task :default => :spec
41
+
42
+ require 'rake/rdoctask'
43
+ Rake::RDocTask.new do |rdoc|
44
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
45
+
46
+ rdoc.rdoc_dir = 'rdoc'
47
+ rdoc.title = "noisy #{version}"
48
+ rdoc.rdoc_files.include('README*')
49
+ rdoc.rdoc_files.include('lib/**/*.rb')
50
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/lib/noisy.rb ADDED
@@ -0,0 +1 @@
1
+ require 'noisy/noisy'
@@ -0,0 +1,87 @@
1
+ module Noisy
2
+ class Noisy
3
+ def initialize(seed = 1, octaves = 4)
4
+ @seed = seed
5
+ @octaves = octaves
6
+ @persistence = 0.25
7
+ end
8
+
9
+ def raw_noise(x)
10
+ n = (x.to_i << 13) ^ x.to_i
11
+ (1.0 - ((n * (n * n * 15731 * @seed + 789221 * @seed) + 1376312589 * @seed) & 0x7fffffff) / 1073741824.0)
12
+ end
13
+
14
+ def raw_noise_2d(x, y)
15
+ n = (x + y * 57).to_i
16
+ n = (n << 13) ^ n
17
+ (1.0 - ((n * (n * n * 15731 * @seed + 789221 * @seed) + 1376312589 * @seed) & 0x7fffffff) / 1073741824.0)
18
+ end
19
+
20
+ def smooth_noise(x)
21
+ left = raw_noise(x - 1.0)
22
+ right = raw_noise(x + 1.0)
23
+ raw_noise(x)/2.0 + left/4.0 + right/4.0
24
+ end
25
+
26
+ def smooth_noise_2d(x, y)
27
+ corners = raw_noise_2d(x - 1, y - 1) + raw_noise_2d(x - 1, y + 1) + raw_noise_2d(x + 1, y - 1) + raw_noise_2d(x + 1, y + 1)
28
+ sides = raw_noise_2d(x, y - 1) + raw_noise_2d(x, y + 1) + raw_noise_2d(x - 1, y) + raw_noise_2d(x + 1, y)
29
+ center = raw_noise_2d(x, y)
30
+
31
+ center / 4 + sides / 8 + corners / 16
32
+ end
33
+
34
+ def linear_interpolate(a, b, x)
35
+ a * (1 - x) + b * x
36
+ end
37
+
38
+ def cosine_interpolate(a, b, x)
39
+ f = (1 - Math.cos(x * Math::PI)) / 2
40
+ a * (1 - f) + b * f
41
+ end
42
+
43
+ # alias interpolate linear_interpolate
44
+ alias interpolate cosine_interpolate
45
+
46
+ def interpolate_noise(x)
47
+ interpolate(smooth_noise(x.floor), smooth_noise(x.floor + 1), x - x.floor)
48
+ end
49
+
50
+ def interpolate_noise_2d(x, y)
51
+ a = interpolate(smooth_noise_2d(x.floor, y.floor), smooth_noise_2d(x.floor + 1, y.floor), x - x.floor)
52
+ b = interpolate(smooth_noise_2d(x.floor, y.floor + 1), smooth_noise_2d(x.floor + 1, y.floor + 1), x - x.floor)
53
+ interpolate(a, b, y - y.floor)
54
+ end
55
+
56
+ def perlin_noise(x)
57
+ total = 0.0
58
+ (0...@octaves).each do |i|
59
+ frequency = 2.0 ** i
60
+ amplitude = @persistence ** i
61
+ total += interpolate_noise(x * frequency) * amplitude
62
+ end
63
+ total
64
+ end
65
+
66
+ def perlin_noise_2d(x, y)
67
+ total = 0.0
68
+ (0...@octaves).each do |i|
69
+ frequency = 2.0 ** i
70
+ amplitude = @persistence ** i
71
+ total += interpolate_noise_2d(x * frequency, y * frequency) * amplitude
72
+ end
73
+ total
74
+ end
75
+
76
+ def perlin_noise_map(width, height)
77
+ noise = Array.new(width)
78
+ noise.map! { Array.new(height) }
79
+ (0...width).each do |x|
80
+ (0...height).each do |y|
81
+ noise[x][y] = perlin_noise_2d(x, y)
82
+ end
83
+ end
84
+ noise
85
+ end
86
+ end
87
+ end
data/noisy.gemspec ADDED
@@ -0,0 +1,65 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{noisy}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Geoffrey Byers"]
12
+ s.date = %q{2011-02-19}
13
+ s.description = %q{Gem to allow easy generation of perlin noise}
14
+ s.email = %q{geoff@seaandco.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "lib/noisy.rb",
29
+ "lib/noisy/noisy.rb",
30
+ "noisy.gemspec",
31
+ "spec/noisy_spec.rb",
32
+ "spec/spec_helper.rb"
33
+ ]
34
+ s.homepage = %q{http://github.com/gdcbyers/noisy}
35
+ s.licenses = ["MIT"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.5.0}
38
+ s.summary = %q{Perlin Noise generation for Ruby}
39
+ s.test_files = [
40
+ "spec/noisy_spec.rb",
41
+ "spec/spec_helper.rb"
42
+ ]
43
+
44
+ if s.respond_to? :specification_version then
45
+ s.specification_version = 3
46
+
47
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
48
+ s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
49
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
50
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
51
+ s.add_development_dependency(%q<rcov>, [">= 0"])
52
+ else
53
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
54
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
55
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
56
+ s.add_dependency(%q<rcov>, [">= 0"])
57
+ end
58
+ else
59
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
60
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
61
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
62
+ s.add_dependency(%q<rcov>, [">= 0"])
63
+ end
64
+ end
65
+
@@ -0,0 +1,131 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe 'Noisy::Noisy' do
4
+ before(:all) do
5
+ seed = 1 + rand(100)
6
+ @check_x = 1 + rand(10) + rand
7
+ @check_y = 1 + rand(10) + rand
8
+ puts "Running tests with seed #{seed} and check value [#{@check_x}, #{@check_y}]"
9
+
10
+ @noisy = Noisy::Noisy.new(seed)
11
+ end
12
+
13
+ describe '#raw_noise' do
14
+ it 'returns a number from -1.0 to 1.0' do
15
+ @noisy.raw_noise(@check_x).should be_within(1.0).of(0.0)
16
+ end
17
+
18
+ it 'returns the same number for the same input' do
19
+ v1 = @noisy.raw_noise(@check_x)
20
+ v2 = @noisy.raw_noise(@check_x)
21
+ v1.should == v2
22
+ end
23
+ end
24
+
25
+ describe '#raw_noise_2d' do
26
+ it 'returns a number from -1.0 to 1.0' do
27
+ @noisy.raw_noise_2d(@check_x, @check_y).should be_within(1.0).of(0.0)
28
+ end
29
+
30
+ it 'returns the same number for the same input' do
31
+ v1 = @noisy.raw_noise_2d(@check_x, @check_y)
32
+ v2 = @noisy.raw_noise_2d(@check_x, @check_y)
33
+ v1.should == v2
34
+ end
35
+ end
36
+
37
+ describe '#smooth_noise' do
38
+ it 'return a number not drastically different from near noise values' do
39
+
40
+ threshold = 0.6
41
+
42
+ v1 = @noisy.smooth_noise(@check_x - 1)
43
+ v2 = @noisy.smooth_noise(@check_x)
44
+ v3 = @noisy.smooth_noise(@check_x + 1)
45
+
46
+ v2.should be_within(threshold).of(v1)
47
+ v2.should be_within(threshold).of(v3)
48
+ end
49
+ end
50
+
51
+ describe 'interpolation' do
52
+ before(:each) do
53
+ @a, @b, @x = 0.0, 5.0, 0.5
54
+ end
55
+
56
+ describe '#linear_interpolate' do
57
+ it 'return a value between a and b' do
58
+ n = @noisy.linear_interpolate(@a, @b, @x)
59
+ n.should >= @a
60
+ n.should <= @b
61
+ end
62
+
63
+ it 'return a when x is 0' do
64
+ @noisy.linear_interpolate(@a, @b, 0.0).should == @a
65
+ end
66
+
67
+ it 'return b when x is 1' do
68
+ @noisy.linear_interpolate(@a, @b, 1.0).should == @b
69
+ end
70
+ end
71
+
72
+ describe '#cosine_interpolate' do
73
+ it 'return a value between a and b' do
74
+ n = @noisy.cosine_interpolate(@a, @b, @x)
75
+ n.should >= @a
76
+ n.should <= @b
77
+ end
78
+
79
+ it 'return a when x is 0' do
80
+ @noisy.cosine_interpolate(@a, @b, 0.0).should == @a
81
+ end
82
+
83
+ it 'return b when x is 1' do
84
+ @noisy.linear_interpolate(@a, @b, 1.0).should == @b
85
+ end
86
+ end
87
+ end
88
+
89
+ describe '#interpolate_noise' do
90
+ it 'returns a number from -1.0 to 1.0' do
91
+ @noisy.interpolate_noise(@check_x).should be_within(1.0).of(0.0)
92
+ end
93
+
94
+ it 'returns the same number for the same input' do
95
+ v1 = @noisy.interpolate_noise(@check_x)
96
+ v2 = @noisy.interpolate_noise(@check_x)
97
+ v1.should == v2
98
+ end
99
+ end
100
+
101
+ describe '#perlin_noise' do
102
+ it 'returns a number from -1.0 to 1.0' do
103
+ @noisy.perlin_noise(@check_x).should be_within(1.0).of(0.0)
104
+ end
105
+
106
+ it 'returns the same number for the same input' do
107
+ v1 = @noisy.perlin_noise(@check_x)
108
+ v2 = @noisy.perlin_noise(@check_x)
109
+ v1.should == v2
110
+ end
111
+ end
112
+
113
+ describe '#perlin_noise_2d' do
114
+ it 'returns a number from -1.0 to 1.0' do
115
+ @noisy.perlin_noise_2d(@check_x, @check_y).should be_within(1.0).of(0.0)
116
+ end
117
+
118
+ it 'returns the same number for the same input' do
119
+ v1 = @noisy.perlin_noise_2d(@check_x, @check_y)
120
+ v2 = @noisy.perlin_noise_2d(@check_x, @check_y)
121
+ v1.should == v2
122
+ end
123
+ end
124
+
125
+ describe '#perlin_noise_map' do
126
+ it 'returns a multidimensional array with perlin noise values for [x, y]' do
127
+ a = @noisy.perlin_noise_map(5, 5)
128
+ a.count.should == a.first.count
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'noisy'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: noisy
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Geoffrey Byers
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-02-19 00:00:00 -05:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rspec
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ~>
23
+ - !ruby/object:Gem::Version
24
+ version: 2.3.0
25
+ type: :development
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ~>
34
+ - !ruby/object:Gem::Version
35
+ version: 1.0.0
36
+ type: :development
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: jeweler
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: 1.5.2
47
+ type: :development
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: rcov
51
+ prerelease: false
52
+ requirement: &id004 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ type: :development
59
+ version_requirements: *id004
60
+ description: Gem to allow easy generation of perlin noise
61
+ email: geoff@seaandco.com
62
+ executables: []
63
+
64
+ extensions: []
65
+
66
+ extra_rdoc_files:
67
+ - LICENSE.txt
68
+ - README.rdoc
69
+ files:
70
+ - .document
71
+ - .rspec
72
+ - Gemfile
73
+ - Gemfile.lock
74
+ - LICENSE.txt
75
+ - README.rdoc
76
+ - Rakefile
77
+ - VERSION
78
+ - lib/noisy.rb
79
+ - lib/noisy/noisy.rb
80
+ - noisy.gemspec
81
+ - spec/noisy_spec.rb
82
+ - spec/spec_helper.rb
83
+ has_rdoc: true
84
+ homepage: http://github.com/gdcbyers/noisy
85
+ licenses:
86
+ - MIT
87
+ post_install_message:
88
+ rdoc_options: []
89
+
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: "0"
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: "0"
104
+ requirements: []
105
+
106
+ rubyforge_project:
107
+ rubygems_version: 1.5.0
108
+ signing_key:
109
+ specification_version: 3
110
+ summary: Perlin Noise generation for Ruby
111
+ test_files:
112
+ - spec/noisy_spec.rb
113
+ - spec/spec_helper.rb