perfect-shape 0.0.8 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +27 -1
- data/README.md +108 -17
- data/VERSION +1 -1
- data/lib/perfect_shape/arc.rb +1 -1
- data/lib/perfect_shape/ellipse.rb +1 -1
- data/lib/perfect_shape/line.rb +35 -7
- data/lib/perfect_shape/math.rb +2 -2
- data/lib/perfect_shape/multi_point.rb +0 -8
- data/lib/perfect_shape/path.rb +201 -0
- data/lib/perfect_shape/point.rb +88 -0
- data/lib/perfect_shape/point_location.rb +54 -0
- data/lib/perfect_shape/polygon.rb +2 -1
- data/lib/perfect_shape/rectangle.rb +1 -1
- data/lib/perfect_shape/rectangular_shape.rb +6 -22
- data/lib/perfect_shape/shape.rb +12 -3
- data/perfect-shape.gemspec +7 -4
- metadata +10 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8bb743eb7a0d935839d671a33b58a30ea8eb65b945ee20e6016690fd0c6864f8
|
4
|
+
data.tar.gz: cd5b26777efcf931ed81a84ec63fee3dcaed5dfa3b878dc39350e6eb206f937e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7adf8c29ae005e8f22c15e460dcf99befd28acef67a5387d950bef1d94781be6fce9cb25b725092892da83a6821b5b59bc699cf5c581af11133d90b4946ed29b
|
7
|
+
data.tar.gz: 334ef034a6f1010f49ad2fa2edc9dfccae994d0f8524427104100b6f4ccc83a63b276f731d483c494625111ad5a668ac05300c9b589ae57b801645d393130c38
|
data/CHANGELOG.md
CHANGED
@@ -1,15 +1,41 @@
|
|
1
1
|
# Change Log
|
2
2
|
|
3
|
+
## 0.1.0
|
4
|
+
|
5
|
+
- `PerfectShape::Path` (having points or lines)
|
6
|
+
- `PerfectShape::Path#contain?(x_or_point, y=nil, distance: 0)`
|
7
|
+
- `PerfectShape::Path#point_crossings(x_or_point, y=nil)`
|
8
|
+
- `PerfectShape::Path#==`
|
9
|
+
|
10
|
+
## 0.0.11
|
11
|
+
|
12
|
+
- `PerfectShape::Polygon#==`
|
13
|
+
- `PerfectShape::Line#==`
|
14
|
+
- `PerfectShape::Point#==`
|
15
|
+
|
16
|
+
## 0.0.10
|
17
|
+
|
18
|
+
- `PerfectShape::Point`
|
19
|
+
- `PerfectShape::Point#point_distance`
|
20
|
+
- `PerfectShape::Point#contain?(x_or_point, y=nil, distance: 0)`
|
21
|
+
- Refactor `PerfectShape::Point`,`PerfectShape::RectangularShape` to include shared `PerfectShape::PointLocation`
|
22
|
+
|
23
|
+
## 0.0.9
|
24
|
+
|
25
|
+
- `PerfectShape::Line#contain?(x_or_point, y=nil, distance: 0)` (add a distance tolerance fuzz factor option)
|
26
|
+
|
3
27
|
## 0.0.8
|
4
28
|
|
5
29
|
- `PerfectShape::Line`
|
6
|
-
- `PerfectShape::Line#contain
|
30
|
+
- `PerfectShape::Line#contain?(x_or_point, y=nil)`
|
7
31
|
- `PerfectShape::Line#relative_counterclockwise`
|
8
32
|
- `PerfectShape::Line#point_segment_distance`
|
9
33
|
- Update `PerfectShape::Math::radians_to_degrees`, `PerfectShape::Math::degrees_to_radians`, and `PerfectShape::Math::normalize_degrees` to normalize numbers to `BigDecimal`
|
10
34
|
|
11
35
|
## 0.0.7
|
12
36
|
|
37
|
+
- `PerfectShape::Polygon`
|
38
|
+
- `PerfectShape::Polygon#contain?(x_or_point, y)` (Ray Casting Algorithm, aka Even-Odd Rule)
|
13
39
|
- `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`
|
14
40
|
|
15
41
|
## 0.0.6
|
data/README.md
CHANGED
@@ -1,25 +1,26 @@
|
|
1
|
-
# Perfect Shape 0.0
|
1
|
+
# Perfect Shape 0.1.0
|
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
|
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), polyline, polyquad, polycubic, and [paths](#perfectshapepath) containing [lines](#perfectshapeline), quadratic bézier curves, 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
|
17
|
+
gem install perfect-shape -v 0.1.0
|
17
18
|
```
|
18
19
|
|
19
20
|
Or include in Bundler `Gemfile`:
|
20
21
|
|
21
22
|
```ruby
|
22
|
-
gem 'perfect-shape', '~> 0.0
|
23
|
+
gem 'perfect-shape', '~> 0.1.0'
|
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
|
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,17 +87,47 @@ 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
|
+
|
74
117
|
### `PerfectShape::Line`
|
75
118
|
|
76
119
|
Class
|
120
|
+
|
77
121
|
Extends `PerfectShape::Shape`
|
78
122
|
|
79
|
-
|
123
|
+
Includes `PerfectShape::MultiPoint`
|
124
|
+
|
125
|
+
![line](https://raw.githubusercontent.com/AndyObtiva/perfect-shape/master/images/line.png)
|
80
126
|
|
81
127
|
- `::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)”.
|
82
128
|
- `::point_segment_distance_square(x1, y1, x2, y2, px, py)`: Returns the square of distance from a point to a line segment.
|
83
129
|
- `::point_segment_distance(x1, y1, x2, y2, px, py)`: Returns the distance from a point to a line segment.
|
84
|
-
- `::new(points: nil)`: constructs a polygon with `points` as `Array` of `Array`s of
|
130
|
+
- `::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
|
85
131
|
- `#min_x`: min x
|
86
132
|
- `#min_y`: min y
|
87
133
|
- `#max_x`: max x
|
@@ -91,17 +137,20 @@ Extends `PerfectShape::Shape`
|
|
91
137
|
- `#center_x`: center x
|
92
138
|
- `#center_y`: center y
|
93
139
|
- `#bounding_box`: bounding box is a rectangle with x = min x, y = min y, and width/height of shape
|
94
|
-
- `#contain?(x_or_point, y=nil)`: checks if point lies on line
|
140
|
+
- `#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.
|
95
141
|
- `#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)”.
|
96
142
|
- `#point_segment_distance(x_or_point, y=nil)`: Returns the distance from a point to a line segment.
|
143
|
+
- `#==(other)`: Returns `true` if equal to `other` or `false` otherwise
|
97
144
|
|
98
145
|
### `PerfectShape::Rectangle`
|
99
146
|
|
100
147
|
Class
|
148
|
+
|
101
149
|
Extends `PerfectShape::Shape`
|
150
|
+
|
102
151
|
Includes `PerfectShape::RectangularShape`
|
103
152
|
|
104
|
-
![rectangle](images/rectangle.png)
|
153
|
+
![rectangle](https://raw.githubusercontent.com/AndyObtiva/perfect-shape/master/images/rectangle.png)
|
105
154
|
|
106
155
|
- `::new(x: 0, y: 0, width: 1, height: 1)`: constructs a rectangle
|
107
156
|
- `#x`: top-left x
|
@@ -116,13 +165,15 @@ Includes `PerfectShape::RectangularShape`
|
|
116
165
|
- `#max_y`: max y
|
117
166
|
- `#bounding_box`: bounding box is a rectangle with x = min x, y = min y, and width/height of shape
|
118
167
|
- `#contain?(x_or_point, y=nil)`: checks if point is inside
|
168
|
+
- `#==(other)`: Returns `true` if equal to `other` or `false` otherwise
|
119
169
|
|
120
170
|
### `PerfectShape::Square`
|
121
171
|
|
122
172
|
Class
|
173
|
+
|
123
174
|
Extends `PerfectShape::Rectangle`
|
124
175
|
|
125
|
-
![square](images/square.png)
|
176
|
+
![square](https://raw.githubusercontent.com/AndyObtiva/perfect-shape/master/images/square.png)
|
126
177
|
|
127
178
|
- `::new(x: 0, y: 0, length: 1)`: constructs a square
|
128
179
|
- `#x`: top-left x
|
@@ -138,18 +189,21 @@ Extends `PerfectShape::Rectangle`
|
|
138
189
|
- `#max_y`: max y
|
139
190
|
- `#bounding_box`: bounding box is a rectangle with x = min x, y = min y, and width/height of shape
|
140
191
|
- `#contain?(x_or_point, y=nil)`: checks if point is inside
|
192
|
+
- `#==(other)`: Returns `true` if equal to `other` or `false` otherwise
|
141
193
|
|
142
194
|
### `PerfectShape::Arc`
|
143
195
|
|
144
196
|
Class
|
197
|
+
|
145
198
|
Extends `PerfectShape::Shape`
|
199
|
+
|
146
200
|
Includes `PerfectShape::RectangularShape`
|
147
201
|
|
148
202
|
Arcs can be of type `:open`, `:chord`, or `:pie`
|
149
203
|
|
150
204
|
Open Arc | Chord Arc | Pie Arc
|
151
205
|
---------|-----------|--------
|
152
|
-
![arc-open](images/arc-open.png) | ![arc-chord](images/arc-chord.png) | ![arc-pie](images/arc-pie.png)
|
206
|
+
![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)
|
153
207
|
|
154
208
|
- `::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`
|
155
209
|
- `#type`: `:open`, `:chord`, or `:pie`
|
@@ -169,13 +223,15 @@ Open Arc | Chord Arc | Pie Arc
|
|
169
223
|
- `#max_y`: max y
|
170
224
|
- `#bounding_box`: bounding box is a rectangle with x = min x, y = min y, and width/height of shape
|
171
225
|
- `#contain?(x_or_point, y=nil)`: checks if point is inside
|
226
|
+
- `#==(other)`: Returns `true` if equal to `other` or `false` otherwise
|
172
227
|
|
173
228
|
### `PerfectShape::Ellipse`
|
174
229
|
|
175
230
|
Class
|
231
|
+
|
176
232
|
Extends `PerfectShape::Arc`
|
177
233
|
|
178
|
-
![ellipse](images/ellipse.png)
|
234
|
+
![ellipse](https://raw.githubusercontent.com/AndyObtiva/perfect-shape/master/images/ellipse.png)
|
179
235
|
|
180
236
|
- `::new(x: 0, y: 0, width: 1, height: 1, center_x: nil, center_y: nil, radius_x: nil, radius_y: nil)`: constructs an ellipse
|
181
237
|
- `#x`: top-left x
|
@@ -195,13 +251,15 @@ Extends `PerfectShape::Arc`
|
|
195
251
|
- `#max_y`: max y
|
196
252
|
- `#bounding_box`: bounding box is a rectangle with x = min x, y = min y, and width/height of shape
|
197
253
|
- `#contain?(x_or_point, y=nil)`: checks if point is inside
|
254
|
+
- `#==(other)`: Returns `true` if equal to `other` or `false` otherwise
|
198
255
|
|
199
256
|
### `PerfectShape::Circle`
|
200
257
|
|
201
258
|
Class
|
259
|
+
|
202
260
|
Extends `PerfectShape::Ellipse`
|
203
261
|
|
204
|
-
![circle](images/circle.png)
|
262
|
+
![circle](https://raw.githubusercontent.com/AndyObtiva/perfect-shape/master/images/circle.png)
|
205
263
|
|
206
264
|
- `::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
|
207
265
|
- `#x`: top-left x
|
@@ -223,15 +281,19 @@ Extends `PerfectShape::Ellipse`
|
|
223
281
|
- `#max_y`: max y
|
224
282
|
- `#bounding_box`: bounding box is a rectangle with x = min x, y = min y, and width/height of shape
|
225
283
|
- `#contain?(x_or_point, y=nil)`: checks if point is inside
|
284
|
+
- `#==(other)`: Returns `true` if equal to `other` or `false` otherwise
|
226
285
|
|
227
286
|
### `PerfectShape::Polygon`
|
228
287
|
|
229
288
|
Class
|
289
|
+
|
230
290
|
Extends `PerfectShape::Shape`
|
231
291
|
|
232
|
-
|
292
|
+
Includes `PerfectShape::MultiPoint`
|
293
|
+
|
294
|
+
![polygon](https://raw.githubusercontent.com/AndyObtiva/perfect-shape/master/images/polygon.png)
|
233
295
|
|
234
|
-
- `::new(points: nil)`: constructs a polygon with `points` as `Array` of `Array`s of
|
296
|
+
- `::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
|
235
297
|
- `#min_x`: min x
|
236
298
|
- `#min_y`: min y
|
237
299
|
- `#max_x`: max x
|
@@ -242,6 +304,35 @@ Extends `PerfectShape::Shape`
|
|
242
304
|
- `#center_y`: center y
|
243
305
|
- `#bounding_box`: bounding box is a rectangle with x = min x, y = min y, and width/height of shape
|
244
306
|
- `#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))
|
307
|
+
- `#==(other)`: Returns `true` if equal to `other` or `false` otherwise
|
308
|
+
|
309
|
+
### `PerfectShape::Path`
|
310
|
+
|
311
|
+
Class
|
312
|
+
|
313
|
+
Extends `PerfectShape::Shape`
|
314
|
+
|
315
|
+
Includes `PerfectShape::MultiPoint`
|
316
|
+
|
317
|
+
![path](https://raw.githubusercontent.com/AndyObtiva/perfect-shape/master/images/path.png)
|
318
|
+
|
319
|
+
- `::new(shapes: nil, 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), or `PerfectShape::Line`. 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`.
|
320
|
+
- `#shapes`: the shapes that the path is composed of
|
321
|
+
- `#closed?`: returns `true` if closed and `false` otherwise
|
322
|
+
- `#winding_rule`: returns winding rule (`:wind_non_zero` or `:wind_even_odd`)
|
323
|
+
- `#points`: path points calculated (derived) from shapes
|
324
|
+
- `#min_x`: min x
|
325
|
+
- `#min_y`: min y
|
326
|
+
- `#max_x`: max x
|
327
|
+
- `#max_y`: max y
|
328
|
+
- `#width`: width (from min x to max x)
|
329
|
+
- `#height`: height (from min y to max y)
|
330
|
+
- `#center_x`: center x
|
331
|
+
- `#center_y`: center y
|
332
|
+
- `#bounding_box`: bounding box is a rectangle with x = min x, y = min y, and width/height of shape
|
333
|
+
- `#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))
|
334
|
+
- `#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)
|
335
|
+
- `#==(other)`: Returns `true` if equal to `other` or `false` otherwise
|
245
336
|
|
246
337
|
## Process
|
247
338
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0
|
1
|
+
0.1.0
|
data/lib/perfect_shape/arc.rb
CHANGED
@@ -135,7 +135,7 @@ module PerfectShape
|
|
135
135
|
@height = nil
|
136
136
|
end
|
137
137
|
|
138
|
-
# Checks if arc contains point
|
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
|
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.
|
data/lib/perfect_shape/line.rb
CHANGED
@@ -182,24 +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 0 if (py < y1 && py < y2)
|
193
|
+
return 0 if (py >= y1 && py >= y2)
|
194
|
+
# assert(y1 != y2);
|
195
|
+
return 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 0 if (px >= xintercept)
|
199
|
+
(y1 < y2) ? 1 : -1
|
200
|
+
end
|
185
201
|
end
|
186
202
|
|
187
203
|
include MultiPoint
|
204
|
+
include Equalizer.new(:points)
|
188
205
|
|
189
|
-
# Checks if
|
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
|
197
|
-
#
|
198
|
-
def contain?(x_or_point, y = nil)
|
213
|
+
# the line, {@code false} if the point lies outside of the
|
214
|
+
# line's bounds.
|
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
|
-
|
202
|
-
|
218
|
+
distance = BigDecimal(distance.to_s)
|
219
|
+
point_segment_distance(x, y) <= distance
|
203
220
|
end
|
204
221
|
|
205
222
|
def point_segment_distance(x_or_point, y = nil)
|
@@ -213,5 +230,16 @@ module PerfectShape
|
|
213
230
|
return unless x && y
|
214
231
|
Line.relative_counterclockwise(points[0][0], points[0][1], points[1][0], points[1][1], x, y)
|
215
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
|
216
244
|
end
|
217
245
|
end
|
data/lib/perfect_shape/math.rb
CHANGED
@@ -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,
|
79
|
+
def method_missing(method_name, *args, &block)
|
80
80
|
if ::Math.respond_to?(method_name, true)
|
81
|
-
::Math.send(method_name, *args,
|
81
|
+
::Math.send(method_name, *args, &block)
|
82
82
|
else
|
83
83
|
super
|
84
84
|
end
|
@@ -0,0 +1,201 @@
|
|
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/multi_point'
|
26
|
+
|
27
|
+
module PerfectShape
|
28
|
+
# Mostly ported from java.awt.geom: https://docs.oracle.com/javase/8/docs/api/java/awt/geom/Path2D.html
|
29
|
+
class Path < Shape
|
30
|
+
include MultiPoint
|
31
|
+
include Equalizer.new(:shapes, :closed, :winding_rule)
|
32
|
+
|
33
|
+
SHAPE_TYPES = [Array, Point, Line]
|
34
|
+
WINDING_RULES = [:wind_non_zero, :wind_even_odd]
|
35
|
+
|
36
|
+
attr_reader :winding_rule
|
37
|
+
attr_accessor :shapes, :closed
|
38
|
+
alias closed? closed
|
39
|
+
|
40
|
+
# Constructs Path with winding rule, closed status, and shapes (must always start with PerfectShape::Point or Array of [x,y] coordinates)
|
41
|
+
# Shape class types can be any of SHAPE_TYPES: Array (x,y coordinates), PerfectShape::Point, or PerfectShape::Line
|
42
|
+
# winding_rule can be any of WINDING_RULES: :wind_non_zero (default) or :wind_even_odd
|
43
|
+
def initialize(shapes: [], closed: false, winding_rule: :wind_non_zero)
|
44
|
+
self.closed = closed
|
45
|
+
self.winding_rule = winding_rule
|
46
|
+
self.shapes = shapes
|
47
|
+
end
|
48
|
+
|
49
|
+
def points
|
50
|
+
@shapes.map do |shape|
|
51
|
+
case shape
|
52
|
+
when Point
|
53
|
+
shape.to_a
|
54
|
+
when Array
|
55
|
+
shape
|
56
|
+
when Line
|
57
|
+
shape.points.last.to_a
|
58
|
+
# when QuadraticBezierCurve # TODO
|
59
|
+
# when CubicBezierCurve # TODO
|
60
|
+
end
|
61
|
+
end.tap do |the_points|
|
62
|
+
the_points << @shapes.first.to_a if closed?
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def points=(some_points)
|
67
|
+
raise "Cannot assign points directly! Must set shapes instead and points are calculated from them automatically."
|
68
|
+
end
|
69
|
+
|
70
|
+
def drawing_types
|
71
|
+
the_drawing_shapes = @shapes.map do |shape|
|
72
|
+
case shape
|
73
|
+
when Point
|
74
|
+
:move_to
|
75
|
+
when Array
|
76
|
+
:move_to
|
77
|
+
when Line
|
78
|
+
:line_to
|
79
|
+
# when QuadraticBezierCurve # TODO
|
80
|
+
# when CubicBezierCurve # TODO
|
81
|
+
end
|
82
|
+
end
|
83
|
+
the_drawing_shapes << :close if closed?
|
84
|
+
the_drawing_shapes
|
85
|
+
end
|
86
|
+
|
87
|
+
def winding_rule=(value)
|
88
|
+
raise "Invalid winding rule: #{value}" unless WINDING_RULES.include?(value.to_s.to_sym)
|
89
|
+
@winding_rule = value
|
90
|
+
end
|
91
|
+
|
92
|
+
# Checks if path contains point (two-number Array or x, y args)
|
93
|
+
# using the Nonzero-Rule (aka Winding Number Algorithm): https://en.wikipedia.org/wiki/Nonzero-rule
|
94
|
+
# or using the Even-Odd Rule (aka Ray Casting Algorithm): https://en.wikipedia.org/wiki/Even%E2%80%93odd_rule
|
95
|
+
#
|
96
|
+
# @param x The X coordinate of the point to test.
|
97
|
+
# @param y The Y coordinate of the point to test.
|
98
|
+
#
|
99
|
+
# @return {@code true} if the point lies within the bound of
|
100
|
+
# the path, {@code false} if the point lies outside of the
|
101
|
+
# path's bounds.
|
102
|
+
def contain?(x_or_point, y = nil)
|
103
|
+
x, y = normalize_point(x_or_point, y)
|
104
|
+
return unless x && y
|
105
|
+
if (x * 0.0 + y * 0.0) == 0.0
|
106
|
+
# N * 0.0 is 0.0 only if N is finite.
|
107
|
+
# Here we know that both x and y are finite.
|
108
|
+
return false if shapes.count < 2
|
109
|
+
mask = winding_rule == :wind_non_zero ? -1 : 1
|
110
|
+
(point_crossings(x, y) & mask) != 0
|
111
|
+
else
|
112
|
+
# Either x or y was infinite or NaN.
|
113
|
+
# A NaN always produces a negative response to any test
|
114
|
+
# and Infinity values cannot be "inside" any path so
|
115
|
+
# they should return false as well.
|
116
|
+
false
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
# Calculates the number of times the given path
|
121
|
+
# crosses the ray extending to the right from (x,y).
|
122
|
+
# If the point lies on a part of the path,
|
123
|
+
# then no crossings are counted for that intersection.
|
124
|
+
# +1 is added for each crossing where the Y coordinate is increasing
|
125
|
+
# -1 is added for each crossing where the Y coordinate is decreasing
|
126
|
+
# The return value is the sum of all crossings for every segment in
|
127
|
+
# the path.
|
128
|
+
# The path must start with a PerfectShape::Point (initial location)
|
129
|
+
# The caller must check for NaN values.
|
130
|
+
# The caller may also reject infinite values as well.
|
131
|
+
def point_crossings(x_or_point, y = nil)
|
132
|
+
x, y = normalize_point(x_or_point, y)
|
133
|
+
return unless x && y
|
134
|
+
return 0 if shapes.count == 0
|
135
|
+
movx = movy = curx = cury = endx = endy = 0
|
136
|
+
coords = points.flatten
|
137
|
+
curx = movx = coords[0]
|
138
|
+
cury = movy = coords[1]
|
139
|
+
crossings = 0
|
140
|
+
ci = 2
|
141
|
+
1.upto(shapes.count - 1).each do |i|
|
142
|
+
case drawing_types[i]
|
143
|
+
when :move_to
|
144
|
+
if cury != movy
|
145
|
+
line = PerfectShape::Line.new(points: [[curx, cury], [movx, movy]])
|
146
|
+
crossings += line.point_crossings(x, y)
|
147
|
+
end
|
148
|
+
movx = curx = coords[ci]
|
149
|
+
ci += 1
|
150
|
+
movy = cury = coords[ci]
|
151
|
+
ci += 1
|
152
|
+
when :line_to
|
153
|
+
endx = coords[ci]
|
154
|
+
ci += 1
|
155
|
+
endy = coords[ci]
|
156
|
+
ci += 1
|
157
|
+
line = PerfectShape::Line.new(points: [[curx, cury], [endx, endy]])
|
158
|
+
crossings += line.point_crossings(x, y)
|
159
|
+
curx = endx;
|
160
|
+
cury = endy;
|
161
|
+
# when :quad_to # TODO
|
162
|
+
# crossings +=
|
163
|
+
# Curve.point_crossings_for_quad(x, y,
|
164
|
+
# curx, cury,
|
165
|
+
# coords[ci++],
|
166
|
+
# coords[ci++],
|
167
|
+
# endx = coords[ci++],
|
168
|
+
# endy = coords[ci++],
|
169
|
+
# 0);
|
170
|
+
# curx = endx;
|
171
|
+
# cury = endy;
|
172
|
+
# when :cubic_to # TODO
|
173
|
+
# crossings +=
|
174
|
+
# Curve.point_crossings_for_cubic(x, y,
|
175
|
+
# curx, cury,
|
176
|
+
# coords[ci++],
|
177
|
+
# coords[ci++],
|
178
|
+
# coords[ci++],
|
179
|
+
# coords[ci++],
|
180
|
+
# endx = coords[ci++],
|
181
|
+
# endy = coords[ci++],
|
182
|
+
# 0);
|
183
|
+
# curx = endx;
|
184
|
+
# cury = endy;
|
185
|
+
when :close
|
186
|
+
if cury != movy
|
187
|
+
line = PerfectShape::Line.new(points: [[curx, cury], [movx, movy]])
|
188
|
+
crossings += line.point_crossings(x, y)
|
189
|
+
end
|
190
|
+
curx = movx
|
191
|
+
cury = movy
|
192
|
+
end
|
193
|
+
end
|
194
|
+
if cury != movy
|
195
|
+
line = PerfectShape::Line.new(points: [[curx, cury], [movx, movy]])
|
196
|
+
crossings += line.point_crossings(x, y)
|
197
|
+
end
|
198
|
+
crossings
|
199
|
+
end
|
200
|
+
end
|
201
|
+
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
|
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.
|
@@ -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
|
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
|
-
|
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
|
data/lib/perfect_shape/shape.rb
CHANGED
@@ -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
|
-
#
|
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
|
-
#
|
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
|
data/perfect-shape.gemspec
CHANGED
@@ -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
|
5
|
+
# stub: perfect-shape 0.1.0 ruby lib
|
6
6
|
|
7
7
|
Gem::Specification.new do |s|
|
8
8
|
s.name = "perfect-shape".freeze
|
9
|
-
s.version = "0.0
|
9
|
+
s.version = "0.1.0"
|
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-
|
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
|
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, polyline, polyquad, polycubic, 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,6 +31,9 @@ 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",
|
35
38
|
"lib/perfect_shape/rectangle.rb",
|
36
39
|
"lib/perfect_shape/rectangular_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
|
4
|
+
version: 0.1.0
|
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-
|
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,11 @@ 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
|
101
|
-
|
102
|
-
|
103
|
-
(also known as
|
100
|
+
pie), ellipse, circle, polygon, polyline, polyquad, polycubic, and paths containing
|
101
|
+
lines, quadratic bézier curves, and cubic bézier curves (including both Ray Casting
|
102
|
+
Algorithm, aka Even-odd Rule, and Winding Number Algorithm, aka Nonzero Rule). Additionally,
|
103
|
+
it contains some purely mathematical algorithms like IEEEremainder (also known as
|
104
|
+
IEEE-754 remainder).
|
104
105
|
email: andy.am@gmail.com
|
105
106
|
executables: []
|
106
107
|
extensions: []
|
@@ -120,6 +121,9 @@ files:
|
|
120
121
|
- lib/perfect_shape/line.rb
|
121
122
|
- lib/perfect_shape/math.rb
|
122
123
|
- lib/perfect_shape/multi_point.rb
|
124
|
+
- lib/perfect_shape/path.rb
|
125
|
+
- lib/perfect_shape/point.rb
|
126
|
+
- lib/perfect_shape/point_location.rb
|
123
127
|
- lib/perfect_shape/polygon.rb
|
124
128
|
- lib/perfect_shape/rectangle.rb
|
125
129
|
- lib/perfect_shape/rectangular_shape.rb
|