Class: Sumi::Geometry::Element::Lined

Inherits:
Sumi::Geometry::Element show all
Defined in:
geometry/lib/sumi/geometry/element/lined.rb

Overview

Element whose boundary is represented by straight segments.

The same path is available as immutable #points, #segments, and #lines collections. #vertices omits the repeated closing point from closed shapes. Open paths have the same values in vertices and points. Only closed shapes have a filled interior, so inside? on an open path is equivalent to testing its boundary.

Examples:

Inspect path and geometric views

rect = Sumi::Geometry::Rect[8, 4]
rect.vertices.size # => 4
rect.points.size   # => 5
rect.segments.size # => 4
rect.lines.size    # => 4

See Also:

Instance Method Summary collapse

Methods inherited from Sumi::Geometry::Element

#at, #closed?, #ignorable?, lined

Instance Method Details

#[](i) ⇒ Sumi::Geometry::Line

Returns a line by index.

Parameters:

  • i (Integer) —

    line index

Returns:

Raises:



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

def [](i) = lines[i].tap { |line| Error.("No line exists for index: #{i}") unless line }

#approx(precision = nil) ⇒ Sumi::Geometry::Element::Lined

Returns an element rebuilt from rounded boundary points.

Segments are derived from the rounded points so both representations describe the same path. When rounding breaks a concrete shape invariant, the result widens to a less specific Rect, Polygon, or Polyline rather than retaining a misleading concrete class.

Parameters:

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

    decimal precision, or nil for the current function default

Returns:

Raises:



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

def approx(precision = nil) = self.class.send(:approximate, *rounded_points(precision))

#box ⇒ Sumi::Geometry::Rect

Returns the bounding rectangle.



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

def box = Rect.from_corners([(xs = points.map(&:x)).min, (ys = points.map(&:y)).min], [xs.max, ys.max])

#call(i) ⇒ Sumi::Geometry::Point

Returns a point by index.

Parameters:

  • i (Integer) —

    point index

Returns:

Raises:



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

def call(i) = points[i].tap { Error.("No point exists for index: #{i}") unless it }

#draw(node, **attributes) ⇒ Object

Draws an approximate element into a graphics node.

Parameters:

  • node (Object) —

    graphics node receiving the drawing command

  • attributes (Hash) —

    drawing attributes

Returns:

  • (Object) —

    graphics node command result



242
243
244
# File 'geometry/lib/sumi/geometry/element/lined.rb', line 242

def draw(...)
  approx.send(:draw!, ...)
end

#eq?(other, precision: nil) ⇒ Boolean

Compares element points with optional numeric precision.

Parameters:

  • other (Object) —

    object to compare

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

    decimal precision, or nil for the current function default

Returns:

  • (Boolean)


340
341
342
343
344
# File 'geometry/lib/sumi/geometry/element/lined.rb', line 340

def eq?(other, precision: nil)
  other.instance_of?(self.class) &&
    points.size == other.points.size &&
    points.zip(other.points).all? { |left, right| left.eq?(right, precision:) }
end

#eql?(other) ⇒ Boolean Also known as: ==

Reports strict element equality by class and exact points.

Parameters:

  • other (Object) —

    object to compare

Returns:

  • (Boolean)


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

def eql?(other) = other.instance_of?(self.class) && points == other.points

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

Returns immutable boundary equations for all lines.

Returns:



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

def equations = @equations ||= lines.map(&:equation).freeze

#hash ⇒ Integer

Returns a hash compatible with strict equality.

Returns:

  • (Integer)


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

def hash = [self.class, *points].hash

#head ⇒ Sumi::Geometry::Segment

Returns the first segment.



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

def head = @head ||= segments.first

#inside?(point) ⇒ Boolean

Reports whether a point is inside or on the boundary.

Open paths have no filled interior. For them this predicate is true only for points on the actual path boundary.

Examples:

Compare closed and open path containment

