perfect-shape 0.3.1 → 0.3.2

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: d9e16e850ee86ff6e5953dd10f2c1c753109906b671a85115ea1913386a3a736
4
- data.tar.gz: a5b1fef3214f92dd3185c1ee1c6e3f0522ff278cda0a0f384341c9e4a5bad5f7
3
+ metadata.gz: 8f9bd6a8a2c7c3d3ec4ec3fd3e5b311a9eec08fa63e35666547534a2e19273c6
4
+ data.tar.gz: 4dbcd6b139e118c555d50681efe4e770cf796774a904459994a64c2bb19d5140
5
5
  SHA512:
6
- metadata.gz: 19c2f3261ef17fa8e048c8e826e56a3f1d07b2ff39a3a8d18832a0b1e2113b63af8ce09aa8a978f6c81a931ffcb8154e0fdc77ed53b0f0937f7d8d6529250fd5
7
- data.tar.gz: 681654b7cac1242f872225a0726f8f2fa5813a8c61d999342466fa06516ccaeb981be68f5fad003eeb0d86a4b58c698f264e94a356a4ee934361482f6a072726
6
+ metadata.gz: e30baa5bb362e5e540af6f67ba9c99ff0f5f0469f48a724823a76522111d6ea86e1bce6cb6b273b056cc9bf7e8f92bd667103aa348abbc237e4ffec082154434
7
+ data.tar.gz: 7627cabd6ee3ef671ed802d6846ad1c192065cc4a5bf6d00062a7d8da7d3376c3d84b836664413a5033644dfa839d19a8634badf851e4aa050e6f03cdef031bc
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Change Log
2
2
 
3
+ ## 0.3.2
4
+
5
+ - Check point containment in cubic bezier curve outline with distance tolerance (new method signature: `PerfectShape::CubicBezierCurve#contain?(x_or_point, y = nil, outline: false, distance_tolerance: 0)`)
6
+ - `PerfectShape::CubicBezierCurve#curve_center_point`, `PerfectShape::CubicBezierCurve#curve_center_x`, `PerfectShape::CubicBezierCurve#curve_center_y`
7
+ - `PerfectShape::CubicBezierCurve#subdivisions(number=2)`
8
+ - `PerfectShape::CubicBezierCurve#point_segment_distance(x_or_point, y = nil, minimum_distance_threshold: OUTLINE_MINIMUM_DISTANCE_THRESHOLD)`
9
+
3
10
  ## 0.3.1
4
11
 
5
12
  - Check point containment in arc outline with distance tolerance (new method signature: `PerfectShape::Arc#contain?(x_or_point, y = nil, outline: false, distance_tolerance: 0)`)
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Perfect Shape 0.3.1
1
+ # Perfect Shape 0.3.2
2
2
  ## Geometric Algorithms
