geomotion 0.0.2 → 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +117 -12
- data/lib/geomotion/cg_point.rb +36 -0
- data/lib/geomotion/cg_rect.rb +112 -1
- data/lib/geomotion/cg_size.rb +41 -0
- data/lib/geomotion/version.rb +1 -1
- data/spec/cg_point_spec.rb +65 -0
- data/spec/cg_rect_spec.rb +381 -0
- data/spec/cg_size_spec.rb +60 -0
- metadata +10 -4
- data/spec/main_spec.rb +0 -36
data/README.md
CHANGED
@@ -1,46 +1,151 @@
|
|
1
1
|
# Geomotion
|
2
2
|
|
3
|
-
iOS
|
3
|
+
iOS Geometry in idiomatic Ruby. Exhaustively tested. What's not to love?
|
4
4
|
|
5
|
-
##
|
5
|
+
## Features
|
6
|
+
|
7
|
+
### CGRect
|
6
8
|
|
7
9
|
```ruby
|
8
|
-
#
|
10
|
+
# Initializers
|
9
11
|
rect = CGRect.make(x: 10, y: 100, width: 50, height: 20)
|
12
|
+
another_way = CGRect.make(origin: CGPoint, size: CGSize)
|
10
13
|
|
11
|
-
#
|
14
|
+
# Getters
|
12
15
|
[rect.x, rect.y, rect.width, rect.height]
|
13
16
|
=> [10, 100, 50, 20]
|
14
17
|
|
15
|
-
|
18
|
+
rect_zero = CGRect.empty
|
19
|
+
=> CGRect(0, 0, 0, 0)
|
20
|
+
rect_zero.empty?
|
21
|
+
=> true
|
22
|
+
|
23
|
+
rect.center
|
24
|
+
=> CGPoint(25, 10) # center as bounds
|
25
|
+
rect.center(true)
|
26
|
+
=> CGPoint(35, 110) # center as frame
|
27
|
+
|
28
|
+
# Operator Overloading
|
29
|
+
rect + CGRect.make(x: 9, y: 99, width: 10, height: 10)
|
30
|
+
=> CGRect(9, 99, 50, 20) # == union of rects
|
31
|
+
|
32
|
+
rect + CGSize.new(11, 1)
|
33
|
+
=> CGRect(10, 100, 61, 21) # == increases the size, but keeps the origin
|
34
|
+
|
35
|
+
rect + CGPoint.new(10, 10)
|
36
|
+
=> CGRect.new(20, 110, 50, 20)
|
37
|
+
|
38
|
+
rect + UIOffsetMake(10, 10)
|
39
|
+
=> CGRect.new(20, 110, 50, 20)
|
40
|
+
|
41
|
+
a_point + a_size
|
42
|
+
=> CGRect # a point and a size make a rectangle. makes sense, right?
|
43
|
+
|
44
|
+
# Union and Intersection
|
45
|
+
rect.union_with CGRect.new(9, 99, 10, 10)
|
46
|
+
=> CGRect(9, 99, 50, 20)
|
47
|
+
|
48
|
+
rect.intersection_with CGRect.new(9, 99, 10, 10)
|
49
|
+
=> CGRect.new(10, 100, 10, 10)
|
50
|
+
|
51
|
+
# Growing and shrinking
|
52
|
+
# The center stays the same. Think margins!
|
53
|
+
rect.grow(CGSize.new(10, 20))
|
54
|
+
=> CGRect.new(5, 90, 60, 40)
|
55
|
+
|
56
|
+
rect.shrink(10)
|
57
|
+
=> CGRect.new(15, 105, 40, 10)
|
58
|
+
|
59
|
+
# Powerful layout adjustments with chainable methods
|
16
60
|
view = UIView.alloc.initWithFrame rect.below.width(100).height(10)
|
17
61
|
view.frame
|
18
|
-
=>
|
62
|
+
=> CGRect(10, 120, 100, 10)
|
19
63
|
|
20
64
|
view2 = UIView.alloc.initWithFrame rect.beside(10)
|
21
65
|
view2.frame
|
22
|
-
=>
|
66
|
+
=> CGRect(70, 100, 50, 20.0)
|
23
67
|
|
68
|
+
# More examples of adjustments
|
69
|
+
rect = CGRect.make(x: 10, y: 100, width: 50, height: 20)
|
24
70
|
[rect.right(20).x, rect.left(20).x, rect.up(20).y, rect.down(20).y]
|
25
71
|
=> [30, -10, 80, 120]
|
26
72
|
|
73
|
+
# Layout "above" and "before" rectangles
|
74
|
+
# (default offset is the rectangle's width or height)
|
75
|
+
rect.before(5)
|
76
|
+
=> CGRect.new(-45, 100, 50, 20)
|
77
|
+
rect.before(5, width:20)
|
78
|
+
=> CGRect.new(-15, 100, 20, 20)
|
79
|
+
|
80
|
+
rect.above(5)
|
81
|
+
=> CGRect.new(10, 75, 50, 20)
|
82
|
+
rect.above(5, height:10)
|
83
|
+
=> CGRect.new(10, 85, 50, 10)
|
84
|
+
|
27
85
|
# Layout a rect relative to others
|
28
|
-
rect2 = CGRect.
|
29
|
-
rect3 = CGRect.
|
86
|
+
rect2 = CGRect.make(x: 50, y: 50, width: 100, height: 100)
|
87
|
+
rect3 = CGRect.make(x:100, y: 200, width: 20, height: 20)
|
30
88
|
|
31
89
|
CGRect.layout(rect, above: rect2, right_of: rect3)
|
32
|
-
=>
|
90
|
+
=> CGRect(120, 30, 50, 20)
|
33
91
|
|
34
92
|
# Also supports margins
|
35
93
|
CGRect.layout(rect, above: rect2, right_of: rect3, margins: [0, 0, 10, 15])
|
36
|
-
=>
|
94
|
+
=> CGRect(135, 20, 50, 20)
|
95
|
+
|
96
|
+
```
|
97
|
+
|
98
|
+
### CGSize
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
# Initializers
|
102
|
+
size = CGSize.make(width: 50, height: 20)
|
103
|
+
|
104
|
+
# Getters
|
105
|
+
size_zero = CGSize.empty
|
106
|
+
=> CGSize(0, 0)
|
107
|
+
size_zero.empty?
|
108
|
+
=> true
|
109
|
+
|
110
|
+
# Operator Overloading
|
111
|
+
size + CGSize.make(width: 100, height: 50)
|
112
|
+
=> CGSize(150, 70)
|
113
|
+
|
114
|
+
size + CGPoint.make(x: 10, y: 30)
|
115
|
+
=> CGRect(10, 30, 50, 20)
|
116
|
+
|
117
|
+
# Combine with CGPoint
|
118
|
+
size.rect_at_point CGPoint.make(x: 10, y: 30)
|
119
|
+
=> CGRect(10, 30, 50, 20)
|
120
|
+
```
|
121
|
+
|
122
|
+
### CGPoint
|
123
|
+
|
124
|
+
```ruby
|
125
|
+
# Initializers
|
126
|
+
point = CGPoint.make(x: 10, y: 100)
|
127
|
+
|
128
|
+
# Operator Overloading
|
129
|
+
point + CGPoint.make(x: 20, y: 40)
|
130
|
+
=> CGPoint(30, 140)
|
131
|
+
|
132
|
+
point + CGSize.make(width: 50, height: 20)
|
133
|
+
=> CGRect(10, 100, 50, 20)
|
134
|
+
|
135
|
+
# Combine with CGSize
|
136
|
+
point.rect_of_size CGSize.make(width: 50, height: 20)
|
137
|
+
=> CGRect(10, 100, 50, 20)
|
138
|
+
|
139
|
+
# Combine with CGRect
|
140
|
+
point.inside? CGRect.make(x: 0, y: 0, width: 20, height: 110)
|
141
|
+
=> true
|
37
142
|
```
|
38
143
|
|
39
144
|
## Install
|
40
145
|
|
41
146
|
1. `gem install geomotion`
|
42
147
|
|
43
|
-
2. Add `require geomotion` in your `Rakefile`.
|
148
|
+
2. Add `require 'geomotion'` in your `Rakefile`.
|
44
149
|
|
45
150
|
|
46
151
|
## Forking
|
@@ -0,0 +1,36 @@
|
|
1
|
+
class CGPoint
|
2
|
+
# CGPoint.make(x: 10, y: 30)
|
3
|
+
def self.make(options = {})
|
4
|
+
CGPoint.new(options[:x] || 0, options[:y] || 0)
|
5
|
+
end
|
6
|
+
|
7
|
+
# size = CGSize.make width: 100, height: 100
|
8
|
+
# point = CPPoint.make x:0, y:10
|
9
|
+
# point.rect_of_size(point) # => CGRect([0, 10], [100, 100])
|
10
|
+
# point.rect_of_size([10, 20]) # => CGRect([10, 20], [100, 100])
|
11
|
+
def rect_of_size(size)
|
12
|
+
CGRect.new([self.x, self.y], size)
|
13
|
+
end
|
14
|
+
|
15
|
+
def +(other)
|
16
|
+
case other
|
17
|
+
when CGSize
|
18
|
+
return self.rect_of_size(other)
|
19
|
+
when CGPoint
|
20
|
+
return CGPoint.new(self.x + other.x, self.y + other.y)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def round
|
25
|
+
CGPoint.new(self.x.round, self.y.round)
|
26
|
+
end
|
27
|
+
|
28
|
+
def inside?(rect)
|
29
|
+
CGRectContainsPoint(rect, self)
|
30
|
+
end
|
31
|
+
|
32
|
+
def ==(point)
|
33
|
+
point.is_a?(CGPoint) && CGPointEqualToPoint(self, point)
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
data/lib/geomotion/cg_rect.rb
CHANGED
@@ -1,9 +1,31 @@
|
|
1
1
|
class CGRect
|
2
2
|
# CGRect.make(x: 10, y: 30)
|
3
|
+
# CGRect.make(x: 10, y: 30, width:100, height: 20)
|
3
4
|
def self.make(options = {})
|
5
|
+
if options[:origin]
|
6
|
+
options[:x] = options[:origin].x
|
7
|
+
options[:y] = options[:origin].y
|
8
|
+
end
|
9
|
+
if options[:size]
|
10
|
+
options[:width] = options[:size].width
|
11
|
+
options[:height] = options[:size].height
|
12
|
+
end
|
4
13
|
CGRect.new([options[:x] || 0, options[:y] || 0], [options[:width] || 0, options[:height] || 0])
|
5
14
|
end
|
6
15
|
|
16
|
+
def self.empty
|
17
|
+
# Don't return CGRectZero; can be mutated
|
18
|
+
CGRect.make
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.null
|
22
|
+
CGRectNull
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.infinite
|
26
|
+
self.new([0, 0], CGSize.infinite)
|
27
|
+
end
|
28
|
+
|
7
29
|
# OPTIONS: [:above, :below, :left_of, :right_of, :margins]
|
8
30
|
# :margins is array of [top, right, bottom, left]
|
9
31
|
# EX CGRect.layout(rect1, above: rect2, left_of: rect3, margins: [0, 10, 20, 0])
|
@@ -91,10 +113,26 @@ class CGRect
|
|
91
113
|
CGRect.new([self.x, self.y + dist], self.size)
|
92
114
|
end
|
93
115
|
|
116
|
+
def above(margin = 0)
|
117
|
+
self.above(margin, height:self.height)
|
118
|
+
end
|
119
|
+
|
120
|
+
def above(margin, height:height)
|
121
|
+
CGRect.new([self.x, self.y - height - margin], [self.width, height])
|
122
|
+
end
|
123
|
+
|
94
124
|
def below(margin = 0)
|
95
125
|
CGRect.new([self.x, self.y + self.height + margin], self.size)
|
96
126
|
end
|
97
127
|
|
128
|
+
def before(margin = 0)
|
129
|
+
self.before(margin, width:self.width)
|
130
|
+
end
|
131
|
+
|
132
|
+
def before(margin, width:width)
|
133
|
+
CGRect.new([self.x - width - margin, self.y], [width, self.height])
|
134
|
+
end
|
135
|
+
|
98
136
|
def beside(margin = 0)
|
99
137
|
CGRect.new([self.x + self.width + margin, self.y], self.size)
|
100
138
|
end
|
@@ -115,4 +153,77 @@ class CGRect
|
|
115
153
|
CGRect.new([offset_x + ((rect.width - self.width) / 2),
|
116
154
|
offset_y + ((rect.height - self.height) / 2)], self.size)
|
117
155
|
end
|
118
|
-
|
156
|
+
|
157
|
+
def +(other)
|
158
|
+
case other
|
159
|
+
when CGRect
|
160
|
+
return self.union_with(other)
|
161
|
+
when CGSize
|
162
|
+
return CGRect.new([self.x, self.y], [self.width + other.width, self.height + other.height])
|
163
|
+
when CGPoint
|
164
|
+
return CGRectOffset(self, other.x, other.y)
|
165
|
+
when UIOffset
|
166
|
+
CGRectOffset(self, other.horizontal, other.vertical)
|
167
|
+
when UIEdgeInsets
|
168
|
+
UIEdgeInsetsInsetRect(self, other)
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
def intersection_with(rect)
|
173
|
+
CGRectIntersection(self, rect)
|
174
|
+
end
|
175
|
+
|
176
|
+
def union_with(rect)
|
177
|
+
CGRectUnion(self, rect)
|
178
|
+
end
|
179
|
+
|
180
|
+
def grow(size)
|
181
|
+
if size.is_a? Numeric
|
182
|
+
size = CGSize.new(size, size)
|
183
|
+
end
|
184
|
+
CGRectInset(self, -size.width, -size.height)
|
185
|
+
end
|
186
|
+
|
187
|
+
def shrink(size)
|
188
|
+
if size.is_a? Numeric
|
189
|
+
size = CGSize.new(size, size)
|
190
|
+
end
|
191
|
+
CGRectInset(self, size.width, size.height)
|
192
|
+
end
|
193
|
+
|
194
|
+
def empty?
|
195
|
+
CGRectIsEmpty(self)
|
196
|
+
end
|
197
|
+
|
198
|
+
def infinite?
|
199
|
+
self.size.infinite?
|
200
|
+
end
|
201
|
+
|
202
|
+
def null?
|
203
|
+
CGRectIsNull(self)
|
204
|
+
end
|
205
|
+
|
206
|
+
def intersects?(rect)
|
207
|
+
case rect
|
208
|
+
when CGRect
|
209
|
+
CGRectIntersectsRect(self, rect)
|
210
|
+
else
|
211
|
+
super
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
def contains?(rect_or_point)
|
216
|
+
case rect_or_point
|
217
|
+
when CGPoint
|
218
|
+
CGRectContainsPoint(self, rect_or_point)
|
219
|
+
when CGRect
|
220
|
+
CGRectContainsRect(self, rect_or_point)
|
221
|
+
else
|
222
|
+
super
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
def ==(rect)
|
227
|
+
rect.is_a?(CGRect) && CGRectEqualToRect(self, rect)
|
228
|
+
end
|
229
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
class CGSize
|
2
|
+
# CGSize.make(width: 10, height: 30)
|
3
|
+
def self.make(options = {})
|
4
|
+
CGSize.new(options[:width] || 0, options[:height] || 0)
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.infinite
|
8
|
+
infinity = CGRect.null[0][0]
|
9
|
+
CGSizeMake(infinity, infinity)
|
10
|
+
end
|
11
|
+
|
12
|
+
# size = CGSize.make width: 100, height: 100
|
13
|
+
# point = CPPoint.make x:0, y:10
|
14
|
+
# size.rect_at_point(point) # => CGRect([0, 10], [100, 100])
|
15
|
+
# size.rect_at_point([10, 20]) # => CGRect([10, 20], [100, 100])
|
16
|
+
def rect_at_point(point)
|
17
|
+
CGRect.new(point, [self.width, self.height])
|
18
|
+
end
|
19
|
+
|
20
|
+
def +(other)
|
21
|
+
case other
|
22
|
+
when CGSize
|
23
|
+
return CGSize.new(self.width + other.width, self.height + other.height)
|
24
|
+
when CGPoint
|
25
|
+
return self.rect_at_point(other)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def infinite?
|
30
|
+
infinity = CGRect.null[0][0] # null rects are rects with infinite width & height
|
31
|
+
self.width == infinity or self.height == infinity
|
32
|
+
end
|
33
|
+
|
34
|
+
def empty?
|
35
|
+
self == CGSizeZero
|
36
|
+
end
|
37
|
+
|
38
|
+
def ==(size)
|
39
|
+
size.is_a?(CGSize) && CGSizeEqualToSize(self, size)
|
40
|
+
end
|
41
|
+
end
|
data/lib/geomotion/version.rb
CHANGED
@@ -0,0 +1,65 @@
|
|
1
|
+
describe "CGPoint" do
|
2
|
+
before do
|
3
|
+
@point = CGPoint.make(x: 10, y: 20)
|
4
|
+
end
|
5
|
+
|
6
|
+
describe ".make" do
|
7
|
+
it "should work with options" do
|
8
|
+
CGPointEqualToPoint(@point, CGPointMake(10, 20)).should == true
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should work with no options" do
|
12
|
+
CGPointEqualToPoint(CGPoint.make, CGPointMake(0, 0)).should == true
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "#rect_of_size" do
|
17
|
+
it "should work" do
|
18
|
+
size = CGSizeMake(20, 30)
|
19
|
+
@point.rect_of_size(size).should == CGRectMake(10, 20, 20, 30)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "#+" do
|
24
|
+
it "should work with CGSize" do
|
25
|
+
size = CGSizeMake(20, 30)
|
26
|
+
(@point + size).should == CGRectMake(10, 20, 20, 30)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should work with CGPoint" do
|
30
|
+
point = CGPoint.make(x: 100, y: 200)
|
31
|
+
(@point + point).should == CGPointMake(110, 220)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe "#inside?" do
|
36
|
+
it "should return true" do
|
37
|
+
rect = CGRectMake(0, 0, 100, 100)
|
38
|
+
@point.inside?(rect).should == true
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should return false" do
|
42
|
+
rect = CGRectMake(0, 0, 5, 5)
|
43
|
+
@point.inside?(rect).should == false
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe "#round" do
|
48
|
+
it "should work" do
|
49
|
+
point = CGPointMake(10.4, 11.5).round
|
50
|
+
CGPointEqualToPoint(point, CGPointMake(10, 12)).should == true
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe "#==?" do
|
55
|
+
it "should return true" do
|
56
|
+
point = CGPointMake(10, 20)
|
57
|
+
@point.should == point
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should return false" do
|
61
|
+
point = CGPointMake(20, 10)
|
62
|
+
@point.should != point
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,381 @@
|
|
1
|
+
describe "CGRect" do
|
2
|
+
before do
|
3
|
+
@rect = CGRect.make(x: 10, y: 100, width: 50, height: 20)
|
4
|
+
end
|
5
|
+
|
6
|
+
describe ".make" do
|
7
|
+
it "should work with options" do
|
8
|
+
CGRectEqualToRect(@rect, CGRectMake(10, 100, 50, 20)).should == true
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should work with nested options" do
|
12
|
+
CGRectEqualToRect(
|
13
|
+
CGRect.make(origin: CGPointMake(10, 100), size: CGSizeMake(50,20)),
|
14
|
+
CGRectMake(10, 100, 50, 20)
|
15
|
+
).should == true
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should work with no options" do
|
19
|
+
CGRectEqualToRect(CGRect.make, CGRectMake(0, 0, 0, 0)).should == true
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe ".empty" do
|
24
|
+
it "should work" do
|
25
|
+
CGRectIsEmpty(CGRect.empty).should == true
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "#empty?" do
|
30
|
+
it "should work" do
|
31
|
+
p "ZERO1 #{CGRectZero.inspect}"
|
32
|
+
CGRectZero.empty?.should == true
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe ".null" do
|
37
|
+
it "should work" do
|
38
|
+
CGRectIsNull(CGRect.null).should == true
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "#null?" do
|
43
|
+
it "should work" do
|
44
|
+
CGRectNull.null?.should == true
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
# Currently does NOT work due to some strange RM bug?
|
49
|
+
=begin
|
50
|
+
describe ".infinite" do
|
51
|
+
it "should work" do
|
52
|
+
CGRectIsInfinite(CGRect.infinite).should == true
|
53
|
+
end
|
54
|
+
end
|
55
|
+
=end
|
56
|
+
|
57
|
+
describe "#infinite?" do
|
58
|
+
it "should work" do
|
59
|
+
CGRect.infinite.infinite?.should == true
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe "#intersects?" do
|
64
|
+
it "should work" do
|
65
|
+
left_rect = CGRectMake(0, 0, 100, 100)
|
66
|
+
right_rect = CGRectMake(80, 0, 100, 100)
|
67
|
+
left_rect.intersects?(right_rect).should == true
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe "#contains?" do
|
72
|
+
it "should work with CGPoint" do
|
73
|
+
left_rect = CGRectMake(0, 0, 100, 100)
|
74
|
+
point = CGPointMake(10, 10)
|
75
|
+
left_rect.contains?(point).should == true
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should work with CGRect" do
|
79
|
+
left_rect = CGRectMake(0, 0, 100, 100)
|
80
|
+
inner_rect = CGRectMake(80, 0, 10, 10)
|
81
|
+
left_rect.contains?(inner_rect).should == true
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe "#==" do
|
86
|
+
it "should return true" do
|
87
|
+
@rect.should == CGRectMake(10, 100, 50, 20)
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should return false" do
|
91
|
+
@rect.should != CGRectMake(10, 100, 50, 10)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
describe ".layout" do
|
96
|
+
it "should work without margins" do
|
97
|
+
above = CGRectMake(50, 50, 100, 100)
|
98
|
+
right_of = CGRectMake(100, 200, 20, 20)
|
99
|
+
|
100
|
+
no_margins = CGRect.layout(@rect, above: above, right_of: right_of)
|
101
|
+
|
102
|
+
CGRectEqualToRect(no_margins, CGRectMake(120, 30, 50, 20)).should == true
|
103
|
+
end
|
104
|
+
|
105
|
+
it "should work with margins" do
|
106
|
+
above = CGRectMake(50, 50, 100, 100)
|
107
|
+
right_of = CGRectMake(100, 200, 20, 20)
|
108
|
+
|
109
|
+
margins = CGRect.layout(@rect, above: above, right_of: right_of, margins: [0, 0, 10, 15])
|
110
|
+
|
111
|
+
CGRectEqualToRect(margins, CGRectMake(135, 20, 50, 20)).should == true
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
describe "#x" do
|
116
|
+
it "returns value when no args" do
|
117
|
+
@rect.x.should == 10
|
118
|
+
end
|
119
|
+
|
120
|
+
it "returns copy when has args" do
|
121
|
+
rect = @rect.x(20)
|
122
|
+
rect.is_a?(CGRect).should == true
|
123
|
+
CGRectEqualToRect(rect, CGRectMake(20, 100, 50, 20)).should == true
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
describe "#x=" do
|
128
|
+
it "sets value" do
|
129
|
+
rect = CGRect.empty
|
130
|
+
rect.x = 20
|
131
|
+
rect.origin.x.should == 20
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
describe "#y" do
|
136
|
+
it "returns value when no args" do
|
137
|
+
@rect.y.should == 100
|
138
|
+
end
|
139
|
+
|
140
|
+
it "returns copy when has args" do
|
141
|
+
rect = @rect.y(20)
|
142
|
+
rect.is_a?(CGRect).should == true
|
143
|
+
CGRectEqualToRect(rect, CGRectMake(10, 20, 50, 20)).should == true
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
describe "#y=" do
|
148
|
+
it "sets value" do
|
149
|
+
rect = CGRect.empty
|
150
|
+
rect.y = 20
|
151
|
+
rect.origin.y.should == 20
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
describe "#width" do
|
156
|
+
it "returns value when no args" do
|
157
|
+
@rect.width.should == 50
|
158
|
+
end
|
159
|
+
|
160
|
+
it "returns copy when has args" do
|
161
|
+
rect = @rect.width(20)
|
162
|
+
rect.is_a?(CGRect).should == true
|
163
|
+
CGRectEqualToRect(rect, CGRectMake(10, 100, 20, 20)).should == true
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
describe "#width=" do
|
168
|
+
it "sets value" do
|
169
|
+
rect = CGRect.empty
|
170
|
+
rect.width = 20
|
171
|
+
rect.size.width.should == 20
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
describe "#height" do
|
176
|
+
it "returns value when no args" do
|
177
|
+
@rect.height.should == 20
|
178
|
+
end
|
179
|
+
|
180
|
+
it "returns copy when has args" do
|
181
|
+
rect = @rect.height(50)
|
182
|
+
rect.is_a?(CGRect).should == true
|
183
|
+
CGRectEqualToRect(rect, CGRectMake(10, 100, 50, 50)).should == true
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
describe "#height=" do
|
188
|
+
it "sets value" do
|
189
|
+
rect = CGRect.empty
|
190
|
+
rect.height = 50
|
191
|
+
rect.size.height.should == 50
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
describe "#left" do
|
196
|
+
it "works" do
|
197
|
+
rect = CGRect.empty
|
198
|
+
rect = rect.left(20)
|
199
|
+
rect.origin.x.should == -20
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
describe "#right" do
|
204
|
+
it "works" do
|
205
|
+
rect = CGRect.empty.right(20)
|
206
|
+
rect.origin.x.should == 20
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
describe "#up" do
|
211
|
+
it "works" do
|
212
|
+
rect = CGRect.empty.up(20)
|
213
|
+
rect.origin.y.should == -20
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
217
|
+
describe "#down" do
|
218
|
+
it "works" do
|
219
|
+
rect = CGRect.empty.down(20)
|
220
|
+
rect.origin.y.should == 20
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|
224
|
+
describe "#above" do
|
225
|
+
it "works with margins" do
|
226
|
+
rect = CGRect.make(height: 50).above(20)
|
227
|
+
rect.origin.y.should == -70
|
228
|
+
end
|
229
|
+
|
230
|
+
it "works without margins" do
|
231
|
+
rect = CGRect.make(height: 50).above
|
232
|
+
rect.origin.y.should == -50
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
describe "#below" do
|
237
|
+
it "works" do
|
238
|
+
rect = CGRect.make(height: 50).below(20)
|
239
|
+
rect.origin.y.should == 70
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
243
|
+
describe "#before" do
|
244
|
+
it "works" do
|
245
|
+
rect = CGRect.make(x: 50).before(20)
|
246
|
+
rect.origin.x.should == 30
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
250
|
+
describe "#before:width:" do
|
251
|
+
it "works" do
|
252
|
+
rect = CGRect.make(x: 50).before(20, width: 50)
|
253
|
+
rect.origin.x.should == -20
|
254
|
+
end
|
255
|
+
end
|
256
|
+
|
257
|
+
describe "#beside" do
|
258
|
+
it "works with margins" do
|
259
|
+
rect = CGRect.make(x: 50, width: 20).beside(10)
|
260
|
+
rect.origin.x.should == 80
|
261
|
+
end
|
262
|
+
|
263
|
+
it "works without margins" do
|
264
|
+
rect = CGRect.make(x: 50, width: 20).beside
|
265
|
+
rect.origin.x.should == 70
|
266
|
+
end
|
267
|
+
end
|
268
|
+
|
269
|
+
describe "#center" do
|
270
|
+
it "works" do
|
271
|
+
point = CGRect.make(width: 100, height: 100).center
|
272
|
+
point.is_a?(CGPoint).should == true
|
273
|
+
CGPointEqualToPoint(point, CGPointMake(50, 50)).should == true
|
274
|
+
end
|
275
|
+
|
276
|
+
it "works as relative" do
|
277
|
+
point = CGRect.make(x: 50, y: 50, width: 100, height: 100).center(true)
|
278
|
+
point.is_a?(CGPoint).should == true
|
279
|
+
CGPointEqualToPoint(point, CGPointMake(100, 100)).should == true
|
280
|
+
end
|
281
|
+
end
|
282
|
+
|
283
|
+
describe "#round" do
|
284
|
+
it "works" do
|
285
|
+
rect = CGRect.make(x: 10.5, y: 10.4, width: 100.025, height: 100.75)
|
286
|
+
CGRectEqualToRect(rect.round, CGRectMake(11, 10, 100, 101)).should == true
|
287
|
+
end
|
288
|
+
end
|
289
|
+
|
290
|
+
describe "#centered_in" do
|
291
|
+
it "works" do
|
292
|
+
outer_rect = CGRect.make(width: 100, height: 100)
|
293
|
+
inner_rect = CGRect.make(width: 50, height: 50)
|
294
|
+
|
295
|
+
centered_rect = inner_rect.centered_in(outer_rect)
|
296
|
+
CGRectEqualToRect(centered_rect, CGRectMake(25, 25, 50, 50)).should == true
|
297
|
+
end
|
298
|
+
|
299
|
+
it "works as relative" do
|
300
|
+
outer_rect = CGRect.make(x: 20, y: 30, width: 100, height: 100)
|
301
|
+
inner_rect = CGRect.make(width: 50, height: 50)
|
302
|
+
|
303
|
+
centered_rect = inner_rect.centered_in(outer_rect, true)
|
304
|
+
CGRectEqualToRect(centered_rect, CGRectMake(45, 55, 50, 50)).should == true
|
305
|
+
end
|
306
|
+
end
|
307
|
+
|
308
|
+
describe "#+" do
|
309
|
+
it "works with CGRect" do
|
310
|
+
_rect = CGRectMake(10, 100, 50, 20)
|
311
|
+
rect = (_rect + CGRectMake(0, 0, 50, 110))
|
312
|
+
CGRectEqualToRect(rect, CGRectMake(0, 0, 60, 120)).should == true
|
313
|
+
end
|
314
|
+
|
315
|
+
it "works with CGSize" do
|
316
|
+
size = CGSizeMake(50, 20)
|
317
|
+
rect = (@rect + size)
|
318
|
+
CGRectEqualToRect(rect, CGRectMake(10, 100, 100, 40)).should == true
|
319
|
+
end
|
320
|
+
|
321
|
+
it "works with CGPoint" do
|
322
|
+
point = CGPointMake(50, 20)
|
323
|
+
rect = (@rect + point)
|
324
|
+
CGRectEqualToRect(rect, CGRectMake(60, 120, 50, 20)).should == true
|
325
|
+
end
|
326
|
+
|
327
|
+
it "works with UIOffset" do
|
328
|
+
offset = UIOffsetMake(50, 20)
|
329
|
+
rect = (@rect + offset)
|
330
|
+
CGRectEqualToRect(rect, CGRectMake(60, 120, 50, 20)).should == true
|
331
|
+
end
|
332
|
+
|
333
|
+
it "works with UIEdgeInsets" do
|
334
|
+
inset = UIEdgeInsetsMake(10, 10, 10, 5)
|
335
|
+
rect = (@rect + inset)
|
336
|
+
CGRectEqualToRect(rect, CGRectMake(20, 110, 35, 0)).should == true
|
337
|
+
end
|
338
|
+
end
|
339
|
+
|
340
|
+
describe "#intersection_with" do
|
341
|
+
it "should work" do
|
342
|
+
lower_rect = CGRectMake(0, 0, 100, 100)
|
343
|
+
upper_rect = CGRectMake(10, 10, 100, 100)
|
344
|
+
rect = lower_rect.intersection_with(upper_rect)
|
345
|
+
CGRectEqualToRect(rect, CGRectMake(10, 10, 90, 90)).should == true
|
346
|
+
end
|
347
|
+
end
|
348
|
+
|
349
|
+
describe "#union_with" do
|
350
|
+
it "should work" do
|
351
|
+
lower_rect = CGRectMake(0, 0, 100, 100)
|
352
|
+
upper_rect = CGRectMake(10, 10, 100, 100)
|
353
|
+
rect = lower_rect.union_with(upper_rect)
|
354
|
+
CGRectEqualToRect(rect, CGRectMake(0, 0, 110, 110)).should == true
|
355
|
+
end
|
356
|
+
end
|
357
|
+
|
358
|
+
describe "#grow" do
|
359
|
+
it "should work with Numeric" do
|
360
|
+
rect = @rect.grow(10)
|
361
|
+
CGRectEqualToRect(rect, CGRectMake(0, 90, 70, 40)).should == true
|
362
|
+
end
|
363
|
+
|
364
|
+
it "should work with CGSize" do
|
365
|
+
rect = @rect.grow(CGSizeMake(10, 20))
|
366
|
+
CGRectEqualToRect(rect, CGRectMake(0, 80, 70, 60)).should == true
|
367
|
+
end
|
368
|
+
end
|
369
|
+
|
370
|
+
describe "#shrink" do
|
371
|
+
it "should work with Numeric" do
|
372
|
+
rect = @rect.shrink(10)
|
373
|
+
CGRectEqualToRect(rect, CGRectMake(20, 110, 30, 0)).should == true
|
374
|
+
end
|
375
|
+
|
376
|
+
it "should work with CGSize" do
|
377
|
+
rect = @rect.shrink(CGSizeMake(20, 10))
|
378
|
+
CGRectEqualToRect(rect, CGRectMake(30, 110, 10, 0)).should == true
|
379
|
+
end
|
380
|
+
end
|
381
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
describe "CGSize" do
|
2
|
+
before do
|
3
|
+
@size = CGSize.make(width: 100, height: 200)
|
4
|
+
end
|
5
|
+
|
6
|
+
describe ".make" do
|
7
|
+
it "should work with options" do
|
8
|
+
CGSizeEqualToSize(@size, CGSizeMake(100, 200)).should == true
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should work with no options" do
|
12
|
+
CGSizeEqualToSize(CGSize.make, CGSizeMake(0, 0)).should == true
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "#rect_at_point" do
|
17
|
+
it "should work" do
|
18
|
+
point = CGPointMake(20, 30)
|
19
|
+
@size.rect_at_point(point).should == CGRectMake(20, 30, 100, 200)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "#+" do
|
24
|
+
it "should work with CGSize" do
|
25
|
+
size = CGSizeMake(20, 30)
|
26
|
+
(@size + size).should == CGSizeMake(120, 230)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should work with CGPoint" do
|
30
|
+
point = CGPointMake(20, 30)
|
31
|
+
(@size + point).should == CGRectMake(20, 30, 100, 200)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe "#infinite?" do
|
36
|
+
it "should return true" do
|
37
|
+
infinite = CGSize.infinite
|
38
|
+
infinite.infinite?.should == true
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "#empty?" do
|
43
|
+
it "should return true" do
|
44
|
+
empty = CGSizeMake(0, 0)
|
45
|
+
empty.empty?.should == true
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe "#==?" do
|
50
|
+
it "should return true" do
|
51
|
+
size = CGSizeMake(100, 200)
|
52
|
+
@size.should == size
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should return false" do
|
56
|
+
size = CGSizeMake(20, 10)
|
57
|
+
@size.should != size
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: geomotion
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: '0.1'
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-10-
|
12
|
+
date: 2012-10-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -41,9 +41,13 @@ files:
|
|
41
41
|
- Rakefile
|
42
42
|
- app/app_delegate.rb
|
43
43
|
- lib/geomotion.rb
|
44
|
+
- lib/geomotion/cg_point.rb
|
44
45
|
- lib/geomotion/cg_rect.rb
|
46
|
+
- lib/geomotion/cg_size.rb
|
45
47
|
- lib/geomotion/version.rb
|
46
|
-
- spec/
|
48
|
+
- spec/cg_point_spec.rb
|
49
|
+
- spec/cg_rect_spec.rb
|
50
|
+
- spec/cg_size_spec.rb
|
47
51
|
homepage: https://github.com/clayallsopp/geomotion
|
48
52
|
licenses: []
|
49
53
|
post_install_message:
|
@@ -69,4 +73,6 @@ signing_key:
|
|
69
73
|
specification_version: 3
|
70
74
|
summary: A RubyMotion Geometry Wrapper
|
71
75
|
test_files:
|
72
|
-
- spec/
|
76
|
+
- spec/cg_point_spec.rb
|
77
|
+
- spec/cg_rect_spec.rb
|
78
|
+
- spec/cg_size_spec.rb
|
data/spec/main_spec.rb
DELETED
@@ -1,36 +0,0 @@
|
|
1
|
-
describe "CGRect" do
|
2
|
-
before do
|
3
|
-
@rect = CGRect.make(x: 10, y: 100, width: 50, height: 20)
|
4
|
-
end
|
5
|
-
|
6
|
-
it ".x, etc work" do
|
7
|
-
[@rect.x, @rect.y, @rect.width, @rect.height].should == [10, 100, 50, 20]
|
8
|
-
end
|
9
|
-
|
10
|
-
it "chaining works" do
|
11
|
-
rect = @rect.below(10).width(100).height(10)
|
12
|
-
[rect.x, rect.y, rect.width, rect.height].should == [10, 130, 100, 10]
|
13
|
-
|
14
|
-
[@rect.right(20).x, @rect.left(20).x, @rect.up(20).y, @rect.down(20).y].should == [30, -10, 80, 120]
|
15
|
-
end
|
16
|
-
|
17
|
-
it ".beside works" do
|
18
|
-
rect = @rect.beside(10)
|
19
|
-
[rect.x, rect.y, rect.width, rect.height].should == [70, 100, 50, 20]
|
20
|
-
end
|
21
|
-
|
22
|
-
it ".below works" do
|
23
|
-
@rect.below(10).y.should == 130
|
24
|
-
end
|
25
|
-
|
26
|
-
it "layout works" do
|
27
|
-
rect2 = CGRect.new [50, 50], [100, 100]
|
28
|
-
rect3 = CGRect.new [100, 200], [20, 20]
|
29
|
-
|
30
|
-
no_margins = CGRect.layout(@rect, above: rect2, right_of: rect3)
|
31
|
-
[no_margins.x, no_margins.y, no_margins.width, no_margins.height].should == [120, 30, 50, 20]
|
32
|
-
|
33
|
-
margins = CGRect.layout(@rect, above: rect2, right_of: rect3, margins: [0, 0, 10, 15])
|
34
|
-
[margins.x, margins.y, margins.width, margins.height].should == [135, 20, 50, 20]
|
35
|
-
end
|
36
|
-
end
|