sangaku-eyeball 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use 1.9.3@sangaku-eyeball --create
data/Gemfile ADDED
@@ -0,0 +1,24 @@
1
+ source :rubygems
2
+
3
+ gem 'gosu'
4
+ gem 'sangaku', '>=0.5.4'
5
+
6
+ group :development do
7
+ gem "awesome_print"
8
+ end
9
+
10
+ group :test do
11
+ gem "rake"
12
+ gem "rspec"
13
+ gem "yard"
14
+ gem "debugger"
15
+ end
16
+
17
+ group :document do
18
+ gem "rdoc"
19
+ gem "redcarpet"
20
+ end
21
+
22
+ group :gemify do
23
+ gem "jeweler"
24
+ end
@@ -0,0 +1,49 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ awesome_print (1.1.0)
5
+ columnize (0.3.6)
6
+ debugger (1.5.0)
7
+ columnize (>= 0.3.1)
8
+ debugger-linecache (~> 1.2.0)
9
+ debugger-ruby_core_source (~> 1.2.0)
10
+ debugger-linecache (1.2.0)
11
+ debugger-ruby_core_source (1.2.0)
12
+ diff-lcs (1.2.1)
13
+ git (1.2.5)
14
+ gosu (0.7.45)
15
+ jeweler (1.8.4)
16
+ bundler (~> 1.0)
17
+ git (>= 1.2.5)
18
+ rake
19
+ rdoc
20
+ json (1.7.7)
21
+ rake (10.0.3)
22
+ rdoc (4.0.0)
23
+ json (~> 1.4)
24
+ redcarpet (2.2.2)
25
+ rspec (2.13.0)
26
+ rspec-core (~> 2.13.0)
27
+ rspec-expectations (~> 2.13.0)
28
+ rspec-mocks (~> 2.13.0)
29
+ rspec-core (2.13.1)
30
+ rspec-expectations (2.13.0)
31
+ diff-lcs (>= 1.1.3, < 2.0)
32
+ rspec-mocks (2.13.0)
33
+ sangaku (0.5.4)
34
+ yard (0.8.5.2)
35
+
36
+ PLATFORMS
37
+ ruby
38
+
39
+ DEPENDENCIES
40
+ awesome_print
41
+ debugger
42
+ gosu
43
+ jeweler
44
+ rake
45
+ rdoc
46
+ redcarpet
47
+ rspec
48
+ sangaku (>= 0.5.4)
49
+ yard
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Agworld Pty. Ltd.
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.
@@ -0,0 +1,36 @@
1
+ Sangaku Eyeball
2
+ ===============
3
+
4
+ Sangaku Eyeball is a simple tool for visualising what the Sangaku gem can do.
5
+
6
+ [![Code Climate](https://codeclimate.com/github/agworld/sangaku-eyeball.png)](https://codeclimate.com/github/agworld/sangaku-eyeball)
7
+ [![Dependency Status](https://gemnasium.com/agworld/sangaku-eyeball.png)](https://gemnasium.com/agworld/sangaku-eyeball)
8
+ [![Build Status](https://secure.travis-ci.org/agworld/sangaku-eyeball.png)](http://travis-ci.org/#!/agworld/sangaku-eyeball)
9
+
10
+ Getting Started
11
+ ---------------
12
+
13
+ Simply `sudo gem install sangaku-eyeball` and then run `sangaku-eyeball`.
14
+
15
+ Example Usage
16
+ -------------
17
+
18
+ * Draw polygons with the mouse (LMB to place a point, RMB to complete the polygon).
19
+ * Press SPACE to find the "Pole of Inaccessibility".
20
+ * Press ESCAPE to exit.
21
+
22
+ Contributing to Sangaku Eyeball
23
+ -------------------------------
24
+
25
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
26
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
27
+ * Fork the project.
28
+ * Start a feature/bugfix branch.
29
+ * Commit and push until you are happy with your contribution.
30
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
31
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
32
+
33
+ Copyright
34
+ ---------
35
+
36
+ Copyright (c) 2013 Agworld Pty. Ltd. See LICENSE.txt for further details.
@@ -0,0 +1,42 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "sangaku-eyeball"
18
+ gem.homepage = "http://github.com/agworld/sangaku-eyeball"
19
+ gem.license = "MIT"
20
+ gem.summary = "Sangaku Eyeball is a simple tool for visualising what the Sangaku gem can do."
21
+ gem.description = "Sangaku Eyeball uses Gosu, a 2D Game Library, to display lines, polygons and so on as defined by the Sangaku gem. It allows you to draw arbitrary polygons and then locate the Pole of Inaccessibility (usually the best place to display a label) using a fast, iterative algorithm."
22
+ gem.email = "jason.hutchens@agworld.com.au"
23
+ gem.authors = ["Jason Hutchens"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = false
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'yard'
42
+ YARD::Rake::YardocTask.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,153 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'logger'
4
+ require 'rubygems'
5
+ Bundler.require(:default, :development)
6
+
7
+ PATH = File.join(File.dirname(__FILE__), '..', 'lib', 'sangaku-eyeball')
8
+
9
+ require File.join(PATH, 'drawable')
10
+
11
+ class GameWindow < Gosu::Window
12
+
13
+ def initialize
14
+ super 800, 600, false
15
+ self.caption = "The Sangaku Eyeball"
16
+ @cursor = Gosu::Image.new(self, File.join(PATH, 'assets', 'cursor.png'), true)
17
+ @target = Gosu::Image.new(self, File.join(PATH, 'assets', 'target.png'), true)
18
+ @poly = Sangaku::Polygon.new
19
+ @line = Sangaku::Line.new([0, 0], [0, 0])
20
+ @mouse = Sangaku::Point.new(0, 0)
21
+ @aabb = nil
22
+ @grid = nil
23
+ @stars = nil
24
+ @state = :waiting
25
+ @running = false
26
+ @smooth = false
27
+ end
28
+
29
+ def update
30
+ @mouse.x = mouse_x
31
+ @mouse.y = mouse_y
32
+ @line.p1 = @poly.points.last
33
+ @line.p2 = @mouse
34
+ if @running
35
+ _step
36
+ if @aabb.w < 8
37
+ @running = false
38
+ @state = :waiting
39
+ end
40
+ end
41
+ end
42
+
43
+ def button_down(id)
44
+ case id
45
+ when Gosu::KbEscape
46
+ if @state != :drawing
47
+ if @poly.length == 0
48
+ close
49
+ else
50
+ _clear
51
+ end
52
+ end
53
+ when Gosu::MsLeft
54
+ if @state == :waiting && @poly.length == 0
55
+ @state = :drawing
56
+ end
57
+ if @state == :drawing
58
+ @poly << @mouse
59
+ end
60
+ when Gosu::MsRight
61
+ if @state == :drawing
62
+ _commit
63
+ end
64
+ when Gosu::KbRight
65
+ if @poly.length > 2
66
+ if @state == :waiting
67
+ @state = :gridify
68
+ end
69
+ if @state != :drawing
70
+ _step
71
+ end
72
+ end
73
+ when Gosu::KbSpace
74
+ if @poly.length > 2 && @state == :waiting
75
+ @state = :gridify
76
+ end
77
+ if @state != :waiting && @state != :drawing
78
+ @running = !@running
79
+ end
80
+ end
81
+ end
82
+
83
+ def draw
84
+ @poly.draw(self)
85
+ @line.draw(self) if @state == :drawing
86
+ if @poly.closed?
87
+ @aabb.draw(self)
88
+ @aabb.mid.draw(@target, [-16, -16]) if @state != :fittest
89
+ @grid.draw(self) unless @grid.nil? || @state != :pointify
90
+ @stars.each { |s| s.draw(self) } unless @stars.nil? || @state != :fittest
91
+ end
92
+ @mouse.draw(@cursor, [-8, -8])
93
+ end
94
+
95
+ private
96
+
97
+ def _step
98
+ case @state
99
+ when :gridify
100
+ @grid = @aabb.to_grid(13)
101
+ @state = :pointify
102
+ when :pointify
103
+ @stars = @grid.get_stars(@poly)
104
+ @state = :fittest
105
+ when :fittest
106
+ @aabb.centre!(@stars.sort.first.center)
107
+ @state = :iterate
108
+ when :iterate
109
+ @stars = nil
110
+ @aabb *= 0.71
111
+ @state = :gridify
112
+ end
113
+ end
114
+
115
+ def _clear
116
+ @poly.clear!
117
+ @aabb = nil
118
+ @state = :waiting
119
+ @running = false
120
+ @grid = nil
121
+ @smooth = false
122
+ end
123
+
124
+ def _commit
125
+ @poly.close!
126
+ @aabb = @poly.aabb
127
+ @aabb.square!
128
+ @aabb *= 1.1
129
+ @state = :waiting
130
+ end
131
+
132
+ def _smooth(points)
133
+ retval = []
134
+ while points.length > 0
135
+ point = points.shift
136
+ retval.pop if _colinear?(retval[-2], retval[-1], point)
137
+ retval << point
138
+ end
139
+ retval
140
+ end
141
+
142
+ def _colinear?(p1, p2, p3)
143
+ return false if p1.nil? || p2.nil? || p3.nil?
144
+ l1 = Sangaku::Line.new(p1, p2)
145
+ l2 = Sangaku::Line.new(p1, p3)
146
+ l3 = Sangaku::Line.new(p2, p3)
147
+ 0.5*(l1.dot(l2)+l2.dot(l3)).abs > 0.9
148
+ end
149
+
150
+ end
151
+
152
+ window = GameWindow.new
153
+ window.show
File without changes
@@ -0,0 +1,68 @@
1
+ module Sangaku
2
+
3
+ class Point
4
+
5
+ def draw(image, offset)
6
+ image.draw(x + offset[0], y + offset[1], 0)
7
+ end
8
+
9
+ end
10
+
11
+ class Line
12
+
13
+ def draw(window, color = 0x88FFFFFF)
14
+ window.draw_line(*(p1.to_a), color, *(p2.to_a), color)
15
+ end
16
+
17
+ end
18
+
19
+ class Polygon
20
+
21
+ def draw(window, color = 0xFFFFFFFF)
22
+ lines.each { |line| line.draw(window, color) }
23
+ end
24
+
25
+ end
26
+
27
+ class AABB
28
+
29
+ def draw(window, color = 0x885555FF)
30
+ x1, y1 = min.to_a
31
+ x2, y2 = max.to_a
32
+ window.draw_line(x1, y1, color, x2, y1, color)
33
+ window.draw_line(x2, y1, color, x2, y2, color)
34
+ window.draw_line(x2, y2, color, x1, y2, color)
35
+ window.draw_line(x1, y2, color, x1, y1, color)
36
+ end
37
+
38
+ end
39
+
40
+ class Grid
41
+
42
+ def draw(window, color = 0x55aaaaFF)
43
+ @xs.each do |x|
44
+ window.draw_line(x, 0, color, x, window.height, color)
45
+ end
46
+ @ys.each do |y|
47
+ window.draw_line(0, y, color, window.width, y, color)
48
+ end
49
+ end
50
+
51
+ end
52
+
53
+ class Star
54
+
55
+ def draw(window, color = 0x88FF5555)
56
+ x = @center.x
57
+ y = @center.y
58
+ dx = 0.5 * @size.x
59
+ dy = 0.5 * @size.y
60
+ window.draw_line(x, y, color, x+dx, y, color)
61
+ window.draw_line(x, y, color, x-dx, y, color)
62
+ window.draw_line(x, y, color, x, y+dy, color)
63
+ window.draw_line(x, y, color, x, y-dy, color)
64
+ end
65
+
66
+ end
67
+
68
+ end
@@ -0,0 +1,62 @@
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 = "sangaku-eyeball"
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 = ["Jason Hutchens"]
12
+ s.date = "2013-03-15"
13
+ s.description = "Sangaku Eyeball uses Gosu, a 2D Game Library, to display lines, polygons and so on as defined by the Sangaku gem. It allows you to draw arbitrary polygons and then locate the Pole of Inaccessibility (usually the best place to display a label) using a fast, iterative algorithm."
14
+ s.email = "jason.hutchens@agworld.com.au"
15
+ s.executables = ["sangaku-eyeball.rb"]
16
+ s.extra_rdoc_files = [
17
+ "LICENSE.txt",
18
+ "README.md"
19
+ ]
20
+ s.files = [
21
+ ".document",
22
+ ".rvmrc",
23
+ "Gemfile",
24
+ "Gemfile.lock",
25
+ "LICENSE.txt",
26
+ "README.md",
27
+ "Rakefile",
28
+ "VERSION",
29
+ "bin/sangaku-eyeball.rb",
30
+ "lib/sangaku-eyeball.rb",
31
+ "lib/sangaku-eyeball/assets/cursor.png",
32
+ "lib/sangaku-eyeball/assets/target.png",
33
+ "lib/sangaku-eyeball/drawable.rb",
34
+ "sangaku-eyeball.gemspec",
35
+ "test/helper.rb",
36
+ "test/test_sangaku-eyeball.rb"
37
+ ]
38
+ s.homepage = "http://github.com/agworld/sangaku-eyeball"
39
+ s.licenses = ["MIT"]
40
+ s.require_paths = ["lib"]
41
+ s.rubygems_version = "1.8.24"
42
+ s.summary = "Sangaku Eyeball is a simple tool for visualising what the Sangaku gem can do."
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_runtime_dependency(%q<gosu>, [">= 0"])
49
+ s.add_runtime_dependency(%q<sangaku>, [">= 0.5.4"])
50
+ s.add_development_dependency(%q<awesome_print>, [">= 0"])
51
+ else
52
+ s.add_dependency(%q<gosu>, [">= 0"])
53
+ s.add_dependency(%q<sangaku>, [">= 0.5.4"])
54
+ s.add_dependency(%q<awesome_print>, [">= 0"])
55
+ end
56
+ else
57
+ s.add_dependency(%q<gosu>, [">= 0"])
58
+ s.add_dependency(%q<sangaku>, [">= 0.5.4"])
59
+ s.add_dependency(%q<awesome_print>, [">= 0"])
60
+ end
61
+ end
62
+
@@ -0,0 +1,18 @@
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 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'sangaku-eyeball'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestSangakuEyeball < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sangaku-eyeball
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jason Hutchens
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: gosu
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: sangaku
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 0.5.4
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 0.5.4
46
+ - !ruby/object:Gem::Dependency
47
+ name: awesome_print
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Sangaku Eyeball uses Gosu, a 2D Game Library, to display lines, polygons
63
+ and so on as defined by the Sangaku gem. It allows you to draw arbitrary polygons
64
+ and then locate the Pole of Inaccessibility (usually the best place to display a
65
+ label) using a fast, iterative algorithm.
66
+ email: jason.hutchens@agworld.com.au
67
+ executables:
68
+ - sangaku-eyeball.rb
69
+ extensions: []
70
+ extra_rdoc_files:
71
+ - LICENSE.txt
72
+ - README.md
73
+ files:
74
+ - .document
75
+ - .rvmrc
76
+ - Gemfile
77
+ - Gemfile.lock
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - VERSION
82
+ - bin/sangaku-eyeball.rb
83
+ - lib/sangaku-eyeball.rb
84
+ - lib/sangaku-eyeball/assets/cursor.png
85
+ - lib/sangaku-eyeball/assets/target.png
86
+ - lib/sangaku-eyeball/drawable.rb
87
+ - sangaku-eyeball.gemspec
88
+ - test/helper.rb
89
+ - test/test_sangaku-eyeball.rb
90
+ homepage: http://github.com/agworld/sangaku-eyeball
91
+ licenses:
92
+ - MIT
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ segments:
104
+ - 0
105
+ hash: 3731154873292989781
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 1.8.24
115
+ signing_key:
116
+ specification_version: 3
117
+ summary: Sangaku Eyeball is a simple tool for visualising what the Sangaku gem can
118
+ do.
119
+ test_files: []