Class: Sumi::Geometry::Polygon

Inherits:
PolygonBase
  • Object
show all
Defined in:
geometry/lib/sumi/geometry/elements/polygon.rb,
geometry/lib/sumi/geometry/predicate.rb

Overview

Variable-size closed lined element with at least three vertices.

Examples:

Pair point notation with its English convenience

points = [[0, 0], [2, 0], [1, 1]]
segments = Sumi::Geometry::Polygon.(*points).segments
Sumi::Geometry::Polygon[*segments] == Sumi::Geometry::Polygon.from_segments(*segments)
Sumi::Geometry::Polygon.(*points) == Sumi::Geometry::Polygon.from_points(*points)

Classify points against a closed boundary

polygon = Sumi::Geometry::Polygon.([0, 0], [6, 0], [3, 4])
polygon.inside?([3, 2]) # => true
polygon.on?([3, 0])     # => true
polygon.outside?([7, 2]) # => true

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](*segments, position: Origin) ⇒ Sumi::Geometry::Polygon

Builds a polygon from boundary segments.

Parameters:

Returns:

Raises:



46
47
48
49
50
51
52
# File 'geometry/lib/sumi/geometry/elements/polygon.rb', line 46

class Polygon < PolygonBase
  private

  def validate_geometry!
    Error.("Polygon requires at least three vertices") if points.size < 4
  end
end

.call(*points) ⇒ Sumi::Geometry::Polygon

Builds a polygon from boundary points.

Parameters:

  • points (Array<Sumi::Geometry::Point, Array<Numeric>>) —

    boundary points without a repeated closing point

Returns:

Raises:



46
47
48
49
50
51
52
# File 'geometry/lib/sumi/geometry/elements/polygon.rb', line 46

class Polygon < PolygonBase
  private

  def validate_geometry!
    Error.("Polygon requires at least three vertices") if points.size < 4
  end
end

.from_points(*points) ⇒ Sumi::Geometry::Polygon

Builds a polygon from boundary points.

Parameters:

  • points (Array<Sumi::Geometry::Point, Array<Numeric>>) —

    boundary points without a repeated closing point

Returns:

Raises:



46
47
48
49
50
51
52
# File 'geometry/lib/sumi/geometry/elements/polygon.rb', line 46

class Polygon < PolygonBase
  private

  def validate_geometry!
    Error.("Polygon requires at least three vertices") if points.size < 4
  end
end

.from_segments(*segments, position: Origin) ⇒ Sumi::Geometry::Polygon

Builds a polygon from boundary segments.

Parameters:

Returns:

Raises:



46
47
48
49
50
51
52
# File 'geometry/lib/sumi/geometry/elements/polygon.rb', line 46

class Polygon < PolygonBase
  private

  def validate_geometry!
    Error.("Polygon requires at least three vertices") if points.size < 4
  end
end

Instance Method Details

#concave?(precision: nil) ⇒ Boolean

Reports whether this is a simple non-convex polygon.

Self-intersecting and degenerate polygons are neither convex nor concave.

Parameters:

  • precision (Integer, nil) (defaults to: nil) —

    decimal precision, or nil for the current function default

Returns:

  • (Boolean)

Raises:



173
174
175
# File 'geometry/lib/sumi/geometry/predicate.rb', line 173

def concave?(precision: nil)
  simple?(precision:) && Predicate.concave_turns?(vertices, precision:)
end

#convex?(precision: nil) ⇒ Boolean

Reports whether this is a simple polygon whose non-collinear turns all have one orientation.

Redundant vertices on straight edges are permitted. Self-intersecting and fully degenerate polygons are not convex.

Parameters:

  • precision (Integer, nil) (defaults to: nil) —

    decimal precision, or nil for the current function default

Returns:

  • (Boolean)

Raises:



163
164
165
# File 'geometry/lib/sumi/geometry/predicate.rb', line 163

def convex?(precision: nil)
  simple?(precision:) && Predicate.convex_turns?(vertices, precision:)
end

#perimeter ⇒ Float

Returns the closed path perimeter.

Returns:

  • (Float)


46
47
48
49
50
51
52
# File 'geometry/lib/sumi/geometry/elements/polygon.rb', line 46

class Polygon < PolygonBase
  private

  def validate_geometry!
    Error.("Polygon requires at least three vertices") if points.size < 4
  end
end

#simple?(precision: nil) ⇒ Boolean

Reports whether the polygon boundary has no self-intersection.

Adjacent edges may meet only at their shared endpoint. Non-adjacent touches and overlaps make the polygon non-simple.

Parameters:

  • precision (Integer, nil) (defaults to: nil) —

    decimal precision, or nil for the current function default

Returns:

  • (Boolean)

Raises:



154
# File 'geometry/lib/sumi/geometry/predicate.rb', line 154

def simple?(precision: nil) = Predicate.simple?(vertices, precision:)