gui_geometry 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in gui_geometry.gemspec
4
+ gemspec
@@ -0,0 +1,7 @@
1
+ guard 'rspec', :cli => "--color" do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { "spec" }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
+
6
+ end
7
+
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Shane Brinkman-Davis
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,70 @@
1
+ # GuiGeometry
2
+
3
+ There are a few good geometry gems already available for Ruby (ruby-geometry and geometry), but I need one which is focused on the needs of creating 2D graphical user interfaces.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'gui_geometry'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install gui_geometry
18
+
19
+ ## Usage
20
+
21
+ Points are represented by X and Y coordinates:
22
+
23
+ Rectangles consist of two point objects. One specifies the location of the rectangle - it's top-left corner. The other specifies its size in terms of width then height.
24
+
25
+ ``` ruby
26
+ require "gui_geometry"
27
+ include GuiGeometry::Tools
28
+
29
+ # create point x == y == 0
30
+ > point
31
+ => point(0,0)
32
+
33
+ # create a point x=3, y=4
34
+ > my_point = point(3,4)
35
+ => point(3,4)
36
+
37
+ > [my_point.x, my_point.y]
38
+ => [3, 4]
39
+
40
+ # create a rectangle with location (top-left corner) = poiunt(4,5), width=100, height=80
41
+ > rect(4,5,100,80)
42
+ => rect(4,5,100,80)
43
+ > my_rect = rect point(4,5), point(100,80) # alternate form
44
+ => rect(4,5,100,80)
45
+
46
+ # create a rect with location == point(0,0), size = point(30,50) (width = 30, height = 50)
47
+ > rect point(30,50)
48
+ => rect(0,0,30,50)
49
+
50
+ # get loc and size:
51
+ > [my_rect.loc, my_rect.size]
52
+ => [point(4,5), point(100,80)]
53
+
54
+ # get x, y, width and height
55
+ > [my_rect.x, my_rect.y, my_rect.w, my_rect.h]
56
+ => [4, 5, 100, 80]
57
+
58
+ # get the 4 corners:
59
+ > [my_rect.tl, my_rect.tr, my_rect.bl, my_rect.br]
60
+ => [point(4,5), point(104,5), point(4,85), point(104,85)]
61
+
62
+ ```
63
+
64
+ ## Contributing
65
+
66
+ 1. Fork it
67
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
68
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
69
+ 4. Push to the branch (`git push origin my-new-feature`)
70
+ 5. Create new Pull Request
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'gui_geometry/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "gui_geometry"
8
+ gem.version = GuiGeometry::VERSION
9
+ gem.authors = ["Shane Brinkman-Davis"]
10
+ gem.email = ["shanebdavis@gmail.com"]
11
+ gem.description = %q{2D geometry specificly designed to support graphical user interfaces and bitmaps currently focsued on Point and Rectangle classes}
12
+ gem.summary = %q{2D geometry specificly designed to support graphical user interfaces and bitmaps}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_development_dependency 'rake'
21
+ gem.add_development_dependency 'rspec'
22
+ gem.add_development_dependency 'guard-rspec'
23
+ gem.add_development_dependency 'guard-test'
24
+ gem.add_development_dependency 'rb-fsevent'
25
+ gem.add_development_dependency 'simplecov'
26
+ end
@@ -0,0 +1,13 @@
1
+
2
+ %w{
3
+ tools
4
+ point
5
+ rectangle
6
+ version
7
+ }.each do |file|
8
+ require File.join(File.dirname(__FILE__),"gui_geometry",file)
9
+ end
10
+
11
+ module GuiGeometry
12
+ # Your code goes here...
13
+ end
@@ -0,0 +1,26 @@
1
+ module GuiGeometry
2
+ class Point < Struct.new(:x, :y)
3
+ include Tools
4
+
5
+ def initialize(*args)
6
+ self.x = self.y = 0
7
+ super if args.length!=0
8
+ end
9
+
10
+ def min(b); point(super(x, b.x), super(y, b.y)); end
11
+ def max(b); point(super(x, b.x), super(y, b.y)); end
12
+
13
+ def inspect; "point(#{x},#{y})" end
14
+ def to_s; "(#{x},#{y})" end
15
+
16
+ def >=(b) x>=b.x && y>=b.y end
17
+ def <=(b) x<=b.x && y<=b.y end
18
+ def >(b) x>b.x && y>b.y end
19
+ def <(b) x<b.x && y<b.y end
20
+
21
+ def +(b) b.kind_of?(Point) ? point(x+b.x, y+b.y) : point(x+b, y+b) end
22
+ def -(b) b.kind_of?(Point) ? point(x-b.x, y-b.y) : point(x-b, y-b) end
23
+ def *(b) b.kind_of?(Point) ? point(x*b.x, y*b.y) : point(x*b, y*b) end
24
+ def /(b) b.kind_of?(Point) ? point(x/b.x, y/b.y) : point(x/b, y/b) end
25
+ end
26
+ end
@@ -0,0 +1,74 @@
1
+ module GuiGeometry
2
+ class Rectangle < Struct.new(:loc, :size)
3
+ include Tools
4
+
5
+ def initialize(*args)
6
+ case args.length
7
+ when 0 then super point, point
8
+ when 1 then super point, args[0].clone
9
+ when 2 then super args[0].clone, args[1].clone
10
+ when 4 then super point(*args[0..1]), point(*args[2..3])
11
+ else raise ArgumentError.new
12
+ end
13
+ end
14
+
15
+ def +(b) b.kind_of?(Point) ? rect(loc+b, size) : rect(loc+b.loc, size+b.size) end
16
+ def -(b) b.kind_of?(Point) ? rect(loc-b, size) : rect(loc-b.loc, size-b.size) end
17
+
18
+ def present?
19
+ w > 0 && h > 0
20
+ end
21
+
22
+ def blank?
23
+ !present?
24
+ end
25
+
26
+ def x; loc.x; end
27
+ def y; loc.y; end
28
+ def w; size.x; end
29
+ def h; size.y; end
30
+
31
+ def x_range; x .. (x + w - 1) end
32
+ def y_range; y .. (y + h - 1) end
33
+
34
+ def inspect
35
+ "rect"+to_s
36
+ end
37
+
38
+ def to_s
39
+ "(#{[loc.x,loc.y,size.x,size.y].join ','})"
40
+ end
41
+
42
+ def overlaps?(b)
43
+ b.loc + b.size > loc &&
44
+ loc + size > b.loc
45
+ end
46
+
47
+ def contains?(b)
48
+ (self & b) == self
49
+ end
50
+
51
+ def tl; loc; end
52
+ def br; loc + size; end
53
+ def bl; point(x, y + h); end
54
+ def tr; point(x + w, y); end
55
+
56
+
57
+ def union(b)
58
+ return clone unless b
59
+ l = loc.min(b.loc)
60
+ s = br.max(b.br) - l
61
+ rect l, s
62
+ end
63
+ alias :& :union
64
+
65
+ def intersection(b)
66
+ return rect unless b
67
+ l = loc.max(b.loc)
68
+ s = br.min(b.br) - l
69
+ return rect unless s>point
70
+ rect l, s
71
+ end
72
+ alias :| :intersection
73
+ end
74
+ end
@@ -0,0 +1,12 @@
1
+ module GuiGeometry
2
+ module Tools
3
+ def min(a,b) a < b ? a : b; end
4
+ def max(a,b) a > b ? a : b; end
5
+ def bound(a,bounded,c) bounded < a ? a : (bounded > c) ? c : bounded; end
6
+
7
+ def point(*args); Point.new *args end
8
+ def rect(*args); Rectangle.new *args end
9
+ def buffer(*args); Buffer.new *args end
10
+ def window(*args); Window.new *args end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module GuiGeometry
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,83 @@
1
+ require 'spec_helper'
2
+
3
+ module GuiGeometry
4
+ describe "Point" do
5
+ include Tools
6
+
7
+ it "init" do
8
+ point.should == point(0,0)
9
+ end
10
+
11
+ it "+" do
12
+ (point(1,2) + point(3,4)).should == point(4,6)
13
+ (point(1,2) + 3).should == point(4,5)
14
+ end
15
+
16
+ it "-" do
17
+ (point(9,8) - point(3,4)).should == point(6,4)
18
+ (point(9,8) - 3).should == point(6,5)
19
+ end
20
+
21
+ it "*" do
22
+ (point(9,8) * point(3,4)).should == point(27,32)
23
+ (point(9,8) * 3).should == point(27,24)
24
+ end
25
+
26
+ it "/" do
27
+ (point(36,24) / point(4,3)).should == point(9,8)
28
+ (point(36,24) / 2).should == point(18,12)
29
+ end
30
+
31
+ it "==" do
32
+ (point(3,4) == point(3,4)).should == true
33
+ (point(3,4) == point(2,4)).should == false
34
+ (point(3,4) == point(3,3)).should == false
35
+ end
36
+
37
+ it "<=" do
38
+ (point(3,4) <= point(3,4)).should == true
39
+ (point(3,4) <= point(2,4)).should == false
40
+ (point(3,4) <= point(3,3)).should == false
41
+ (point(4,4) <= point(3,4)).should == false
42
+ (point(3,5) <= point(3,4)).should == false
43
+ end
44
+
45
+ it "<" do
46
+ (point(3,4) < point(4,5)).should == true
47
+ (point(3,4) < point(3,5)).should == false
48
+ (point(3,4) < point(4,4)).should == false
49
+ (point(4,4) < point(4,5)).should == false
50
+ (point(3,5) < point(4,5)).should == false
51
+ end
52
+
53
+ it ">=" do
54
+ (point(3,4) >= point(3,4)).should == true
55
+ (point(2,4) >= point(3,4)).should == false
56
+ (point(3,3) >= point(3,4)).should == false
57
+ (point(3,4) >= point(4,4)).should == false
58
+ (point(3,4) >= point(3,5)).should == false
59
+ end
60
+
61
+ it ">" do
62
+ (point(4,5) > point(3,4)).should == true
63
+ (point(3,5) > point(3,4)).should == false
64
+ (point(4,4) > point(3,4)).should == false
65
+ (point(4,5) > point(4,4)).should == false
66
+ (point(4,5) > point(3,5)).should == false
67
+ end
68
+
69
+ it "min" do
70
+ point(5,5).min(point(9,9)).should == point(5,5)
71
+ point(9,9).min(point(5,5)).should == point(5,5)
72
+ point(9,5).min(point(5,9)).should == point(5,5)
73
+ point(5,9).min(point(9,5)).should == point(5,5)
74
+ end
75
+
76
+ it "max" do
77
+ point(5,5).max(point(9,9)).should == point(9,9)
78
+ point(9,9).max(point(5,5)).should == point(9,9)
79
+ point(9,5).max(point(5,9)).should == point(9,9)
80
+ point(5,9).max(point(9,5)).should == point(9,9)
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,81 @@
1
+ require 'spec_helper'
2
+
3
+ module GuiGeometry
4
+ describe "Rectangle" do
5
+ include Tools
6
+
7
+ it "defaults" do
8
+ rect.should == rect(0,0,0,0)
9
+ rect.should == rect(point,point)
10
+ end
11
+
12
+ it "invalid init" do
13
+ expect { rect(1,2,3,4,5) }.to raise_error
14
+ end
15
+
16
+ it "init with one point" do
17
+ rect(point(1,2)).should == rect(0,0,1,2)
18
+ end
19
+
20
+ it "inspect" do
21
+ rect.inspect.class.should == String
22
+ end
23
+
24
+ it "contains?" do
25
+ rect(0,0,10,10).contains?(rect(2,2,5,5)).should == true
26
+ rect(0,0,10,10).contains?(rect(8,2,5,5)).should == false
27
+ end
28
+
29
+ it ".overlap?" do
30
+ # solidly overlapping
31
+ rect(0,0,10,10).overlaps?(rect(0,0,10,10)).should == true
32
+ rect(0,0,10,10).overlaps?(rect(0,5,10,10)).should == true
33
+ rect(0,0,10,10).overlaps?(rect(5,0,10,10)).should == true
34
+
35
+ # just below, just to the right, just above, just to the left
36
+ rect(0,0,10,10).overlaps?(rect(10,0,10,10)).should == false
37
+ rect(0,0,10,10).overlaps?(rect(0,10,10,10)).should == false
38
+ rect(10,0,10,10).overlaps?(rect(0,0,10,10)).should == false
39
+ rect(0,10,10,10).overlaps?(rect(0,0,10,10)).should == false
40
+
41
+ # below, right, above, left
42
+ rect(0,0,10,10).overlaps?(rect(11,0,10,10)).should == false
43
+ rect(0,0,10,10).overlaps?(rect(0,11,10,10)).should == false
44
+ rect(11,0,10,10).overlaps?(rect(0,0,10,10)).should == false
45
+ rect(0,11,10,10).overlaps?(rect(0,0,10,10)).should == false
46
+
47
+ # just overlapping below, right, above, left
48
+ rect(0,0,10,10).overlaps?(rect(9,0,10,10)).should == true
49
+ rect(0,0,10,10).overlaps?(rect(0,9,10,10)).should == true
50
+ rect(9,0,10,10).overlaps?(rect(0,0,10,10)).should == true
51
+ rect(0,9,10,10).overlaps?(rect(0,0,10,10)).should == true
52
+ end
53
+
54
+ it ".union" do
55
+ (rect(0,0,10,10) & rect(0,5,10,10)).should == rect(0,0,10,15)
56
+ (rect(0,0,10,10) & rect(5,0,10,10)).should == rect(0,0,15,10)
57
+ (rect(20,20,10,10) & rect(0,0,10,10)).should == rect(0,0,30,30)
58
+ end
59
+
60
+ it ".intersection" do
61
+ (rect(0,0,10,10) | rect(0,0,10,10)).should == rect(0,0,10,10)
62
+ (rect(0,0,10,10) | rect(0,5,10,10)).should == rect(0,5,10,5)
63
+ (rect(0,0,10,10) | rect(5,0,10,10)).should == rect(5,0,5,10)
64
+ (rect(0,0,10,10) | rect(10,0,10,10)).should == rect(0,0,0,0)
65
+ (rect(0,0,10,10) | rect(-5,-5,10,10)).should == rect(0,0,5,5)
66
+ end
67
+
68
+ it ".present?" do
69
+ rect(0,0,1,1).present?.should == true
70
+ rect(0,0,1,0).present?.should == false
71
+ rect(0,0,0,1).present?.should == false
72
+ rect(0,0,0,0).present?.should == false
73
+ end
74
+ it ".blank?" do
75
+ rect(0,0,1,1).blank?.should == false
76
+ rect(0,0,1,0).blank?.should == true
77
+ rect(0,0,0,1).blank?.should == true
78
+ rect(0,0,0,0).blank?.should == true
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,6 @@
1
+ require 'simplecov'
2
+ SimpleCov.start do
3
+ add_filter "vendor/ruby"
4
+ end
5
+
6
+ require File.join(File.dirname(__FILE__),"..","lib","gui_geometry")
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ module GuiGeometry
4
+ describe "Tools" do
5
+ include Tools
6
+
7
+ it "min" do
8
+ min(4,5).should == 4
9
+ end
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,163 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gui_geometry
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Shane Brinkman-Davis
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
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: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: guard-rspec
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
+ - !ruby/object:Gem::Dependency
63
+ name: guard-test
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rb-fsevent
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: simplecov
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: 2D geometry specificly designed to support graphical user interfaces
111
+ and bitmaps currently focsued on Point and Rectangle classes
112
+ email:
113
+ - shanebdavis@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - .gitignore
119
+ - Gemfile
120
+ - Guardfile
121
+ - LICENSE.txt
122
+ - README.md
123
+ - Rakefile
124
+ - gui_geometry.gemspec
125
+ - lib/gui_geometry.rb
126
+ - lib/gui_geometry/point.rb
127
+ - lib/gui_geometry/rectangle.rb
128
+ - lib/gui_geometry/tools.rb
129
+ - lib/gui_geometry/version.rb
130
+ - spec/point_spec.rb
131
+ - spec/rectangle_spec.rb
132
+ - spec/spec_helper.rb
133
+ - spec/tools_spec.rb
134
+ homepage: ''
135
+ licenses: []
136
+ post_install_message:
137
+ rdoc_options: []
138
+ require_paths:
139
+ - lib
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ none: false
142
+ requirements:
143
+ - - ! '>='
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ none: false
148
+ requirements:
149
+ - - ! '>='
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ requirements: []
153
+ rubyforge_project:
154
+ rubygems_version: 1.8.24
155
+ signing_key:
156
+ specification_version: 3
157
+ summary: 2D geometry specificly designed to support graphical user interfaces and
158
+ bitmaps
159
+ test_files:
160
+ - spec/point_spec.rb
161
+ - spec/rectangle_spec.rb
162
+ - spec/spec_helper.rb
163
+ - spec/tools_spec.rb