rect = Sumi::Geometry::Rect[8, 4]
line = Sumi::Geometry::Line.([0, 0], [8, 0])
rect.inside?([4, 2]) # => true
line.inside?([4, 2]) # => false
line.inside?([4, 0]) # => true

Parameters:

Returns:

  • (Boolean)

Raises:



447
448
449
450
451
452
453
# File 'geometry/lib/sumi/geometry/element/lined.rb', line 447

def inside?(point)
  point = Tuple[Point, point]

  return on?(point) unless closed?

  on?(point) || pnpoly(points, point)
end

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

Intersects the lined 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:



376
377
378
379
380
381
382
383
384
# File 'geometry/lib/sumi/geometry/element/lined.rb', line 376

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

  points = lines.flat_map do |line|
    equation.intersect(line.equation).select { |point| line_point?(line, point, precision) }
  end

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

#length ⇒ Float

Returns the total path length.

Returns:

  • (Float)


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

def length = @length ||= segments.sum(&:length)

#lines ⇒ Array<Sumi::Geometry::Line>

Returns immutable boundary lines with the stored endpoints and segments.

Returns:



410
411
412
413
414
415
416
417
418
419
420
421
422
# File 'geometry/lib/sumi/geometry/element/lined.rb', line 410

def lines
  @lines ||= points
    .each_cons(2)
    .zip(segments)
    .map do |points, segment|
      # Rebuilding from either view alone loses stored endpoints or input angles.
      Line.send(:new) do
        @points = points
        @segments = [segment]
      end
    end
    .freeze
end

#on?(point) ⇒ Boolean

Reports whether a point is on the boundary.

Parameters:

Returns:

  • (Boolean)

Raises:



459
460
461
462
463
# File 'geometry/lib/sumi/geometry/element/lined.rb', line 459

def on?(point)
  point = Tuple[Point, point]

  lines.any? { it.over?(point) }
end

#outside?(point) ⇒ Boolean

Reports whether a point is outside the element boundary.

Parameters:

Returns:

  • (Boolean)

Raises:



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

def outside?(point) = !inside?(point)

#points(approximate = false) ⇒ Array<Sumi::Geometry::Point>

Returns immutable element points in path order. Closed elements repeat the first vertex at the end.

Parameters:

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

    true to round points with the current function precision

Returns:



250
251
252
# File 'geometry/lib/sumi/geometry/element/lined.rb', line 250

def points(approximate = false)
  approximate ? rounded_points(nil) : @points
end

#position ⇒ Sumi::Geometry::Point

Returns the first point.



265
266
267
# File 'geometry/lib/sumi/geometry/element/lined.rb', line 265

def position
  @position ||= points.first
end

#reflect(x: true, y: true) ⇒ Sumi::Geometry::Element::Lined

Returns an element reflected across the selected axes.

Parameters:

  • x (Boolean) (defaults to: true) —

    reflect across the x-axis

  • y (Boolean) (defaults to: true) —

    reflect across the y-axis

Returns:

Raises:



327
328
329
330
331
332
# File 'geometry/lib/sumi/geometry/element/lined.rb', line 327

Affinity.public_instance_methods(false).each do |transform|
  define_method(transform) do |*args, **kwargs, &block|
    transformed = points.map { it.public_send(transform, *args, **kwargs, &block) }
    self.class.send(:affine, *transformed)
  end
end

#rotate(a) ⇒ Sumi::Geometry::Element::Lined

Returns an element rotated around the origin.

Parameters:

  • a (Numeric) —

    clockwise angle in degrees

Returns:

Raises:



327
328
329
330
331
332
# File 'geometry/lib/sumi/geometry/element/lined.rb', line 327

Affinity.public_instance_methods(false).each do |transform|
  define_method(transform) do |*args, **kwargs, &block|
    transformed = points.map { it.public_send(transform, *args, **kwargs, &block) }
    self.class.send(:affine, *transformed)
  end
end

#scale(sx, sy = Undefined) ⇒ Sumi::Geometry::Element::Lined

