perfect-shape 0.0.9 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 934071723ece0f7ebe64e6e67880b5a1f4ec6781517d2ee2a7dea70ceae4a9be
4
- data.tar.gz: d436da8745d7f4d971a7df04b5d54701e6e89abb9f8624273f79a6ac1d28ea77
3
+ metadata.gz: a4eb7bc277a02c0795966623e05ee57514483d982bc0d80f264a525f6b3d5afa
4
+ data.tar.gz: f070a8292b835f6d2c68ff4ccddd3073b7a83d36e882d4e5c34c6e91a85d88ee
5
5
  SHA512:
6
- metadata.gz: bcea5c4c02f2e056c0e18a857404bbef4b411420c2c3a7e438a2f122df90a30466979c60cc7804c61b5d454ed2879cd81fd1fc750360839393c20a4561aebce6
7
- data.tar.gz: c321b5b1697b28e9608c11d4bc5e04038c3e269df311b9c7263e468c6f952c60bec6ecf4244ff044eb8fb33cf68462e9d362326962c0473bbd72150847181e0e
6
+ metadata.gz: 1add8519bdd16f38f8fb8431d8342c77df9f5d8d76f0dc6f1481bba5d9c0a15b653c56a9a0e372082f1590b55f5897ebac3f6a4d4505acd6ba017f257261e80d
7
+ data.tar.gz: fc3e2fce45f0a46ad7869fd5fec07d0dcf826e3bd36676439144eb3fd0198b8224d816a26db54858b43d0bb5332ff6b4108950efca3e71c1d5e42cebde4de63f
data/CHANGELOG.md CHANGED
@@ -1,5 +1,32 @@
1
1
  # Change Log
2
2
 
3
+ ## 0.1.1
4
+
5
+ - `PerfectShape::QuadraticBezierCurve` (two end points and one control point)
6
+ - `PerfectShape::QuadraticBezierCurve#contain?(x_or_point, y=nil)`
7
+ - `PerfectShape::QuadraticBezierCurve#==`
8
+ - `PerfectShape::Path` having quadratic bezier curves in addition to points and lines
9
+
10
+ ## 0.1.0
11
+
12
+ - `PerfectShape::Path` (having points or lines)
13
+ - `PerfectShape::Path#contain?(x_or_point, y=nil, distance: 0)`
14
+ - `PerfectShape::Path#point_crossings(x_or_point, y=nil)`
15
+ - `PerfectShape::Path#==`
16
+
17
+ ## 0.0.11
18
+
19
+ - `PerfectShape::Polygon#==`
20
+ - `PerfectShape::Line#==`
21
+ - `PerfectShape::Point#==`
22
+
23
+ ## 0.0.10
24
+
25
+ - `PerfectShape::Point`
26
+ - `PerfectShape::Point#point_distance`
27
+ - `PerfectShape::Point#contain?(x_or_point, y=nil, distance: 0)`
28
+ - Refactor `PerfectShape::Point`,`PerfectShape::RectangularShape` to include shared `PerfectShape::PointLocation`
29
+
3
30
  ## 0.0.9
4
31
 
5
32
  - `PerfectShape::Line#contain?(x_or_point, y=nil, distance: 0)` (add a distance tolerance fuzz factor option)
@@ -14,6 +41,8 @@
14
41
 
15
42
  ## 0.0.7
16
43
 
44
+ - `PerfectShape::Polygon`
45
+ - `PerfectShape::Polygon#contain?(x_or_point, y)` (Ray Casting Algorithm, aka Even-Odd Rule)
17
46
  - `PerfectShape::Shape#min_x`/`PerfectShape::Shape#min_y`/`PerfectShape::Shape#max_x`/`PerfectShape::Shape#max_y`/`PerfectShape::Shape#center_x`/`PerfectShape::Shape#center_y`/`PerfectShape::Shape#bounding_box`
18
47
 
19
48
  ## 0.0.6
data/README.md CHANGED
@@ -1,25 +1,26 @@
1
- # Perfect Shape 0.0.9
1
+ # Perfect Shape 0.1.1
2
2
  ## Geometric Algorithms
