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 =
'1.0.1'
Instance Attribute Summary collapse
-
#shape ⇒ Object
readonly
Returns the value of attribute shape.
Instance Method Summary collapse
-
#initialize(shape) ⇒ Validator
constructor
A new instance of Validator.
-
#point_inside_shape?(point, shape) ⇒ true, false
Validate if a point is inside a shape.
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
#point_inside_shape?(point, shape) ⇒ true, false
Validate if a point is inside a shape
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/polygon/validator.rb', line 29 def point_inside_shape?(point, shape) 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 |