Class: Polygon::Validator

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(shape) ⇒ Validator

Returns a new instance of Validator.

Parameters:



18
19
20
# File 'lib/polygon/validator.rb', line 18

def initialize(shape)
  @shape = shape
end

Instance Attribute Details

#shapeObject (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

Parameters:

  • point (Polygon::Point)

    the point to validate if it is inside the shape

  • shape (Polygon::Shape)

    the shape that may or may not contain the point

Returns:

  • (true, false)

    true if point is inside the 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