Class: Polygon::Validator
- Inherits:
-
Object
- Object
- Polygon::Validator
- Defined in:
- lib/polygon/validator.rb,
lib/polygon/validator/version.rb
Overview
Validator service for polygon shapes
Could be used to validate if a point is inside a shape
Defined Under Namespace
Classes: Error
Constant Summary collapse
- VERSION =
'2.0.0'
Instance Attribute Summary collapse
-
#shape ⇒ Object
readonly
Returns the value of attribute shape.
Instance Method Summary collapse
-
#contains_point?(point) ⇒ true, false
Validate if a point is inside the shape.
-
#initialize(shape) ⇒ Validator
constructor
A new instance of Validator.
Constructor Details
#initialize(shape) ⇒ Validator
Returns a new instance of Validator.
18 19 20 |
# File 'lib/polygon/validator.rb', line 18 def initialize(shape) @shape = shape end |
Instance Attribute Details
#shape ⇒ Object (readonly)
Returns the value of attribute shape.
15 16 17 |
# File 'lib/polygon/validator.rb', line 15 def shape @shape end |
Instance Method Details
#contains_point?(point) ⇒ true, false
Validate if a point is inside the shape
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/polygon/validator.rb', line 27 def contains_point?(point) points = shape.points winding_number = 0 points.each.with_index do |point_a, i| point_b = point_a == points.last ? points.first : points[i + 1] next unless vertically_in_bounds?(point_a, point_b, point) winding_number += (left_offset(point_a, point_b, point) <=> 0) end !winding_number.zero? end |