Module: Sumi::Geometry::Operation
- Extended by:
- Operation
- Included in:
- Operation
- Defined in:
- geometry/lib/sumi/geometry/operation.rb,
geometry/lib/sumi/geometry/operation.rb,
geometry/lib/sumi/geometry/operation/box.rb,
geometry/lib/sumi/geometry/operation/align.rb,
geometry/lib/sumi/geometry/operation/sweep.rb
Overview
Stateless operations that relate or derive geometry values.
alignment returns a translation offset, while align applies that
offset to a copy. Center alignment works on both axes. Edge alignments
change only the named axis and preserve the other coordinate. sweep
derives boundary-to-boundary spans from a closed geometry element, and
sweep! additionally requires at least one span.
Constant Summary collapse
- OperationError =
Raised when an operation starts but cannot complete.
Class.new(Error)
- OperationInapplicableError =
Raised when an operation does not apply to the target element.
Class.new(Error)
Class Method Summary collapse
-
.align(element, other, alignment = :center) ⇒ Sumi::Geometry::Element
Returns an element translated to align with another element.
-
.alignment(element, other, alignment = :center) ⇒ Sumi::Geometry::Point
Returns the offset needed to align one element with another.
-
.sweep(element, initial:, angle:, step:, limit: Sweep::LIMIT) {|lines| ... } ⇒ Array<Sumi::Geometry::Line>
Sweeps parallel lines across a geometry element in both directions.
-
.sweep!(element, initial:, angle:, step:, limit: Sweep::LIMIT) {|lines| ... } ⇒ Array<Sumi::Geometry::Line>
Sweeps parallel lines across a geometry element and requires at least one result.
Instance Method Summary collapse
-
#box(*elements) ⇒ Sumi::Geometry::Rect
Returns the smallest axis-aligned rectangle enclosing all elements.
Class Method Details
.align(element, other, alignment = :center) ⇒ Sumi::Geometry::Element
Returns an element translated to align with another element. Center alignment changes both axes. Edge alignments change only the named axis.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'geometry/lib/sumi/geometry/operation.rb', line 82 module Operation extend self # Raised when an operation starts but cannot complete. OperationError = Class.new(Error) # Raised when an operation does not apply to the target element. OperationInapplicableError = Class.new(Error) private # Registers one or more public operation methods. # @api private # @param handler [Module] operation handler module # @param operations [Array<Symbol>] operation method names # @return [Array<Symbol>] registered operation names def register(handler, *operations) = operations.each { |operation| def_operation(operation, handler) } def def_operation(operation, handler) define_singleton_method(operation) do |element, *args, **kwargs, &block| OperationInapplicableError.("Not a Geometric Element: #{element}") unless element.is_a?(Element) unless handler.applicable?(element) OperationInapplicableError.("Operation not applicable to #{element}: #{handler}") end handler.public_send(operation, element, *args, **kwargs, &block) end end end |
.alignment(element, other, alignment = :center) ⇒ Sumi::Geometry::Point
Returns the offset needed to align one element with another. Center alignment includes both axes. Edge alignments return zero on the other axis.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'geometry/lib/sumi/geometry/operation.rb', line 82 module Operation extend self # Raised when an operation starts but cannot complete. OperationError = Class.new(Error) # Raised when an operation does not apply to the target element. OperationInapplicableError = Class.new(Error) private # Registers one or more public operation methods. # @api private # @param handler [Module] operation handler module # @param operations [Array<Symbol>] operation method names # @return [Array<Symbol>] registered operation names def register(handler, *operations) = operations.each { |operation| def_operation(operation, handler) } def def_operation(operation, handler) define_singleton_method(operation) do |element, *args, **kwargs, &block| OperationInapplicableError.("Not a Geometric Element: #{element}") unless element.is_a?(Element) unless handler.applicable?(element) OperationInapplicableError.("Operation not applicable to #{element}: #{handler}") end handler.public_send(operation, element, *args, **kwargs, &block) end end end |
.sweep(element, initial:, angle:, step:, limit: Sweep::LIMIT) {|lines| ... } ⇒ Array<Sumi::Geometry::Line>
Sweeps parallel lines across a geometry element in both directions.
angle is the direction of the returned lines. step is their signed perpendicular spacing.
Open paths yield no interior spans.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'geometry/lib/sumi/geometry/operation.rb', line 82 module Operation extend self # Raised when an operation starts but cannot complete. OperationError = Class.new(Error) # Raised when an operation does not apply to the target element. OperationInapplicableError = Class.new(Error) private # Registers one or more public operation methods. # @api private # @param handler [Module] operation handler module # @param operations [Array<Symbol>] operation method names # @return [Array<Symbol>] registered operation names def register(handler, *operations) = operations.each { |operation| def_operation(operation, handler) } def def_operation(operation, handler) define_singleton_method(operation) do |element, *args, **kwargs, &block| OperationInapplicableError.("Not a Geometric Element: #{element}") unless element.is_a?(Element) unless handler.applicable?(element) OperationInapplicableError.("Operation not applicable to #{element}: #{handler}") end handler.public_send(operation, element, *args, **kwargs, &block) end end end |
.sweep!(element, initial:, angle:, step:, limit: Sweep::LIMIT) {|lines| ... } ⇒ Array<Sumi::Geometry::Line>
Sweeps parallel lines across a geometry element and requires at least one result. It has the same geometry as sweep, but raises when the result is empty.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'geometry/lib/sumi/geometry/operation.rb', line 82 module Operation extend self # Raised when an operation starts but cannot complete. OperationError = Class.new(Error) # Raised when an operation does not apply to the target element. OperationInapplicableError = Class.new(Error) private # Registers one or more public operation methods. # @api private # @param handler [Module] operation handler module # @param operations [Array<Symbol>] operation method names # @return [Array<Symbol>] registered operation names def register(handler, *operations) = operations.each { |operation| def_operation(operation, handler) } def def_operation(operation, handler) define_singleton_method(operation) do |element, *args, **kwargs, &block| OperationInapplicableError.("Not a Geometric Element: #{element}") unless element.is_a?(Element) unless handler.applicable?(element) OperationInapplicableError.("Operation not applicable to #{element}: #{handler}") end handler.public_send(operation, element, *args, **kwargs, &block) end end end |
Instance Method Details
#box(*elements) ⇒ Sumi::Geometry::Rect
Returns the smallest axis-aligned rectangle enclosing all elements.
Each element contributes its existing Element#box, including zero-size boxes whose positions may still extend the result.
18 19 20 21 22 23 |
# File 'geometry/lib/sumi/geometry/operation/box.rb', line 18 def box(*elements) validate_box_elements(elements) boxes = elements.map(&:box) Rect.from_corners(minimum_corner(boxes), maximum_corner(boxes)) end |