Class: Sumi::Geometry::Element

Inherits:
Object
  • Object
show all
Defined in:
geometry/lib/sumi/geometry/element.rb,
geometry/lib/sumi/geometry/element/arced.rb,
geometry/lib/sumi/geometry/element/lined.rb

Overview

Abstract base class for positioned geometry values.

Construct concrete shapes through their class factories. Element operations return new values, and #box supplies the axis-aligned bounds used by alignment and tiling helpers.

Direct Known Subclasses

Arced, Lined

Defined Under Namespace

Classes: Arced, Lined

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.lined(size = Undefined, open: false) ⇒ Class

Examples:

Define a custom two-segment open shape

Path = Sumi::Geometry::Element.lined(2, open: true)
Path.([0, 0], [1, 0], [1, 1])

Builds a lined element subclass. Instances expose total path length. Closed classes also expose perimeter.

Parameters:

  • size (Integer, Sumi::Undefined) (defaults to: Undefined) —

    segment count for fixed-size elements, or Undefined for variable size

  • open (Boolean) (defaults to: false) —

    true for an open path, false for a closed path

Returns:

  • (Class) —

    subclass of Lined

Raises:



23
# File 'geometry/lib/sumi/geometry/element.rb', line 23

def self.lined(...) = Lined.send(:build, ...)

Instance Method Details

#at(point = nil, dx: 0, dy: 0) ⇒ Sumi::Geometry::Element

Returns a copy moved to a point and optional offset.

Examples:

Position a copy without mutating the source

source = Sumi::Geometry::Rect[8, 4, position: [1, 2]]
moved = source.at([10, 20], dx: 2)
source.position.deconstruct # => [1.0, 2.0]
moved.position.deconstruct  # => [12.0, 20.0]

Parameters:

  • point (Sumi::Geometry::Point, Array<Numeric>, nil) (defaults to: nil) —

    target position, or nil to keep current position

  • dx (Numeric) (defaults to: 0) —

    additional x offset

  • dy (Numeric) (defaults to: 0) —

    additional y offset

Returns:

Raises:



38
39
40
41
42
43
44
45
46
47
# File 'geometry/lib/sumi/geometry/element.rb', line 38

def at(point = nil, dx: 0, dy: 0)
  point = point ? Tuple[Point, point] : position
  dx = Real[:dx, dx]
  dy = Real[:dy, dy]

  translate(
    (point.x - position.x) + dx,
    (point.y - position.y) + dy
  )
end

#box ⇒ Sumi::Geometry::Rect

This method is abstract.

Subclasses implement element-specific bounds.

Returns the bounding rectangle.

Returns:

Raises:



53
# File 'geometry/lib/sumi/geometry/element.rb', line 53

def box = PanicError.("#{self.class}#box must be implemented")

#closed? ⇒ Boolean

Reports whether the element boundary forms a closed path.

Returns:

  • (Boolean)


57
# File 'geometry/lib/sumi/geometry/element.rb', line 57

def closed? = self.class.send(:close?)

#equations ⇒ Array<Sumi::Geometry::Equation>

This method is abstract.

Subclasses implement element-specific equations.

Returns carrier equations for candidate boundary intersections. A finite element can represent only part of each carrier.

Returns:

Raises:



64
# File 'geometry/lib/sumi/geometry/element.rb', line 64

def equations = PanicError.("#{self.class}#equations must be implemented")

#ignorable?(precision: nil) ⇒ Boolean

Reports whether the element has zero bounding width and height.

Parameters:

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

    decimal precision, or nil for the current function default

Returns:

  • (Boolean)


69
# File 'geometry/lib/sumi/geometry/element.rb', line 69

def ignorable?(precision: nil) = box.then { F.zero?(it.width, precision:) && F.zero?(it.height, precision:) }

#intersection(equation, precision: nil) ⇒ Array<Sumi::Geometry::Point>

Intersects the element boundary with an equation.

Precision controls boundary membership, returned-coordinate rounding, and duplicate collapse. A nil precision uses the current thread's function precision for all three stages.

Examples:

Intersect a rectangle with a vertical line

rect = Sumi::Geometry::Rect[8, 4]
axis = Sumi::Geometry::Equation.vertical(3)
rect.intersection(axis).map(&:deconstruct) # => [[3.0, 0.0], [3.0, 4.0]]

Parameters:

  • equation (Sumi::Geometry::Equation) —

    equation to intersect with

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

    coordinate precision, or nil for the current function default

Returns:

Raises:



84
85
86
87
88
89
90
91
92
# File 'geometry/lib/sumi/geometry/element.rb', line 84

def intersection(equation, precision: nil)
  Error.("Must be an equation: #{equation}") unless equation.is_a?(Equation)

  points = equations.flat_map do |candidate|
    equation.intersect(candidate).select { |point| boundary_point?(point, precision) }
  end

  points.map { |point| point.approx(precision) }.uniq
end

#position ⇒ Sumi::Geometry::Point

This method is abstract.

Subclasses implement element-specific positioning.

Returns the element position.

Returns:

Raises:



98
# File 'geometry/lib/sumi/geometry/element.rb', line 98

def position = PanicError.("#{self.class}#position must be implemented")

#translate(_x, _y) ⇒ Sumi::Geometry::Element

This method is abstract.

Subclasses implement element-specific translation.

Returns a translated copy.

Parameters:

  • _x (Numeric) —

    x offset

  • _y (Numeric) —

    y offset

Returns:

Raises:



106
# File 'geometry/lib/sumi/geometry/element.rb', line 106

def translate(_x, _y) = PanicError.("#{self.class}#translate must be implemented")