Returns an element scaled from the origin.

Parameters:

  • sx (Numeric) —

    x scale factor

  • sy (Numeric, Sumi::Undefined) (defaults to: Undefined) —

    y scale factor, defaulting to sx

Returns:

Raises:



327
328
329
330
331
332
# File 'geometry/lib/sumi/geometry/element/lined.rb', line 327

Affinity.public_instance_methods(false).each do |transform|
  define_method(transform) do |*args, **kwargs, &block|
    transformed = points.map { it.public_send(transform, *args, **kwargs, &block) }
    self.class.send(:affine, *transformed)
  end
end

#segments(approximate = false) ⇒ Array<Sumi::Geometry::Segment>

Returns immutable element segments.

Parameters:

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

    true to round segments with the current function precision

Returns:



272
273
274
# File 'geometry/lib/sumi/geometry/element/lined.rb', line 272

def segments(approximate = false)
  approximate ? rounded_segments(nil) : @segments
end

#skew(ax, ay = Undefined) ⇒ Sumi::Geometry::Element::Lined

Returns an element skewed from the origin.

Parameters:

  • ax (Numeric) —

    x-axis skew angle in degrees

  • ay (Numeric, Sumi::Undefined) (defaults to: Undefined) —

    y-axis skew angle in degrees, defaulting to ax

Returns:

Raises:



327
328
329
330
331
332
# File 'geometry/lib/sumi/geometry/element/lined.rb', line 327

Affinity.public_instance_methods(false).each do |transform|
  define_method(transform) do |*args, **kwargs, &block|
    transformed = points.map { it.public_send(transform, *args, **kwargs, &block) }
    self.class.send(:affine, *transformed)
  end
end

#skew_x(a) ⇒ Sumi::Geometry::Element::Lined

Returns an element skewed along x.

Parameters:

  • a (Numeric) —

    skew angle in degrees

Returns:

Raises:



327
328
329
330
331
332
# File 'geometry/lib/sumi/geometry/element/lined.rb', line 327

Affinity.public_instance_methods(false).each do |transform|
  define_method(transform) do |*args, **kwargs, &block|
    transformed = points.map { it.public_send(transform, *args, **kwargs, &block) }
    self.class.send(:affine, *transformed)
  end
end

#skew_y(a) ⇒ Sumi::Geometry::Element::Lined

Returns an element skewed along y.

Parameters:

  • a (Numeric) —

    skew angle in degrees

Returns:

Raises:



327
328
329
330
331
332
# File 'geometry/lib/sumi/geometry/element/lined.rb', line 327

Affinity.public_instance_methods(false).each do |transform|
  define_method(transform) do |*args, **kwargs, &block|
    transformed = points.map { it.public_send(transform, *args, **kwargs, &block) }
    self.class.send(:affine, *transformed)
  end
end

#tail ⇒ Sumi::Geometry::Segment

Returns the last segment.



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

def tail = @tail ||= segments.last

#translate(dx, dy = Undefined) ⇒ Sumi::Geometry::Element::Lined

Returns an element translated by offset.

Parameters:

  • dx (Numeric) —

    x offset

  • dy (Numeric, Sumi::Undefined) (defaults to: Undefined) —

    y offset, defaulting to dx

Returns:

Raises:



327
328
329
330
331
332
# File 'geometry/lib/sumi/geometry/element/lined.rb', line 327

Affinity.public_instance_methods(false).each do |transform|
  define_method(transform) do |*args, **kwargs, &block|
    transformed = points.map { it.public_send(transform, *args, **kwargs, &block) }
    self.class.send(:affine, *transformed)
  end
end

#vertices ⇒ Array<Sumi::Geometry::Point>

Returns immutable geometric vertices in path order. Closed elements omit the repeated closing point. Open elements return #points.

Returns:



257
258
259
260
261
# File 'geometry/lib/sumi/geometry/element/lined.rb', line 257

def vertices
  return points unless closed?

  @vertices ||= points[...-1].freeze
end