3
3
  [![Gem Version](https://badge.fury.io/rb/perfect-shape.svg)](http://badge.fury.io/rb/perfect-shape)
4
4
  [![Test](https://github.com/AndyObtiva/perfect-shape/actions/workflows/ruby.yml/badge.svg)](https://github.com/AndyObtiva/perfect-shape/actions/workflows/ruby.yml)
@@ -14,13 +14,13 @@ To ensure high accuracy, this library does all its mathematical operations with
14
14
  Run:
15
15
 
16
16
  ```
17
- gem install perfect-shape -v 0.3.1
17
+ gem install perfect-shape -v 0.3.2
18
18
  ```
19
19
 
20
20
  Or include in Bundler `Gemfile`:
21
21
 
22
22
  ```ruby
23
- gem 'perfect-shape', '~> 0.3.1'
23
+ gem 'perfect-shape', '~> 0.3.2'
24
24
  ```
25
25
 
26
26
  And, run:
@@ -55,9 +55,9 @@ This is a base class for all shapes. It is not meant to be used directly. Subcla
55
55
  - `#center_x`: center x
56
56
  - `#center_y`: center y
57
57
  - `#bounding_box`: bounding box is a rectangle with x = min x, y = min y, and width/height just as those of shape
58
- - `#normalize_point(x_or_point, y = nil)`: normalizes point into an `Array` of `[x,y]` coordinates
59
- - `#contain?(x_or_point, y=nil)`: checks if point is inside
60
58
  - `#==(other)`: Returns `true` if equal to `other` or `false` otherwise
59
+ - `#normalize_point(x_or_point, y = nil)`: normalizes point into an `Array` of `[x,y]` coordinates
60
+ - `#contain?(x_or_point, y=nil, outline: false, distance_tolerance: 0)`: checks if point is inside if `outline` is `false` or if point is on the outline if `outline` is `true`. `distance_tolerance` can be used as a fuzz factor when `outline` is `true`, for example, to help GUI users mouse-click-select a shape from its outline more successfully
61
61
 
62
62
  ### `PerfectShape::PointLocation`
63
63
 
@@ -111,9 +111,9 @@ Points are simply represented by an `Array` of `[x,y]` coordinates when used wit
111
111
  - `#center_x`: center x (always x)
112
112
  - `#center_y`: center y (always y)
113
113
  - `#bounding_box`: bounding box is a rectangle with x = min x, y = min y, and width/height of shape
114
+ - `#==(other)`: Returns `true` if equal to `other` or `false` otherwise
114
115
  - `#contain?(x_or_point, y=nil, distance_tolerance: 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 more successfully.
115
116
  - `#point_distance(x_or_point, y=nil)`: Returns the distance from a point to another point
116
- - `#==(other)`: Returns `true` if equal to `other` or `false` otherwise
117
117
 
118
118
  Example:
119
119
 
@@ -153,10 +153,10 @@ Includes `PerfectShape::MultiPoint`
153
153
  - `#center_x`: center x
154
154
  - `#center_y`: center y
155
155
  - `#bounding_box`: bounding box is a rectangle with x = min x, y = min y, and width/height of shape
156
+ - `#==(other)`: Returns `true` if equal to `other` or `false` otherwise
156
157
  - `#contain?(x_or_point, y=nil, distance_tolerance: 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 more successfully.
157
158
  - `#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)”.
158
159
  - `#point_segment_distance(x_or_point, y=nil)`: Returns the distance from a point to a line segment.
159
- - `#==(other)`: Returns `true` if equal to `other` or `false` otherwise
160
160
 
161
161
  Example:
162
162
 
@@ -194,8 +194,8 @@ Includes `PerfectShape::MultiPoint`
194
194
  - `#center_x`: center x
195
195
  - `#center_y`: center y
196
196
  - `#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)
197
- - `#contain?(x_or_point, y=nil)`: checks if point is inside
198
197
  - `#==(other)`: Returns `true` if equal to `other` or `false` otherwise
198
+ - `#contain?(x_or_point, y=nil)`: checks if point is inside
199
199
 
200
200
  Example:
201
201
 
@@ -229,8 +229,13 @@ Includes `PerfectShape::MultiPoint`
229
229
  - `#center_x`: center x
230
230
  - `#center_y`: center y
231
231
  - `#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)
232
- - `#contain?(x_or_point, y=nil)`: checks if point is inside
233
232
  - `#==(other)`: Returns `true` if equal to `other` or `false` otherwise
233
+ - `#contain?(x_or_point, y=nil, outline: false, distance_tolerance: 0)`: checks if point is inside when `outline` is `false` or if point is on the outline when `outline` is `true`. `distance_tolerance` can be used as a fuzz factor when `outline` is `true`, for example, to help GUI users mouse-click-select a cubic bezier curve shape from its outline more successfully
234
+ - `#curve_center_point`: point at the center of the curve (not the center of the bounding box area like `center_x` and `center_y`)
235
+ - `#curve_center_x`: point x coordinate at the center of the curve (not the center of the bounding box area like `center_x` and `center_y`)
236
+ - `#curve_center_y`: point y coordinate at the center of the curve (not the center of the bounding box area like `center_x` and `center_y`)
237
+ - `#subdivisions(number=2)`: subdivides cubic bezier curve at its center into into 2 cubic bezier curves by default, or more if `number` is specified. `number` must be an even number, or it will be rounded up to the closest even number.
238
+ - `#point_segment_distance(x_or_point, y=nil, minimum_distance_threshold: OUTLINE_MINIMUM_DISTANCE_THRESHOLD)`: calculates distance from point to curve segment. It does so by subdividing curve into smaller curves and checking against the curve center points until the distance is less than `minimum_distance_threshold`, to avoid being an overly costly operation.
234
239
 
235
240
  Example:
236
241
 
@@ -241,6 +246,14 @@ shape = PerfectShape::CubicBezierCurve.new(points: [[200, 150], [235, 235], [270
241
246
 
242
247
  shape.contain?(270, 220) # => true
243
248
  shape.contain?([270, 220]) # => true
249
+ shape.contain?(270, 220, outline: true) # => false
250
+ shape.contain?([270, 220], outline: true) # => false
251
+ shape.contain?(261.875, 245.625, outline: true) # => true
252
+ shape.contain?([261.875, 245.625], outline: true) # => true
253
+ shape.contain?(261.875, 246.625, outline: true) # => false
254
+ shape.contain?([261.875, 246.625], outline: true) # => false
255
+ shape.contain?(261.875, 246.625, outline: true, distance_tolerance: 1) # => true
256
+ shape.contain?([261.875, 246.625], outline: true, distance_tolerance: 1) # => true
244
257
  ```
245
258
 
246
259
  ### `PerfectShape::Rectangle`
@@ -265,8 +278,8 @@ Includes `PerfectShape::RectangularShape`
265
278
  - `#max_x`: max x
266
279
  - `#max_y`: max y
267
280
  - `#bounding_box`: bounding box is a rectangle with x = min x, y = min y, and width/height of shape
268
- - `#contain?(x_or_point, y=nil, outline: false, distance_tolerance: 0)`: checks if point is inside when `outline` is `false` or if point is on the outline when `outline` is `true`. `distance_tolerance` can be used as a fuzz factor when `outline` is `true`, for example, to help GUI users mouse-click-select a rectangle shape from its outline more successfully
269
281
  - `#==(other)`: Returns `true` if equal to `other` or `false` otherwise
282
+ - `#contain?(x_or_point, y=nil, outline: false, distance_tolerance: 0)`: checks if point is inside when `outline` is `false` or if point is on the outline when `outline` is `true`. `distance_tolerance` can be used as a fuzz factor when `outline` is `true`, for example, to help GUI users mouse-click-select a rectangle shape from its outline more successfully
270
283
 
271
284
  Example:
272
285
 
@@ -308,8 +321,8 @@ Extends `PerfectShape::Rectangle`
308
321
  - `#max_x`: max x
309
322
  - `#max_y`: max y
310
323
  - `#bounding_box`: bounding box is a rectangle with x = min x, y = min y, and width/height of shape
311
- - `#contain?(x_or_point, y=nil, outline: false, distance_tolerance: 0)`: checks if point is inside when `outline` is `false` or if point is on the outline when `outline` is `true`. `distance_tolerance` can be used as a fuzz factor when `outline` is `true`, for example, to help GUI users mouse-click-select a square shape from its outline more successfully
312
324
  - `#==(other)`: Returns `true` if equal to `other` or `false` otherwise
325
+ - `#contain?(x_or_point, y=nil, outline: false, distance_tolerance: 0)`: checks if point is inside when `outline` is `false` or if point is on the outline when `outline` is `true`. `distance_tolerance` can be used as a fuzz factor when `outline` is `true`, for example, to help GUI users mouse-click-select a square shape from its outline more successfully
313
326
 
314
327
  Example:
315
328
 
@@ -361,8 +374,8 @@ Open Arc | Chord Arc | Pie Arc
361
374
  - `#max_x`: max x
362
375
  - `#max_y`: max y
363
376
  - `#bounding_box`: bounding box is a rectangle with x = min x, y = min y, and width/height of shape
364
- - `#contain?(x_or_point, y=nil, outline: false, distance_tolerance: 0)`: checks if point is inside when `outline` is `false` or if point is on the outline when `outline` is `true`. `distance_tolerance` can be used as a fuzz factor when `outline` is `true`, for example, to help GUI users mouse-click-select an arc shape from its outline more successfully
365
377
  - `#==(other)`: Returns `true` if equal to `other` or `false` otherwise
378
+ - `#contain?(x_or_point, y=nil, outline: false, distance_tolerance: 0)`: checks if point is inside when `outline` is `false` or if point is on the outline when `outline` is `true`. `distance_tolerance` can be used as a fuzz factor when `outline` is `true`, for example, to help GUI users mouse-click-select an arc shape from its outline more successfully
366
379
 
367
380
  Example:
368
381
 
@@ -483,8 +496,8 @@ Extends `PerfectShape::Arc`
483
496
  - `#max_x`: max x
484
497
  - `#max_y`: max y
485
498
  - `#bounding_box`: bounding box is a rectangle with x = min x, y = min y, and width/height of shape
486
- - `#contain?(x_or_point, y=nil, outline: false, distance_tolerance: 0)`: checks if point is inside when `outline` is `false` or if point is on the outline when `outline` is `true`. `distance_tolerance` can be used as a fuzz factor when `outline` is `true`, for example, to help GUI users mouse-click-select an ellipse shape from its outline more successfully
487
499
  - `#==(other)`: Returns `true` if equal to `other` or `false` otherwise
500
+ - `#contain?(x_or_point, y=nil, outline: false, distance_tolerance: 0)`: checks if point is inside when `outline` is `false` or if point is on the outline when `outline` is `true`. `distance_tolerance` can be used as a fuzz factor when `outline` is `true`, for example, to help GUI users mouse-click-select an ellipse shape from its outline more successfully
488
501
 
489
502
  Example:
490
503
 
@@ -543,8 +556,8 @@ Extends `PerfectShape::Ellipse`
543
556
  - `#max_x`: max x
544
557
  - `#max_y`: max y
545
558
  - `#bounding_box`: bounding box is a rectangle with x = min x, y = min y, and width/height of shape
546
- - `#contain?(x_or_point, y=nil, outline: false, distance_tolerance: 0)`: checks if point is inside when `outline` is `false` or if point is on the outline when `outline` is `true`. `distance_tolerance` can be used as a fuzz factor when `outline` is `true`, for example, to help GUI users mouse-click-select a circle shape from its outline more successfully
547
559
  - `#==(other)`: Returns `true` if equal to `other` or `false` otherwise
560
+ - `#contain?(x_or_point, y=nil, outline: false, distance_tolerance: 0)`: checks if point is inside when `outline` is `false` or if point is on the outline when `outline` is `true`. `distance_tolerance` can be used as a fuzz factor when `outline` is `true`, for example, to help GUI users mouse-click-select a circle shape from its outline more successfully
548
561
 
549
562
  Example:
550
563
 
@@ -598,8 +611,8 @@ A polygon can be thought of as a special case of [path](#perfectshapepath) that
598
611
  - `#center_x`: center x
599
612
  - `#center_y`: center y
600
613
  - `#bounding_box`: bounding box is a rectangle with x = min x, y = min y, and width/height of shape
601
- - `#contain?(x_or_point, y=nil, outline: false, distance_tolerance: 0)`: When `outline` is `false`, it 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)). Otherwise, when `outline` is `true`, it checks if point is on the outline. `distance_tolerance` can be used as a fuzz factor when `outline` is `true`, for example, to help GUI users mouse-click-select a polygon shape from its outline more successfully
602
614
  - `#==(other)`: Returns `true` if equal to `other` or `false` otherwise
615
+ - `#contain?(x_or_point, y=nil, outline: false, distance_tolerance: 0)`: When `outline` is `false`, it 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)). Otherwise, when `outline` is `true`, it checks if point is on the outline. `distance_tolerance` can be used as a fuzz factor when `outline` is `true`, for example, to help GUI users mouse-click-select a polygon shape from its outline more successfully
603
616
 
604
617
  Example:
605
618
 
@@ -644,9 +657,9 @@ Includes `PerfectShape::MultiPoint`
644
657
  - `#center_x`: center x
645
658
  - `#center_y`: center y
646
659
  - `#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)
660
+ - `#==(other)`: Returns `true` if equal to `other` or `false` otherwise
647
661
  - `#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))
648
662
  - `#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)
649
- - `#==(other)`: Returns `true` if equal to `other` or `false` otherwise
650
663
 
651
664
  Example:
652
665
 
@@ -686,8 +699,8 @@ A composite shape is simply an aggregate of multiple shapes (e.g. square and pol
686
699
  - `#center_x`: center x
687
700
  - `#center_y`: center y
688
701
  - `#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)
689
- - `#contain?(x_or_point, y=nil)`: checks if point is inside any of the shapes owned by the composite shape
690
702
  - `#==(other)`: Returns `true` if equal to `other` or `false` otherwise
703
+ - `#contain?(x_or_point, y=nil)`: checks if point is inside any of the shapes owned by the composite shape
691
704
 
692
705
  Example:
693
706
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
1
+ 0.3.2
@@ -30,6 +30,7 @@ module PerfectShape
30
30
  include Equalizer.new(:type, :x, :y, :width, :height, :start, :extent)
31
31
 
32
32
  TYPES = [:open, :chord, :pie]
33
+ DEFAULT_OUTLINE_RADIUS = BigDecimal('0.001')
33
34
  attr_accessor :type
34
35
  attr_reader :start, :extent
35
36
 
@@ -151,7 +152,7 @@ module PerfectShape
151
152
  true
152
153
  else
153
154
  distance_tolerance = BigDecimal(distance_tolerance.to_s)
154
- outside_inside_radius_difference = BigDecimal('0.001') + distance_tolerance * 2.0
155
+ outside_inside_radius_difference = DEFAULT_OUTLINE_RADIUS + distance_tolerance * 2.0
155
156
  outside_radius_difference = inside_radius_difference = outside_inside_radius_difference / 2.0
156
157
  outside_shape = Arc.new(type: type, center_x: center_x, center_y: center_y, radius_x: radius_x + outside_radius_difference, radius_y: radius_y + outside_radius_difference, start: start, extent: extent)
157
158
  inside_shape = Arc.new(type: type, center_x: center_x, center_y: center_y, radius_x: radius_x - inside_radius_difference, radius_y: radius_y - inside_radius_difference, start: start, extent: extent)
@@ -75,6 +75,8 @@ module PerfectShape
75
75
  include MultiPoint
76
76
  include Equalizer.new(:points)
77
77
 
78
+ OUTLINE_MINIMUM_DISTANCE_THRESHOLD = BigDecimal('0.001')
79
+
78
80
  # Checks if cubic bézier curve contains point (two-number Array or x, y args)
79
81
  #
80
82
  # @param x The X coordinate of the point to test.
@@ -83,24 +85,29 @@ module PerfectShape
83
85
  # @return {@code true} if the point lies within the bound of
84
86
  # the cubic bézier curve, {@code false} if the point lies outside of the
85
87
  # cubic bézier curve's bounds.
86
- def contain?(x_or_point, y = nil)
88
+ def contain?(x_or_point, y = nil, outline: false, distance_tolerance: 0)
87
89
  x, y = normalize_point(x_or_point, y)
88
90
  return unless x && y
89
91
 
90
- # Either x or y was infinite or NaN.
91
- # A NaN always produces a negative response to any test
92
- # and Infinity values cannot be "inside" any path so
93
- # they should return false as well.
94
- return false if (!(x * 0.0 + y * 0.0 == 0.0))
95
- # We count the "Y" crossings to determine if the point is
96
- # inside the curve bounded by its closing line.
97
- x1 = points[0][0]
98
- y1 = points[0][1]
99
- x2 = points[3][0]
100
- y2 = points[3][1]
101
- line = PerfectShape::Line.new(points: [[x1, y1], [x2, y2]])
102
- crossings = line.point_crossings(x, y) + point_crossings(x, y);
103
- (crossings & 1) == 1
92
+ if outline
93
+ minimum_distance_threshold = OUTLINE_MINIMUM_DISTANCE_THRESHOLD + distance_tolerance
94
+ point_segment_distance(x, y, minimum_distance_threshold: minimum_distance_threshold) < minimum_distance_threshold
95
+ else
96
+ # Either x or y was infinite or NaN.
97
+ # A NaN always produces a negative response to any test
98
+ # and Infinity values cannot be "inside" any path so
99
+ # they should return false as well.
100
+ return false if (!(x * 0.0 + y * 0.0 == 0.0))
101
+ # We count the "Y" crossings to determine if the point is
102
+ # inside the curve bounded by its closing line.
103
+ x1 = points[0][0]
104
+ y1 = points[0][1]
105
+ x2 = points[3][0]
106
+ y2 = points[3][1]
107
+ line = PerfectShape::Line.new(points: [[x1, y1], [x2, y2]])
108
+ crossings = line.point_crossings(x, y) + point_crossings(x, y);
109
+ (crossings & 1) == 1
110
+ end
104
111
  end
105
112
 
106
113
  # Calculates the number of times the cubic bézier curve
@@ -116,5 +123,86 @@ module PerfectShape
116
123
  return unless x && y
117
124
  CubicBezierCurve.point_crossings(points[0][0], points[0][1], points[1][0], points[1][1], points[2][0], points[2][1], points[3][0], points[3][1], x, y, level)
118
125
  end
126
+
127
+ # The center point on the outline of the curve
128
+ def curve_center_point
129
+ subdivisions.last.points[0]
130
+ end
131
+
132
+ # The center point x on the outline of the curve
133
+ def curve_center_x
134
+ subdivisions.last.points[0][0]
135
+ end
136
+
137
+ # The center point y on the outline of the curve
138
+ def curve_center_y
139
+ subdivisions.last.points[0][1]
140
+ end
141
+
142
+ # Subdivides CubicBezierCurve exactly at its curve center
143
+ # returning 2 CubicBezierCurve's as a two-element Array by default
144
+ # `number` parameter may be specified as an even number in case more
145
+ # subdivisions are needed. If an odd number is given, it is rounded
146
+ # up to the closest even number above it (e.g. 3 becomes 4).
147
+ def subdivisions(number = 2)
148
+ number = (number.to_i / 2.0).ceil*2
149
+ x1 = points[0][0]
150
+ y1 = points[0][1]
151
+ ctrlx1 = points[1][0]
152
+ ctrly1 = points[1][1]
153
+ ctrlx2 = points[2][0]
154
+ ctrly2 = points[2][1]
155
+ x2 = points[3][0]
156
+ y2 = points[3][1]
157
+ centerx = (ctrlx1 + ctrlx2) / 2.0
158
+ centery = (ctrly1 + ctrly2) / 2.0
159
+ ctrlx1 = (x1 + ctrlx1) / 2.0
160
+ ctrly1 = (y1 + ctrly1) / 2.0
161
+ ctrlx2 = (x2 + ctrlx2) / 2.0
162
+ ctrly2 = (y2 + ctrly2) / 2.0
163
+ ctrlx12 = (ctrlx1 + centerx) / 2.0
164
+ ctrly12 = (ctrly1 + centery) / 2.0
165
+ ctrlx21 = (ctrlx2 + centerx) / 2.0
166
+ ctrly21 = (ctrly2 + centery) / 2.0
167
+ centerx = (ctrlx12 + ctrlx21) / 2.0
168
+ centery = (ctrly12 + ctrly21) / 2.0
169
+ default_subdivisions = [
170
+ CubicBezierCurve.new(points: [x1, y1, ctrlx1, ctrly1, ctrlx12, ctrly12, centerx, centery]),
171
+ CubicBezierCurve.new(points: [centerx, centery, ctrlx21, ctrly21, ctrlx2, ctrly2, x2, y2])
172
+ ]
173
+ if number > 2
174
+ default_subdivisions.map { |curve| curve.subdivisions(number - 2) }.flatten
175
+ else
176
+ default_subdivisions
177
+ end
178
+ end
179
+
180
+ def point_segment_distance(x_or_point, y = nil, minimum_distance_threshold: OUTLINE_MINIMUM_DISTANCE_THRESHOLD)
181
+ x, y = normalize_point(x_or_point, y)
182
+ return unless x && y
183
+
184
+ point = Point.new(x, y)
185
+ current_curve = self
186
+ minimum_distance = point.point_distance(curve_center_point)
187
+ last_minimum_distance = minimum_distance + 1 # start bigger to ensure going through loop once at least
188
+ while minimum_distance >= minimum_distance_threshold && minimum_distance < last_minimum_distance
189
+ curve1, curve2 = current_curve.subdivisions
190
+ distance1 = point.point_distance(curve1.curve_center_point)
191
+ distance2 = point.point_distance(curve2.curve_center_point)
192
+ last_minimum_distance = minimum_distance
193
+ if distance1 < distance2
194
+ minimum_distance = distance1
195
+ current_curve = curve1
196
+ else
197
+ minimum_distance = distance2
198
+ current_curve = curve2
199
+ end
200
+ end
201
+ if minimum_distance < minimum_distance_threshold
202
+ minimum_distance
203
+ else
204
+ last_minimum_distance
205
+ end
206
+ end
119
207
  end
120
208
  end
@@ -84,6 +84,10 @@ module PerfectShape
84
84
  [x, y]
85
85
  end
86
86
 
87
+ # Subclasses must implement
88
+ def contain?(x_or_point, y = nil, outline: false, distance_tolerance: 0)
89
+ end
90
+
87
91
  # Subclasses must implement
88
92
  def ==(other)
89
93
  end
@@ -2,16 +2,16 @@
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.3.1 ruby lib
5
+ # stub: perfect-shape 0.3.2 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "perfect-shape".freeze
9
- s.version = "0.3.1"
9
+ s.version = "0.3.2"
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 = "2022-01-08"
14
+ s.date = "2022-01-10"
15
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 bezier 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 = [
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.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Maleh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-01-08 00:00:00.000000000 Z
11
+ date: 2022-01-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: equalizer