perfect-shape 0.0.11 → 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 +9 -0
- data/README.md +43 -10
- data/VERSION +1 -1
- data/lib/perfect_shape/line.rb +27 -0
- data/lib/perfect_shape/math.rb +2 -2
- data/lib/perfect_shape/path.rb +201 -0
- data/lib/perfect_shape/point.rb +6 -0
- data/lib/perfect_shape/point_location.rb +1 -0
- data/perfect-shape.gemspec +5 -4
- metadata +8 -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,5 +1,12 @@
|
|
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
|
+
|
3
10
|
## 0.0.11
|
4
11
|
|
5
12
|
- `PerfectShape::Polygon#==`
|
@@ -27,6 +34,8 @@
|
|
27
34
|
|
28
35
|
## 0.0.7
|
29
36
|
|
37
|
+
- `PerfectShape::Polygon`
|
38
|
+
- `PerfectShape::Polygon#contain?(x_or_point, y)` (Ray Casting Algorithm, aka Even-Odd Rule)
|
30
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`
|
31
40
|
|
32
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:
|
@@ -54,7 +55,7 @@ This is a base class for all shapes. It is not meant to be used directly. Subcla
|
|
54
55
|
- `#center_x`: center x
|
55
56
|
- `#center_y`: center y
|
56
57
|
- `#bounding_box`: bounding box is a rectangle with x = min x, y = min y, and width/height just as those of shape
|
57
|
-
- `#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
|
58
59
|
- `#==(other)`: Returns `true` if equal to `other` or `false` otherwise
|
59
60
|
|
60
61
|
### `PerfectShape::PointLocation`
|
@@ -96,7 +97,7 @@ Includes `PerfectShape::PointLocation`
|
|
96
97
|
|
97
98
|
![point](https://raw.githubusercontent.com/AndyObtiva/perfect-shape/master/images/point.png)
|
98
99
|
|
99
|
-
Points are simply represented by an `Array` of
|
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.
|
100
101
|
|
101
102
|
- `::point_distance(x, y, px, py)`: Returns the distance from a point to another point
|
102
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.
|
@@ -119,12 +120,14 @@ Class
|
|
119
120
|
|
120
121
|
Extends `PerfectShape::Shape`
|
121
122
|
|
123
|
+
Includes `PerfectShape::MultiPoint`
|
124
|
+
|
122
125
|
![line](https://raw.githubusercontent.com/AndyObtiva/perfect-shape/master/images/line.png)
|
123
126
|
|
124
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)”.
|
125
128
|
- `::point_segment_distance_square(x1, y1, x2, y2, px, py)`: Returns the square of distance from a point to a line segment.
|
126
129
|
- `::point_segment_distance(x1, y1, x2, y2, px, py)`: Returns the distance from a point to a line segment.
|
127
|
-
- `::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
|
128
131
|
- `#min_x`: min x
|
129
132
|
- `#min_y`: min y
|
130
133
|
- `#max_x`: max x
|
@@ -286,9 +289,11 @@ Class
|
|
286
289
|
|
287
290
|
Extends `PerfectShape::Shape`
|
288
291
|
|
292
|
+
Includes `PerfectShape::MultiPoint`
|
293
|
+
|
289
294
|
![polygon](https://raw.githubusercontent.com/AndyObtiva/perfect-shape/master/images/polygon.png)
|
290
295
|
|
291
|
-
- `::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
|
292
297
|
- `#min_x`: min x
|
293
298
|
- `#min_y`: min y
|
294
299
|
- `#max_x`: max x
|
@@ -301,6 +306,34 @@ Extends `PerfectShape::Shape`
|
|
301
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))
|
302
307
|
- `#==(other)`: Returns `true` if equal to `other` or `false` otherwise
|
303
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
|
336
|
+
|
304
337
|
## Process
|
305
338
|
|
306
339
|
[Glimmer Process](https://github.com/AndyObtiva/glimmer/blob/master/PROCESS.md)
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0
|
1
|
+
0.1.0
|
data/lib/perfect_shape/line.rb
CHANGED
@@ -182,6 +182,22 @@ 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
|
@@ -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
|
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
|
data/lib/perfect_shape/point.rb
CHANGED
@@ -23,6 +23,7 @@ require 'perfect_shape/shape'
|
|
23
23
|
require 'perfect_shape/point_location'
|
24
24
|
|
25
25
|
module PerfectShape
|
26
|
+
# Point class includes point-specific operations like `#==`, `point_distance` and a fuzzy `contain?` matcher
|
26
27
|
class Point < Shape
|
27
28
|
class << self
|
28
29
|
def point_distance(x, y, px, py)
|
@@ -78,5 +79,10 @@ module PerfectShape
|
|
78
79
|
return unless x && y
|
79
80
|
Point.point_distance(self.x, self.y, x, y)
|
80
81
|
end
|
82
|
+
|
83
|
+
# Convert to pair Array of x,y coordinates
|
84
|
+
def to_a
|
85
|
+
[self.x, self.y]
|
86
|
+
end
|
81
87
|
end
|
82
88
|
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,7 @@ 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",
|
34
35
|
"lib/perfect_shape/point.rb",
|
35
36
|
"lib/perfect_shape/point_location.rb",
|
36
37
|
"lib/perfect_shape/polygon.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,7 @@ 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
|
123
125
|
- lib/perfect_shape/point.rb
|
124
126
|
- lib/perfect_shape/point_location.rb
|
125
127
|
- lib/perfect_shape/polygon.rb
|