3
3
  [![Gem Version](https://badge.fury.io/rb/perfect-shape.svg)](http://badge.fury.io/rb/perfect-shape)
4
+ [![Test](https://github.com/AndyObtiva/perfect-shape/actions/workflows/ruby.yml/badge.svg)](https://github.com/AndyObtiva/perfect-shape/actions/workflows/ruby.yml)
4
5
 
5
- `PerfectShape` is a collection of pure Ruby geometric algorithms that are mostly useful for GUI (Graphical User Interface) manipulation like checking containment of a mouse click point in popular geometry shapes such as rectangle, square, arc (open, chord, and pie), ellipse, circle, polygon (ray casting algorithm/even-odd rule), polyline, polyquad, polycubic, and paths containing lines, bezier curves, and quadratic curves.
6
+ [`PerfectShape`](https://rubygems.org/gems/perfect-shape) is a collection of pure Ruby geometric algorithms that are mostly useful for GUI (Graphical User Interface) manipulation like checking containment of a mouse click [point](#perfectshapepoint) in popular geometry shapes such as [rectangle](#perfectshaperectangle), [square](#perfectshapesquare), [arc](#perfectshapearc) (open, chord, and pie), [ellipse](#perfectshapeellipse), [circle](#perfectshapecircle), [polygon](#perfectshapepolygon), and [paths](#perfectshapepath) containing [lines](#perfectshapeline), [quadratic bézier curves](#perfectshapequadraticbeziercurve), and cubic bézier curves (including both [Ray Casting Algorithm](https://en.wikipedia.org/wiki/Point_in_polygon#Ray_casting_algorithm), aka [Even-odd Rule](https://en.wikipedia.org/wiki/Even%E2%80%93odd_rule), and [Winding Number Algorithm](https://en.wikipedia.org/wiki/Point_in_polygon#Winding_number_algorithm), aka [Nonzero Rule](https://en.wikipedia.org/wiki/Nonzero-rule)).
6
7
 
7
- Additionally, `PerfectShape::Math` contains some purely mathematical algorithms.
8
+ Additionally, [`PerfectShape::Math`](#perfectshapemath) contains some purely mathematical algorithms, like [IEEE 754-1985 Remainder](https://en.wikipedia.org/wiki/IEEE_754-1985).
8
9
 
9
- To ensure high accuracy, this library does all its mathematical operations with `BigDecimal` numbers.
10
+ To ensure high accuracy, this library does all its mathematical operations with [`BigDecimal`](https://ruby-doc.org/stdlib-3.0.2/libdoc/bigdecimal/rdoc/BigDecimal.html) numbers.
10
11
 
11
12
  ## Setup
12
13
 
13
14
  Run:
14
15
 
15
16
  ```
16
- gem install perfect-shape -v 0.0.9
17
+ gem install perfect-shape -v 0.1.1
17
18
  ```
18
19
 
19
20
  Or include in Bundler `Gemfile`:
20
21
 
21
22
  ```ruby
22
- gem 'perfect-shape', '~> 0.0.9'
23
+ gem 'perfect-shape', '~> 0.1.1'
23
24
  ```
24
25
 
25
26
  And, run:
@@ -43,6 +44,8 @@ Module
43
44
 
44
45
  Class
45
46
 
47
+ This is a base class for all shapes. It is not meant to be used directly. Subclasses implement/override its methods as needed.
48
+
46
49
  - `#min_x`: min x
47
50
  - `#min_y`: min y
48
51
  - `#max_x`: max x
@@ -52,12 +55,25 @@ Class
52
55
  - `#center_x`: center x
53
56
  - `#center_y`: center y
54
57
  - `#bounding_box`: bounding box is a rectangle with x = min x, y = min y, and width/height just as those of shape
55
- - `#normalize_point(x_or_point, y = nil)`: normalizes point into an `Array` of (x,y) coordinates
58
+ - `#normalize_point(x_or_point, y = nil)`: normalizes point into an `Array` of `[x,y]` coordinates
59
+ - `#==(other)`: Returns `true` if equal to `other` or `false` otherwise
60
+
61
+ ### `PerfectShape::PointLocation`
62
+
63
+ Module
64
+
65
+ - `#initialize(x: 0, y: 0)`: initializes a point location, usually representing the top-left point in a shape
66
+ - `#x`: top-left x
67
+ - `#y`: top-left y
68
+ - `#min_x`: min x (x by default)
69
+ - `#min_y`: min y (y by default)
56
70
 
57
71
  ### `PerfectShape::RectangularShape`
58
72
 
59
73
  Module
60
74
 
75
+ Includes `PerfectShape::PointLocation`
76
+
61
77
  - `#initialize(x: 0, y: 0, width: 1, height: 1)`: initializes a rectangular shape
62
78
  - `#x`: top-left x
63
79
  - `#y`: top-left y
@@ -71,18 +87,53 @@ Module
71
87
  - `#center_y`: center y
72
88
  - `#bounding_box`: bounding box is a rectangle with x = min x, y = min y, and width/height of shape
73
89
 
90
+ ### `PerfectShape::Point`
91
+
92
+ Class
93
+
94
+ Extends `PerfectShape::Shape`
95
+
96
+ Includes `PerfectShape::PointLocation`
97
+
98
+ ![point](https://raw.githubusercontent.com/AndyObtiva/perfect-shape/master/images/point.png)
99
+
100
+ Points are simply represented by an `Array` of `[x,y]` coordinates when used within other shapes, but when needing point-specific operations like `point_distance`, the `PerfectShape::Point` class can come in handy.
101
+
102
+ - `::point_distance(x, y, px, py)`: Returns the distance from a point to another point
103
+ - `::new(x_or_point=nil, y_arg=nil, x: nil, y: nil)`: constructs a point with (x,y) pair (default: 0,0) whether specified as `Array` of (x,y) pair, flat `x,y` args, or `x:, y:` kwargs.
104
+ - `#min_x`: min x (always x)
105
+ - `#min_y`: min y (always y)
106
+ - `#max_x`: max x (always x)
107
+ - `#max_y`: max y (always y)
108
+ - `#width`: width (always 0)
109
+ - `#height`: height (always 0)
110
+ - `#center_x`: center x (always x)
111
+ - `#center_y`: center y (always y)
112
+ - `#bounding_box`: bounding box is a rectangle with x = min x, y = min y, and width/height of shape
113
+ - `#contain?(x_or_point, y=nil, distance: 0)`: checks if point matches self, with a distance tolerance (0 by default). Distance tolerance provides a fuzz factor that for example enables GUI users to mouse-click-select a point shape in a GUI more successfully.
114
+ - `#point_distance(x_or_point, y=nil)`: Returns the distance from a point to another point
115
+ - `#==(other)`: Returns `true` if equal to `other` or `false` otherwise
116
+
117
+ Example:
118
+
119
+ ```ruby
120
+ shape = PerfectShape::Point.new(x: 200, y: 150)
121
+ ```
122
+
74
123
  ### `PerfectShape::Line`
75
124
 
76
125
  Class
77
126
 
78
127
  Extends `PerfectShape::Shape`
79
128
 
80
- ![line](images/line.png)
129
+ Includes `PerfectShape::MultiPoint`
130
+
131
+ ![line](https://raw.githubusercontent.com/AndyObtiva/perfect-shape/master/images/line.png)
81
132
 
82
133
  - `::relative_counterclockwise(x1, y1, x2, y2, px, py)`: Returns an indicator of where the specified point (px,py) lies with respect to the line segment from (x1,y1) to (x2,y2). The return value can be either 1, -1, or 0 and indicates in which direction the specified line must pivot around its first end point, (x1,y1), in order to point at the specified point (px,py). A return value of 1 indicates that the line segment must turn in the direction that takes the positive X axis towards the negative Y axis. In the default coordinate system used by Java 2D, this direction is counterclockwise. A return value of -1 indicates that the line segment must turn in the direction that takes the positive X axis towards the positive Y axis. In the default coordinate system, this direction is clockwise. A return value of 0 indicates that the point lies exactly on the line segment. Note that an indicator value of 0 is rare and not useful for determining collinearity because of floating point rounding issues. If the point is colinear with the line segment, but not between the end points, then the value will be -1 if the point lies “beyond (x1,y1)” or 1 if the point lies “beyond (x2,y2)”.
83
134
  - `::point_segment_distance_square(x1, y1, x2, y2, px, py)`: Returns the square of distance from a point to a line segment.
84
135
  - `::point_segment_distance(x1, y1, x2, y2, px, py)`: Returns the distance from a point to a line segment.
85
- - `::new(points: nil)`: constructs a polygon with `points` as `Array` of `Array`s of (x,y) pairs or flattened `Array` of alternating x and y values
136
+ - `::new(points: [])`: constructs a line with two `points` as `Array` of `Array`s of `[x,y]` pairs or flattened `Array` of alternating x and y values
86
137
  - `#min_x`: min x
87
138
  - `#min_y`: min y
88
139
  - `#max_x`: max x
@@ -95,6 +146,42 @@ Extends `PerfectShape::Shape`
95
146
  - `#contain?(x_or_point, y=nil, distance: 0)`: checks if point lies on line, with a distance tolerance (0 by default). Distance tolerance provides a fuzz factor that for example enables GUI users to mouse-click-select a line shape in a GUI more successfully.
96
147
  - `#relative_counterclockwise(x_or_point, y=nil)`: Returns an indicator of where the specified point (px,py) lies with respect to the line segment from (x1,y1) to (x2,y2). The return value can be either 1, -1, or 0 and indicates in which direction the specified line must pivot around its first end point, (x1,y1), in order to point at the specified point (px,py). A return value of 1 indicates that the line segment must turn in the direction that takes the positive X axis towards the negative Y axis. In the default coordinate system used by Java 2D, this direction is counterclockwise. A return value of -1 indicates that the line segment must turn in the direction that takes the positive X axis towards the positive Y axis. In the default coordinate system, this direction is clockwise. A return value of 0 indicates that the point lies exactly on the line segment. Note that an indicator value of 0 is rare and not useful for determining collinearity because of floating point rounding issues. If the point is colinear with the line segment, but not between the end points, then the value will be -1 if the point lies “beyond (x1,y1)” or 1 if the point lies “beyond (x2,y2)”.
97
148
  - `#point_segment_distance(x_or_point, y=nil)`: Returns the distance from a point to a line segment.
149
+ - `#==(other)`: Returns `true` if equal to `other` or `false` otherwise
150
+
151
+ Example:
152
+
153
+ ```ruby
154
+ shape = PerfectShape::Line.new(points: [[200, 150], [270, 220]]) # start point and end point
155
+ ```
156
+
157
+ ### `PerfectShape::QuadraticBezierCurve`
158
+
159
+ Class
160
+
161
+ Extends `PerfectShape::Shape`
162
+
163
+ Includes `PerfectShape::MultiPoint`
164
+
165
+ ![quadratic_bezier_curve](https://raw.githubusercontent.com/AndyObtiva/perfect-shape/master/images/quadratic_bezier_curve.png)
166
+
167
+ - `::new(points: [])`: constructs a quadratic bézier curve with three `points` (two end points and one control point) as `Array` of `Array`s of `[x,y]` pairs or flattened `Array` of alternating x and y values
168
+ - `#min_x`: min x
169
+ - `#min_y`: min y
170
+ - `#max_x`: max x
171
+ - `#max_y`: max y
172
+ - `#width`: width (from min x to max x)
173
+ - `#height`: height (from min y to max y)
174
+ - `#center_x`: center x
175
+ - `#center_y`: center y
176
+ - `#bounding_box`: bounding box is a rectangle with x = min x, y = min y, and width/height of shape (bounding box only guarantees that the shape is within it, but it might be bigger than the shape)
177
+ - `#contain?(x_or_point, y=nil)`: checks if point is inside
178
+ - `#==(other)`: Returns `true` if equal to `other` or `false` otherwise
179
+
180
+ Example:
181
+
182
+ ```ruby
183
+ shape = PerfectShape::QuadraticBezierCurve.new(points: [[200, 150], [270, 220], [180, 170]]) # start point, control point, and end point
184
+ ```
98
185
 
99
186
  ### `PerfectShape::Rectangle`
100
187
 
@@ -104,7 +191,7 @@ Extends `PerfectShape::Shape`
104
191
 
105
192
  Includes `PerfectShape::RectangularShape`
106
193
 
107
- ![rectangle](images/rectangle.png)
194
+ ![rectangle](https://raw.githubusercontent.com/AndyObtiva/perfect-shape/master/images/rectangle.png)
108
195
 
109
196
  - `::new(x: 0, y: 0, width: 1, height: 1)`: constructs a rectangle
110
197
  - `#x`: top-left x
@@ -119,6 +206,13 @@ Includes `PerfectShape::RectangularShape`
119
206
  - `#max_y`: max y
120
207
  - `#bounding_box`: bounding box is a rectangle with x = min x, y = min y, and width/height of shape
121
208
  - `#contain?(x_or_point, y=nil)`: checks if point is inside
209
+ - `#==(other)`: Returns `true` if equal to `other` or `false` otherwise
210
+
211
+ Example:
212
+
213
+ ```ruby
214
+ shape = PerfectShape::Rectangle.new(x: 15, y: 30, width: 200, height: 100)
215
+ ```
122
216
 
123
217
  ### `PerfectShape::Square`
124
218
 
@@ -126,7 +220,7 @@ Class
126
220
 
127
221
  Extends `PerfectShape::Rectangle`
128
222
 
129
- ![square](images/square.png)
223
+ ![square](https://raw.githubusercontent.com/AndyObtiva/perfect-shape/master/images/square.png)
130
224
 
131
225
  - `::new(x: 0, y: 0, length: 1)`: constructs a square
132
226
  - `#x`: top-left x
@@ -142,6 +236,13 @@ Extends `PerfectShape::Rectangle`
142
236
  - `#max_y`: max y
143
237
  - `#bounding_box`: bounding box is a rectangle with x = min x, y = min y, and width/height of shape
144
238
  - `#contain?(x_or_point, y=nil)`: checks if point is inside
239
+ - `#==(other)`: Returns `true` if equal to `other` or `false` otherwise
240
+
241
+ Example:
242
+
243
+ ```ruby
244
+ shape = PerfectShape::Square.new(x: 15, y: 30, length: 200)
245
+ ```
145
246
 
146
247
  ### `PerfectShape::Arc`
147
248
 
@@ -155,7 +256,7 @@ Arcs can be of type `:open`, `:chord`, or `:pie`
155
256
 
156
257
  Open Arc | Chord Arc | Pie Arc
157
258
  ---------|-----------|--------
158
- ![arc-open](images/arc-open.png) | ![arc-chord](images/arc-chord.png) | ![arc-pie](images/arc-pie.png)
259
+ ![arc-open](https://raw.githubusercontent.com/AndyObtiva/perfect-shape/master/images/arc-open.png) | ![arc-chord](https://raw.githubusercontent.com/AndyObtiva/perfect-shape/master/images/arc-chord.png) | ![arc-pie](https://raw.githubusercontent.com/AndyObtiva/perfect-shape/master/images/arc-pie.png)
159
260
 
160
261
  - `::new(type: :open, x: 0, y: 0, width: 1, height: 1, start: 0, extent: 360, center_x: nil, center_y: nil, radius_x: nil, radius_y: nil)`: constructs an arc of type `:open` (default), `:chord`, or `:pie`
161
262
  - `#type`: `:open`, `:chord`, or `:pie`
@@ -175,6 +276,14 @@ Open Arc | Chord Arc | Pie Arc
175
276
  - `#max_y`: max y
176
277
  - `#bounding_box`: bounding box is a rectangle with x = min x, y = min y, and width/height of shape
177
278
  - `#contain?(x_or_point, y=nil)`: checks if point is inside
279
+ - `#==(other)`: Returns `true` if equal to `other` or `false` otherwise
280
+
281
+ Example:
282
+
283
+ ```ruby
284
+ shape = PerfectShape::Arc.new(type: :chord, x: 2, y: 3, width: 50, height: 60, start: 30, extent: 90)
285
+ shape2 = PerfectShape::Arc.new(type: :chord, center_x: 2 + 25, center_y: 3 + 30, radius_x: 25, radius_y: 30, start: 30, extent: 90)
286
+ ```
178
287
 
179
288
  ### `PerfectShape::Ellipse`
180
289
 
@@ -182,7 +291,7 @@ Class
182
291
 
183
292
  Extends `PerfectShape::Arc`
184
293
 
185
- ![ellipse](images/ellipse.png)
294
+ ![ellipse](https://raw.githubusercontent.com/AndyObtiva/perfect-shape/master/images/ellipse.png)
186
295
 
187
296
  - `::new(x: 0, y: 0, width: 1, height: 1, center_x: nil, center_y: nil, radius_x: nil, radius_y: nil)`: constructs an ellipse
188
297
  - `#x`: top-left x
@@ -202,6 +311,14 @@ Extends `PerfectShape::Arc`
202
311
  - `#max_y`: max y
203
312
  - `#bounding_box`: bounding box is a rectangle with x = min x, y = min y, and width/height of shape
204
313
  - `#contain?(x_or_point, y=nil)`: checks if point is inside
314
+ - `#==(other)`: Returns `true` if equal to `other` or `false` otherwise
315
+
316
+ Example:
317
+
318
+ ```ruby
319
+ shape = PerfectShape::Ellipse.new(x: 2, y: 3, width: 50, height: 60)
320
+ shape2 = PerfectShape::Ellipse.new(center_x: 27, center_y: 33, radius_x: 25, radius_y: 30)
321
+ ```
205
322
 
206
323
  ### `PerfectShape::Circle`
207
324
 
@@ -209,7 +326,7 @@ Class
209
326
 
210
327
  Extends `PerfectShape::Ellipse`
211
328
 
212
- ![circle](images/circle.png)
329
+ ![circle](https://raw.githubusercontent.com/AndyObtiva/perfect-shape/master/images/circle.png)
213
330
 
214
331
  - `::new(x: 0, y: 0, diameter: 1, width: 1, height: 1, center_x: nil, center_y: nil, radius: nil, radius_x: nil, radius_y: nil)`: constructs a circle
215
332
  - `#x`: top-left x
@@ -231,6 +348,14 @@ Extends `PerfectShape::Ellipse`
231
348
  - `#max_y`: max y
232
349
  - `#bounding_box`: bounding box is a rectangle with x = min x, y = min y, and width/height of shape
233
350
  - `#contain?(x_or_point, y=nil)`: checks if point is inside
351
+ - `#==(other)`: Returns `true` if equal to `other` or `false` otherwise
352
+
353
+ Example:
354
+
355
+ ```ruby
356
+ shape = PerfectShape::Circle.new(x: 2, y: 3, diameter: 60)
357
+ shape2 = PerfectShape::Circle.new(center_x: 2 + 30, center_y: 3 + 30, radius: 30)
358
+ ```
234
359
 
235
360
  ### `PerfectShape::Polygon`
236
361
 
@@ -238,9 +363,13 @@ Class
238
363
 
239
364
  Extends `PerfectShape::Shape`
240
365
 
241
- ![polygon](images/polygon.png)
366
+ Includes `PerfectShape::MultiPoint`
367
+
368
+ A polygon can be thought of as a special case of [path](#perfectshapepath) that is closed, has the [Even-Odd](https://en.wikipedia.org/wiki/Even%E2%80%93odd_rule) winding rule, and consists of lines only.
242
369
 
243
- - `::new(points: nil)`: constructs a polygon with `points` as `Array` of `Array`s of (x,y) pairs or flattened `Array` of alternating x and y values
370
+ ![polygon](https://raw.githubusercontent.com/AndyObtiva/perfect-shape/master/images/polygon.png)
371
+
372
+ - `::new(points: [])`: constructs a polygon with `points` as `Array` of `Array`s of `[x,y]` pairs or flattened `Array` of alternating x and y values
244
373
  - `#min_x`: min x
245
374
  - `#min_y`: min y
246
375
  - `#max_x`: max x
@@ -251,6 +380,52 @@ Extends `PerfectShape::Shape`
251
380
  - `#center_y`: center y
252
381
  - `#bounding_box`: bounding box is a rectangle with x = min x, y = min y, and width/height of shape
253
382
  - `#contain?(x_or_point, y=nil)`: checks if point is inside using the [Ray Casting Algorithm](https://en.wikipedia.org/wiki/Point_in_polygon) (aka [Even-Odd Rule](https://en.wikipedia.org/wiki/Even%E2%80%93odd_rule))
383
+ - `#==(other)`: Returns `true` if equal to `other` or `false` otherwise
384
+
385
+ Example:
386
+
387
+ ```ruby
388
+ shape = PerfectShape::Polygon.new(points: [[200, 150], [270, 170], [250, 220], [220, 190], [200, 200], [180, 170]])
389
+ ```
390
+
391
+ ### `PerfectShape::Path`
392
+
393
+ Class
394
+
395
+ Extends `PerfectShape::Shape`
396
+
397
+ Includes `PerfectShape::MultiPoint`
398
+
399
+ ![path](https://raw.githubusercontent.com/AndyObtiva/perfect-shape/master/images/path.png)
400
+
401
+ - `::new(shapes: [], closed: false, winding_rule: :wind_non_zero)`: constructs a path with `shapes` as `Array` of shape objects, which can be `PerfectShape::Point` (or `Array` of `[x, y]` coordinates), `PerfectShape::Line`, or `PerfectShape::QuadraticBezierCurve`. If a path is closed, its last point is automatically connected to its first point with a line segment. The winding rule can be `:wind_non_zero` (default) or `:wind_even_odd`.
402
+ - `#shapes`: the shapes that the path is composed of (must always start with `PerfectShape::Point` or Array of [x,y] coordinates representing start point)
403
+ - `#closed?`: returns `true` if closed and `false` otherwise
404
+ - `#winding_rule`: returns winding rule (`:wind_non_zero` or `:wind_even_odd`)
405
+ - `#points`: path points calculated (derived) from shapes
406
+ - `#min_x`: min x
407
+ - `#min_y`: min y
408
+ - `#max_x`: max x
409
+ - `#max_y`: max y
410
+ - `#width`: width (from min x to max x)
411
+ - `#height`: height (from min y to max y)
412
+ - `#center_x`: center x
413
+ - `#center_y`: center y
414
+ - `#bounding_box`: bounding box is a rectangle with x = min x, y = min y, and width/height of shape (bounding box only guarantees that the shape is within it, but it might be bigger than the shape)
415
+ - `#contain?(x_or_point, y=nil)`: checks if point is inside path utilizing the configured winding rule, which can be the [Nonzero-Rule](https://en.wikipedia.org/wiki/Nonzero-rule) (aka [Winding Number Algorithm](https://en.wikipedia.org/wiki/Point_in_polygon#Winding_number_algorithm)) or the [Even-Odd Rule](https://en.wikipedia.org/wiki/Even%E2%80%93odd_rule) (aka [Ray Casting Algorithm](https://en.wikipedia.org/wiki/Point_in_polygon#Ray_casting_algorithm))
416
+ - `#point_crossings(x_or_point, y=nil)`: calculates the number of times the given path crosses the ray extending to the right from (x,y)
417
+ - `#==(other)`: Returns `true` if equal to `other` or `false` otherwise
418
+
419
+ Example:
420
+
421
+ ```ruby
422
+ path_shapes = []
423
+ path_shapes << PerfectShape::Point.new(x: 200, y: 150)
424
+ path_shapes << PerfectShape::Line.new(points: [250, 170]) # no need for start point, just end point
425
+ path_shapes << PerfectShape::QuadraticBezierCurve.new(points: [[300, 185], [350, 150]]) # no need for start point, just control point and end point
426
+
427
+ shape = PerfectShape::Path.new(shapes: path_shapes, closed: false, winding_rule: :wind_even_odd)
428
+ ```
254
429
 
255
430
  ## Process
256
431
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.9
1
+ 0.1.1
@@ -135,7 +135,7 @@ module PerfectShape
135
135
  @height = nil
136
136
  end
137
137
 
138
- # Checks if arc contains point denoted by point (two-number Array or x, y args)
138
+ # Checks if arc contains point (two-number Array or x, y args)
139
139
  #
140
140
  # @param x The X coordinate of the point to test.
141
141
  # @param y The Y coordinate of the point to test.
@@ -55,7 +55,7 @@ module PerfectShape
55
55
  end
56
56
  end
57
57
 
58
- # Checks if ellipse contains point denoted by point (two-number Array or x, y args)
58
+ # Checks if ellipse contains point (two-number Array or x, y args)
59
59
  #
60
60
  # @param x The X coordinate of the point to test.
61
61
  # @param y The Y coordinate of the point to test.
@@ -182,25 +182,41 @@ module PerfectShape
182
182
  px, py)
183
183
  BigDecimal(::Math.sqrt(point_segment_distance_square(x1, y1, x2, y2, px, py)).to_s)
184
184
  end
185
+
186
+ # Calculates the number of times the line from (x1,y1) to (x2,y2)
187
+ # crosses the ray extending to the right from (px,py).
188
+ # If the point lies on the line, then no crossings are recorded.
189
+ # +1 is returned for a crossing where the Y coordinate is increasing
190
+ # -1 is returned for a crossing where the Y coordinate is decreasing
191
+ def point_crossings(x1, y1, x2, y2, px, py)
192
+ return BigDecimal('0') if (py < y1 && py < y2)
193
+ return BigDecimal('0') if (py >= y1 && py >= y2)
194
+ # assert(y1 != y2);
195
+ return BigDecimal('0') if (px >= x1 && px >= x2)
196
+ return ((y1 < y2) ? 1 : -1) if (px < x1 && px < x2)
197
+ xintercept = x1 + (py - y1) * (x2 - x1) / (y2 - y1);
198
+ return BigDecimal('0') if (px >= xintercept)
199
+ (y1 < y2) ? BigDecimal('1') : BigDecimal('-1')
200
+ end
185
201
  end
186
202
 
187
203
  include MultiPoint
204
+ include Equalizer.new(:points)
188
205
 
189
- # Checks if polygon contains point denoted by point (two-number Array or x, y args)
190
- # using the Ray Casting Algorithm (aka Even-Odd Rule): https://en.wikipedia.org/wiki/Point_in_polygon
206
+ # Checks if line contains point (two-number Array or x, y args), with distance tolerance (0 by default)
191
207
  #
192
208
  # @param x The X coordinate of the point to test.
193
209
  # @param y The Y coordinate of the point to test.
210
+ # @param distance The distance from line to tolerate (0 by default)
194
211
  #
195
212
  # @return {@code true} if the point lies within the bound of
196
- # the polygon, {@code false} if the point lies outside of the
197
- # polygon's bounds.
213
+ # the line, {@code false} if the point lies outside of the
214
+ # line's bounds.
198
215
  def contain?(x_or_point, y = nil, distance: 0)
199
216
  x, y = normalize_point(x_or_point, y)
200
217
  return unless x && y
201
218
  distance = BigDecimal(distance.to_s)
202
- # TODO implement contain?(point) with a fuzz factor to enable successfully selecting a line in a GUI application
203
- Line.point_segment_distance(points[0][0], points[0][1], points[1][0], points[1][1], x, y) <= distance
219
+ point_segment_distance(x, y) <= distance
204
220
  end
205
221
 
206
222
  def point_segment_distance(x_or_point, y = nil)
@@ -214,5 +230,16 @@ module PerfectShape
214
230
  return unless x && y
215
231
  Line.relative_counterclockwise(points[0][0], points[0][1], points[1][0], points[1][1], x, y)
216
232
  end
233
+
234
+ # Calculates the number of times the line
235
+ # crosses the ray extending to the right from (px,py).
236
+ # If the point lies on the line, then no crossings are recorded.
237
+ # +1 is returned for a crossing where the Y coordinate is increasing
238
+ # -1 is returned for a crossing where the Y coordinate is decreasing
239
+ def point_crossings(x_or_point, y = nil)
240
+ x, y = normalize_point(x_or_point, y)
241
+ return unless x && y
242
+ Line.point_crossings(points[0][0], points[0][1], points[1][0], points[1][1], x, y)
243
+ end
217
244
  end
218
245
  end
@@ -76,9 +76,9 @@ module PerfectShape
76
76
  super || ::Math.respond_to?(method_name, include_private)
77
77
  end
78
78
 
79
- def method_missing(method_name, *args, **kwargs, &block)
79
+ def method_missing(method_name, *args, &block)
80
80
  if ::Math.respond_to?(method_name, true)
81
- ::Math.send(method_name, *args, **kwargs, &block)
81
+ ::Math.send(method_name, *args, &block)
82
82
  else
83
83
  super
84
84
  end
@@ -26,8 +26,8 @@ module PerfectShape
26
26
  module MultiPoint
27
27
  attr_reader :points
28
28
 
29
- def initialize(points: nil)
30
- self.points = points || []
29
+ def initialize(points: [])
30
+ self.points = points
31
31
  end
32
32
 
33
33
  # Sets points, normalizing to an Array of Arrays of (x,y) pairs as BigDecimal
@@ -55,13 +55,5 @@ module PerfectShape
55
55
  def max_y
56
56
  points.map(&:last).max
57
57
  end
58
-
59
- def width
60
- max_x - min_x if min_x && max_x
61
- end
62
-
63
- def height
64
- max_y - min_y if min_y && max_y
65
- end
66
58
  end
67
59
  end
@@ -0,0 +1,209 @@
1
+ # Copyright (c) 2021 Andy Maleh
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.
21
+
22
+ require 'perfect_shape/shape'
23
+ require 'perfect_shape/point'
24
+ require 'perfect_shape/line'
25
+ require 'perfect_shape/quadratic_bezier_curve'
26
+ require 'perfect_shape/multi_point'
27
+
28
+ module PerfectShape
29
+ # Mostly ported from java.awt.geom: https://docs.oracle.com/javase/8/docs/api/java/awt/geom/Path2D.html
30
+ class Path < Shape
31
+ include MultiPoint
32
+ include Equalizer.new(:shapes, :closed, :winding_rule)
33
+
34
+ SHAPE_TYPES = [Array, Point, Line]
35
+ WINDING_RULES = [:wind_non_zero, :wind_even_odd]
36
+
37
+ attr_reader :winding_rule
38
+ attr_accessor :shapes, :closed
39
+ alias closed? closed
40
+
41
+ # Constructs Path with winding rule, closed status, and shapes (must always start with PerfectShape::Point or Array of [x,y] coordinates)
42
+ # Shape class types can be any of SHAPE_TYPES: Array (x,y coordinates), PerfectShape::Point, or PerfectShape::Line
43
+ # winding_rule can be any of WINDING_RULES: :wind_non_zero (default) or :wind_even_odd
44
+ def initialize(shapes: [], closed: false, winding_rule: :wind_non_zero)
45
+ self.closed = closed
46
+ self.winding_rule = winding_rule
47
+ self.shapes = shapes
48
+ end
49
+
50
+ def points
51
+ the_points = []
52
+ @shapes.each do |shape|
53
+ case shape
54
+ when Point
55
+ the_points << shape.to_a
56
+ when Array
57
+ the_points << shape.map {|n| BigDecimal(n.to_s)}
58
+ when Line
59
+ the_points << shape.points.last.to_a
60
+ when QuadraticBezierCurve
61
+ shape.points.each do |point|
62
+ the_points << point.to_a
63
+ end
64
+ # when CubicBezierCurve # TODO
65
+ end
66
+ end
67
+ the_points << @shapes.first.to_a if closed?
68
+ the_points
69
+ end
70
+
71
+ def points=(some_points)
72
+ raise "Cannot assign points directly! Must set shapes instead and points are calculated from them automatically."
73
+ end
74
+
75
+ def drawing_types
76
+ the_drawing_shapes = @shapes.map do |shape|
77
+ case shape
78
+ when Point
79
+ :move_to
80
+ when Array
81
+ :move_to
82
+ when Line
83
+ :line_to
84
+ when QuadraticBezierCurve
85
+ :quad_to
86
+ # when CubicBezierCurve # TODO
87
+ end
88
+ end
89
+ the_drawing_shapes << :close if closed?
90
+ the_drawing_shapes
91
+ end
92
+
93
+ def winding_rule=(value)
94
+ raise "Invalid winding rule: #{value}" unless WINDING_RULES.include?(value.to_s.to_sym)
95
+ @winding_rule = value
96
+ end
97
+
98
+ # Checks if path contains point (two-number Array or x, y args)
99
+ # using the Nonzero-Rule (aka Winding Number Algorithm): https://en.wikipedia.org/wiki/Nonzero-rule
100
+ # or using the Even-Odd Rule (aka Ray Casting Algorithm): https://en.wikipedia.org/wiki/Even%E2%80%93odd_rule
101
+ #
102
+ # @param x The X coordinate of the point to test.
103
+ # @param y The Y coordinate of the point to test.
104
+ #
105
+ # @return {@code true} if the point lies within the bound of
106
+ # the path, {@code false} if the point lies outside of the
107
+ # path's bounds.
108
+ def contain?(x_or_point, y = nil)
109
+ x, y = normalize_point(x_or_point, y)
110
+ return unless x && y
111
+ if (x * 0.0 + y * 0.0) == 0.0
112
+ # N * 0.0 is 0.0 only if N is finite.
113
+ # Here we know that both x and y are finite.
114
+ return false if shapes.count < 2
115
+ mask = winding_rule == :wind_non_zero ? -1 : 1
116
+ (point_crossings(x, y).to_i & mask) != 0
117
+ else
118
+ # Either x or y was infinite or NaN.
119
+ # A NaN always produces a negative response to any test
120
+ # and Infinity values cannot be "inside" any path so
121
+ # they should return false as well.
122
+ false
123
+ end
124
+ end
125
+
126
+ # Calculates the number of times the given path
127
+ # crosses the ray extending to the right from (x,y).
128
+ # If the point lies on a part of the path,
129
+ # then no crossings are counted for that intersection.
130
+ # +1 is added for each crossing where the Y coordinate is increasing
131
+ # -1 is added for each crossing where the Y coordinate is decreasing
132
+ # The return value is the sum of all crossings for every segment in
133
+ # the path.
134
+ # The path must start with a PerfectShape::Point (initial location)
135
+ # The caller must check for NaN values.
136
+ # The caller may also reject infinite values as well.
137
+ def point_crossings(x_or_point, y = nil)
138
+ x, y = normalize_point(x_or_point, y)
139
+ return unless x && y
140
+ return BigDecimal('0') if shapes.count == 0
141
+ movx = movy = curx = cury = endx = endy = 0
142
+ coords = points.flatten
143
+ curx = movx = coords[0]
144
+ cury = movy = coords[1]
145
+ crossings = BigDecimal('0')
146
+ ci = BigDecimal('2')
147
+ 1.upto(shapes.count - 1).each do |i|
148
+ case drawing_types[i]
149
+ when :move_to
150
+ if cury != movy
151
+ line = PerfectShape::Line.new(points: [[curx, cury], [movx, movy]])
152
+ crossings += line.point_crossings(x, y)
153
+ end
154
+ movx = curx = coords[ci]
155
+ ci += 1
156
+ movy = cury = coords[ci]
157
+ ci += 1
158
+ when :line_to
159
+ endx = coords[ci]
160
+ ci += 1
161
+ endy = coords[ci]
162
+ ci += 1
163
+ line = PerfectShape::Line.new(points: [[curx, cury], [endx, endy]])
164
+ crossings += line.point_crossings(x, y)
165
+ curx = endx;
166
+ cury = endy;
167
+ when :quad_to
168
+ quad_ctrlx = coords[ci]
169
+ ci += 1
170
+ quad_ctrly = coords[ci]
171
+ ci += 1
172
+ endx = coords[ci]
173
+ ci += 1
174
+ endy = coords[ci]
175
+ ci += 1
176
+ quad = PerfectShape::QuadraticBezierCurve.new(points: [[curx, cury], [quad_ctrlx, quad_ctrly], [endx, endy]])
177
+ crossings += quad.point_crossings(x, y, 0)
178
+ curx = endx;
179
+ cury = endy;
180
+ # when :cubic_to # TODO
181
+ # crossings +=
182
+ # Curve.point_crossings_for_cubic(x, y,
183
+ # curx, cury,
184
+ # coords[ci++],
185
+ # coords[ci++],
186
+ # coords[ci++],
187
+ # coords[ci++],
188
+ # endx = coords[ci++],
189
+ # endy = coords[ci++],
190
+ # 0);
191
+ # curx = endx;
192
+ # cury = endy;
193
+ when :close
194
+ if cury != movy
195
+ line = PerfectShape::Line.new(points: [[curx, cury], [movx, movy]])
196
+ crossings += line.point_crossings(x, y)
197
+ end
198
+ curx = movx
199
+ cury = movy
200
+ end
201
+ end
202
+ if cury != movy
203
+ line = PerfectShape::Line.new(points: [[curx, cury], [movx, movy]])
204
+ crossings += line.point_crossings(x, y)
205
+ end
206
+ crossings
207
+ end
208
+ end
209
+ end
@@ -0,0 +1,88 @@
1
+ # Copyright (c) 2021 Andy Maleh
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.
21
+
22
+ require 'perfect_shape/shape'
23
+ require 'perfect_shape/point_location'
24
+
25
+ module PerfectShape
26
+ # Point class includes point-specific operations like `#==`, `point_distance` and a fuzzy `contain?` matcher
27
+ class Point < Shape
28
+ class << self
29
+ def point_distance(x, y, px, py)
30
+ x = BigDecimal(x.to_s)
31
+ y = BigDecimal(y.to_s)
32
+ px = BigDecimal(px.to_s)
33
+ py = BigDecimal(py.to_s)
34
+ BigDecimal(Math.sqrt((px - x)**2 + (py - y)**2).to_s)
35
+ end
36
+ end
37
+
38
+ include PointLocation
39
+ include Equalizer.new(:x, :y)
40
+
41
+ def initialize(x_or_point = nil, y_arg = nil, x: nil, y: nil)
42
+ if x_or_point.is_a?(Array)
43
+ x, y = x_or_point
44
+ super(x: x, y: y)
45
+ elsif x_or_point && y_arg
46
+ super(x: x_or_point, y: y_arg)
47
+ else
48
+ x ||= 0
49
+ y ||= 0
50
+ super(x: x, y: y)
51
+ end
52
+ end
53
+
54
+ def max_x
55
+ x
56
+ end
57
+
58
+ def max_y
59
+ y
60
+ end
61
+
62
+ # Checks if points match, with distance tolerance (0 by default)
63
+ #
64
+ # @param x The X coordinate of the point to test.
65
+ # @param y The Y coordinate of the point to test.
66
+ # @param distance The distance from point to tolerate (0 by default)
67
+ #
68
+ # @return {@code true} if the point is close enough within distance tolerance,
69
+ # {@code false} if the point is too far.
70
+ def contain?(x_or_point, y = nil, distance: 0)
71
+ x, y = normalize_point(x_or_point, y)
72
+ return unless x && y
73
+ distance = BigDecimal(distance.to_s)
74
+ point_distance(x, y) <= distance
75
+ end
76
+
77
+ def point_distance(x_or_point, y = nil)
78
+ x, y = normalize_point(x_or_point, y)
79
+ return unless x && y
80
+ Point.point_distance(self.x, self.y, x, y)
81
+ end
82
+
83
+ # Convert to pair Array of x,y coordinates
84
+ def to_a
85
+ [self.x, self.y]
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,54 @@
1
+ # Copyright (c) 2021 Andy Maleh
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.
21
+
22
+ module PerfectShape
23
+ # Point location usually represents the top-left point in a shape
24
+ module PointLocation
25
+ attr_reader :x, :y
26
+
27
+ # Calls super before setting x,y (default: 0,0)
28
+ def initialize(x: 0, y: 0)
29
+ super()
30
+ self.x = x
31
+ self.y = y
32
+ end
33
+
34
+ # Sets x, normalizing to BigDecimal
35
+ def x=(value)
36
+ @x = BigDecimal(value.to_s)
37
+ end
38
+
39
+ # Sets y, normalizing to BigDecimal
40
+ def y=(value)
41
+ @y = BigDecimal(value.to_s)
42
+ end
43
+
44
+ # Returns x by default. Subclasses may override.
45
+ def min_x
46
+ x
47
+ end
48
+
49
+ # Returns y by default. Subclasses may override.
50
+ def min_y
51
+ y
52
+ end
53
+ end
54
+ end
@@ -26,8 +26,9 @@ module PerfectShape
26
26
  # Mostly ported from java.awt.geom: https://docs.oracle.com/javase/8/docs/api/java/awt/Polygon.html
27
27
  class Polygon < Shape
28
28
  include MultiPoint
29
+ include Equalizer.new(:points)
29
30
 
30
- # Checks if polygon contains point denoted by point (two-number Array or x, y args)
31
+ # Checks if polygon contains point (two-number Array or x, y args)
31
32
  # using the Ray Casting Algorithm (aka Even-Odd Rule): https://en.wikipedia.org/wiki/Point_in_polygon
32
33
  #
33
34
  # @param x The X coordinate of the point to test.
@@ -0,0 +1,182 @@
1
+ # Copyright (c) 2021 Andy Maleh
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.
21
+
22
+ require 'perfect_shape/shape'
23
+ require 'perfect_shape/multi_point'
24
+
25
+ module PerfectShape
26
+ # Mostly ported from java.awt.geom: https://docs.oracle.com/javase/8/docs/api/java/awt/geom/QuadCurve2D.html
27
+ class QuadraticBezierCurve < Shape
28
+ class << self
29
+ def point_crossings(x1, y1, xc, yc, x2, y2, px, py, level = 0)
30
+ return BigDecimal('0') if (py < y1 && py < yc && py < y2)
31
+ return BigDecimal('0') if (py >= y1 && py >= yc && py >= y2)
32
+ # Note y1 could equal y2...
33
+ return BigDecimal('0') if (px >= x1 && px >= xc && px >= x2)
34
+ if (px < x1 && px < xc && px < x2)
35
+ if (py >= y1)
36
+ return BigDecimal('1') if (py < y2)
37
+ else
38
+ # py < y1
39
+ return BigDecimal('-1') if (py >= y2)
40
+ end
41
+ # py outside of y11 range, and/or y1==y2
42
+ return BigDecimal('0')
43
+ end
44
+ # double precision only has 52 bits of mantissa
45
+ return PerfectShape::Line.point_crossings(x1, y1, x2, y2, px, py) if (level > 52)
46
+ x1c = (x1 + xc) / BigDecimal('2')
47
+ y1c = (y1 + yc) / BigDecimal('2')
48
+ xc1 = (xc + x2) / BigDecimal('2')
49
+ yc1 = (yc + y2) / BigDecimal('2')
50
+ xc = (x1c + xc1) / BigDecimal('2')
51
+ yc = (y1c + yc1) / BigDecimal('2')
52
+ # [xy]c are NaN if any of [xy]0c or [xy]c1 are NaN
53
+ # [xy]0c or [xy]c1 are NaN if any of [xy][0c1] are NaN
54
+ # These values are also NaN if opposing infinities are added
55
+ return BigDecimal('0') if (xc.nan? || yc.nan?)
56
+ point_crossings(x1, y1, x1c, y1c, xc, yc, px, py, level+1) +
57
+ point_crossings(xc, yc, xc1, yc1, x2, y2, px, py, level+1);
58
+ end
59
+ end
60
+
61
+ include MultiPoint
62
+ include Equalizer.new(:points)
63
+
64
+ # Checks if quadratic bézier curve contains point (two-number Array or x, y args)
65
+ #
66
+ # @param x The X coordinate of the point to test.
67
+ # @param y The Y coordinate of the point to test.
68
+ #
69
+ # @return {@code true} if the point lies within the bound of
70
+ # the quadratic bézier curve, {@code false} if the point lies outside of the
71
+ # quadratic bézier curve's bounds.
72
+ def contain?(x_or_point, y = nil, distance: 0)
73
+ x, y = normalize_point(x_or_point, y)
74
+ return unless x && y
75
+
76
+ x1 = points[0][0]
77
+ y1 = points[0][1]
78
+ xc = points[1][0]
79
+ yc = points[1][1]
80
+ x2 = points[2][0]
81
+ y2 = points[2][1]
82
+
83
+ # We have a convex shape bounded by quad curve Pc(t)
84
+ # and ine Pl(t).
85
+ #
86
+ # P1 = (x1, y1) - start point of curve
87
+ # P2 = (x2, y2) - end point of curve
88
+ # Pc = (xc, yc) - control point
89
+ #
90
+ # Pq(t) = P1*(1 - t)^2 + 2*Pc*t*(1 - t) + P2*t^2 =
91
+ # = (P1 - 2*Pc + P2)*t^2 + 2*(Pc - P1)*t + P1
92
+ # Pl(t) = P1*(1 - t) + P2*t
93
+ # t = [0:1]
94
+ #
95
+ # P = (x, y) - point of interest
96
+ #
97
+ # Let's look at second derivative of quad curve equation:
98
+ #
99
+ # Pq''(t) = 2 * (P1 - 2 * Pc + P2) = Pq''
100
+ # It's constant vector.
101
+ #
102
+ # Let's draw a line through P to be parallel to this
103
+ # vector and find the intersection of the quad curve
104
+ # and the line.
105
+ #
106
+ # Pq(t) is point of intersection if system of equations
107
+ # below has the solution.
108
+ #
109
+ # L(s) = P + Pq''*s == Pq(t)
110
+ # Pq''*s + (P - Pq(t)) == 0
111
+ #
112
+ # | xq''*s + (x - xq(t)) == 0
113
+ # | yq''*s + (y - yq(t)) == 0
114
+ #
115
+ # This system has the solution if rank of its matrix equals to 1.
116
+ # That is, determinant of the matrix should be zero.
117
+ #
118
+ # (y - yq(t))*xq'' == (x - xq(t))*yq''
119
+ #
120
+ # Let's solve this equation with 't' variable.
121
+ # Also let kx = x1 - 2*xc + x2
122
+ # ky = y1 - 2*yc + y2
123
+ #
124
+ # t0q = (1/2)*((x - x1)*ky - (y - y1)*kx) /
125
+ # ((xc - x1)*ky - (yc - y1)*kx)
126
+ #
127
+ # Let's do the same for our line Pl(t):
128
+ #
129
+ # t0l = ((x - x1)*ky - (y - y1)*kx) /
130
+ # ((x2 - x1)*ky - (y2 - y1)*kx)
131
+ #
132
+ # It's easy to check that t0q == t0l. This fact means
133
+ # we can compute t0 only one time.
134
+ #
135
+ # In case t0 < 0 or t0 > 1, we have an intersections outside
136
+ # of shape bounds. So, P is definitely out of shape.
137
+ #
138
+ # In case t0 is inside [0:1], we should calculate Pq(t0)
139
+ # and Pl(t0). We have three points for now, and all of them
140
+ # lie on one line. So, we just need to detect, is our point
141
+ # of interest between points of intersections or not.
142
+ #
143
+ # If the denominator in the t0q and t0l equations is
144
+ # zero, then the points must be collinear and so the
145
+ # curve is degenerate and encloses no area. Thus the
146
+ # result is false.
147
+ kx = x1 - 2 * xc + x2;
148
+ ky = y1 - 2 * yc + y2;
149
+ dx = x - x1;
150
+ dy = y - y1;
151
+ dxl = x2 - x1;
152
+ dyl = y2 - y1;
153
+
154
+ t0 = (dx * ky - dy * kx) / (dxl * ky - dyl * kx)
155
+ return false if (t0 < 0 || t0 > 1 || t0 != t0)
156
+
157
+ xb = kx * t0 * t0 + 2 * (xc - x1) * t0 + x1;
158
+ yb = ky * t0 * t0 + 2 * (yc - y1) * t0 + y1;
159
+ xl = dxl * t0 + x1;
160
+ yl = dyl * t0 + y1;
161
+
162
+ (x >= xb && x < xl) ||
163
+ (x >= xl && x < xb) ||
164
+ (y >= yb && y < yl) ||
165
+ (y >= yl && y < yb)
166
+ end
167
+
168
+ # Calculates the number of times the quad
169
+ # crosses the ray extending to the right from (x,y).
170
+ # If the point lies on a part of the curve,
171
+ # then no crossings are counted for that intersection.
172
+ # the level parameter should be 0 at the top-level call and will count
173
+ # up for each recursion level to prevent infinite recursion
174
+ # +1 is added for each crossing where the Y coordinate is increasing
175
+ # -1 is added for each crossing where the Y coordinate is decreasing
176
+ def point_crossings(x_or_point, y = nil, level = 0)
177
+ x, y = normalize_point(x_or_point, y)
178
+ return unless x && y
179
+ QuadraticBezierCurve.point_crossings(points[0][0], points[0][1], points[1][0], points[1][1], points[2][0], points[2][1], x, y, level)
180
+ end
181
+ end
182
+ end
@@ -28,7 +28,7 @@ module PerfectShape
28
28
  include RectangularShape
29
29
  include Equalizer.new(:x, :y, :width, :height)
30
30
 
31
- # Checks if rectangle contains point denoted by point (two-number Array or x, y args)
31
+ # Checks if rectangle contains point (two-number Array or x, y args)
32
32
  #
33
33
  # @param x The X coordinate of the point to test.
34
34
  # @param y The Y coordinate of the point to test.
@@ -19,31 +19,23 @@
19
19
  # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
20
  # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
21
 
22
+ require 'perfect_shape/point_location'
23
+
22
24
  module PerfectShape
23
25
  # Mixin Module for Rectangular Shapes (having x, y, width, height)
24
26
  # Can only be mixed into a class extending Shape or another module
25
27
  module RectangularShape
26
- attr_reader :x, :y, :width, :height
28
+ include PointLocation
29
+
30
+ attr_reader :width, :height
27
31
 
28
32
  # Calls super before setting x, y, width, height
29
33
  def initialize(x: 0, y: 0, width: 1, height: 1)
30
- super()
31
- self.x = x
32
- self.y = y
34
+ super(x: x, y: y)
33
35
  self.width = width
34
36
  self.height = height
35
37
  end
36
38
 
37
- # Sets x, normalizing to BigDecimal
38
- def x=(value)
39
- @x = BigDecimal(value.to_s)
40
- end
41
-
42
- # Sets y, normalizing to BigDecimal
43
- def y=(value)
44
- @y = BigDecimal(value.to_s)
45
- end
46
-
47
39
  # Sets width, normalizing to BigDecimal
48
40
  def width=(value)
49
41
  @width = BigDecimal(value.to_s)
@@ -54,14 +46,6 @@ module PerfectShape
54
46
  @height = BigDecimal(value.to_s)
55
47
  end
56
48
 
57
- def min_x
58
- @x
59
- end
60
-
61
- def min_y
62
- @y
63
- end
64
-
65
49
  def max_x
66
50
  @x + width if @x && width
67
51
  end
@@ -20,7 +20,8 @@
20
20
  # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
21
 
22
22
  module PerfectShape
23
- # Superclass of all shapes
23
+ # Superclass of all shapes. Not meant to be used directly.
24
+ # Subclasses must implement/override methods as needed.
24
25
  class Shape
25
26
  # Subclasses must implement
26
27
  def min_x
@@ -38,12 +39,16 @@ module PerfectShape
38
39
  def max_y
39
40
  end
40
41
 
41
- # Subclasses must implement
42
+ # Default implementation is max_x - min_x
43
+ # Subclasses can override
42
44
  def width
45
+ max_x - min_x if max_x && min_x
43
46
  end
44
47
 
45
- # Subclasses must implement
48
+ # Default implementation is max_y - min_y
49
+ # Subclasses can override
46
50
  def height
51
+ max_y - min_y if max_y && min_y
47
52
  end
48
53
 
49
54
  # center_x is min_x + width/2.0 by default
@@ -78,5 +83,9 @@ module PerfectShape
78
83
  y = BigDecimal(y.to_s)
79
84
  [x, y]
80
85
  end
86
+
87
+ # Subclasses must implement
88
+ def ==(other)
89
+ end
81
90
  end
82
91
  end
@@ -2,17 +2,17 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Juwelier::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: perfect-shape 0.0.9 ruby lib
5
+ # stub: perfect-shape 0.1.1 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "perfect-shape".freeze
9
- s.version = "0.0.9"
9
+ s.version = "0.1.1"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib".freeze]
13
13
  s.authors = ["Andy Maleh".freeze]
14
- s.date = "2021-12-20"
15
- s.description = "Perfect Shape is a collection of pure Ruby geometric algorithms that are mostly useful for GUI manipulation like checking containment of a mouse click point in popular geometry shapes such as rectangle, square, arc (open, chord, and pie), ellipse, circle, polygon (Ray Casting Algorithm aka Even-Odd Rule), polyline, polyquad, polycubic, and paths containing lines, bezier curves, and quadratic curves. Additionally, it contains some purely mathematical algorithms like IEEEremainder (also known as IEEE-754 remainder).".freeze
14
+ s.date = "2021-12-22"
15
+ s.description = "Perfect Shape is a collection of pure Ruby geometric algorithms that are mostly useful for GUI manipulation like checking containment of a mouse click point in popular geometry shapes such as rectangle, square, arc (open, chord, and pie), ellipse, circle, polygon, and paths containing lines, quadratic b\u00E9zier curves, and cubic b\u00E9zier curves (including both Ray Casting Algorithm, aka Even-odd Rule, and Winding Number Algorithm, aka Nonzero Rule). Additionally, it contains some purely mathematical algorithms like IEEEremainder (also known as IEEE-754 remainder).".freeze
16
16
  s.email = "andy.am@gmail.com".freeze
17
17
  s.extra_rdoc_files = [
18
18
  "CHANGELOG.md",
@@ -31,7 +31,11 @@ Gem::Specification.new do |s|
31
31
  "lib/perfect_shape/line.rb",
32
32
  "lib/perfect_shape/math.rb",
33
33
  "lib/perfect_shape/multi_point.rb",
34
+ "lib/perfect_shape/path.rb",
35
+ "lib/perfect_shape/point.rb",
36
+ "lib/perfect_shape/point_location.rb",
34
37
  "lib/perfect_shape/polygon.rb",
38
+ "lib/perfect_shape/quadratic_bezier_curve.rb",
35
39
  "lib/perfect_shape/rectangle.rb",
36
40
  "lib/perfect_shape/rectangular_shape.rb",
37
41
  "lib/perfect_shape/shape.rb",
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: perfect-shape
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Maleh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-20 00:00:00.000000000 Z
11
+ date: 2021-12-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: equalizer
@@ -97,10 +97,10 @@ dependencies:
97
97
  description: Perfect Shape is a collection of pure Ruby geometric algorithms that
98
98
  are mostly useful for GUI manipulation like checking containment of a mouse click
99
99
  point in popular geometry shapes such as rectangle, square, arc (open, chord, and
100
- pie), ellipse, circle, polygon (Ray Casting Algorithm aka Even-Odd Rule), polyline,
101
- polyquad, polycubic, and paths containing lines, bezier curves, and quadratic curves.
102
- Additionally, it contains some purely mathematical algorithms like IEEEremainder
103
- (also known as IEEE-754 remainder).
100
+ pie), ellipse, circle, polygon, and paths containing lines, quadratic bézier curves,
101
+ and cubic bézier curves (including both Ray Casting Algorithm, aka Even-odd Rule,
102
+ and Winding Number Algorithm, aka Nonzero Rule). Additionally, it contains some
103
+ purely mathematical algorithms like IEEEremainder (also known as IEEE-754 remainder).
104
104
  email: andy.am@gmail.com
105
105
  executables: []
106
106
  extensions: []
@@ -120,7 +120,11 @@ files:
120
120
  - lib/perfect_shape/line.rb
121
121
  - lib/perfect_shape/math.rb
122
122
  - lib/perfect_shape/multi_point.rb
123
+ - lib/perfect_shape/path.rb
124
+ - lib/perfect_shape/point.rb
125
+ - lib/perfect_shape/point_location.rb
123
126
  - lib/perfect_shape/polygon.rb
127
+ - lib/perfect_shape/quadratic_bezier_curve.rb
124
128
  - lib/perfect_shape/rectangle.rb
125
129
  - lib/perfect_shape/rectangular_shape.rb
126
130
  - lib/perfect_shape/shape.rb