Class: Sumi::Geometry::Element::Lined
- Inherits:
-
Sumi::Geometry::Element
- Object
- Sumi::Geometry::Element
- Sumi::Geometry::Element::Lined
- 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.
Instance Method Summary collapse
-
#[](i) ⇒ Sumi::Geometry::Line
Returns a line by index.
-
#approx(precision = nil) ⇒ Sumi::Geometry::Element::Lined
Returns an element rebuilt from rounded boundary points.
-
#box ⇒ Sumi::Geometry::Rect
Returns the bounding rectangle.
-
#call(i) ⇒ Sumi::Geometry::Point
Returns a point by index.
-
#draw(node, **attributes) ⇒ Object
Draws an approximate element into a graphics node.
-
#eq?(other, precision: nil) ⇒ Boolean
Compares element points with optional numeric precision.
-
#eql?(other) ⇒ Boolean
(also: #==)
Reports strict element equality by class and exact points.
-
#equations ⇒ Array<Sumi::Geometry::Equation::Linear>
Returns immutable boundary equations for all lines.
-
#hash ⇒ Integer
Returns a hash compatible with strict equality.
-
#head ⇒ Sumi::Geometry::Segment
Returns the first segment.
-
#inside?(point) ⇒ Boolean
Reports whether a point is inside or on the boundary.
-
#intersection(equation, precision: nil) ⇒ Array<Sumi::Geometry::Point>
Intersects the lined element boundary with an equation.
-
#length ⇒ Float
Returns the total path length.
-
#lines ⇒ Array<Sumi::Geometry::Line>
Returns immutable boundary lines with the stored endpoints and segments.
-
#on?(point) ⇒ Boolean
Reports whether a point is on the boundary.
-
#outside?(point) ⇒ Boolean
Reports whether a point is outside the element boundary.
-
#points(approximate = false) ⇒ Array<Sumi::Geometry::Point>
Returns immutable element points in path order.
-
#position ⇒ Sumi::Geometry::Point
Returns the first point.
-
#reflect(x: true, y: true) ⇒ Sumi::Geometry::Element::Lined
Returns an element reflected across the selected axes.
-
#rotate(a) ⇒ Sumi::Geometry::Element::Lined
Returns an element rotated around the origin.
-
#scale(sx, sy = Undefined) ⇒ Sumi::Geometry::Element::Lined
Returns an element scaled from the origin.
-
#segments(approximate = false) ⇒ Array<Sumi::Geometry::Segment>
Returns immutable element segments.
-
#skew(ax, ay = Undefined) ⇒ Sumi::Geometry::Element::Lined
Returns an element skewed from the origin.
-
#skew_x(a) ⇒ Sumi::Geometry::Element::Lined
Returns an element skewed along x.
-
#skew_y(a) ⇒ Sumi::Geometry::Element::Lined
Returns an element skewed along y.
-
#tail ⇒ Sumi::Geometry::Segment
Returns the last segment.
-
#translate(dx, dy = Undefined) ⇒ Sumi::Geometry::Element::Lined
Returns an element translated by offset.
-
#vertices ⇒ Array<Sumi::Geometry::Point>
Returns immutable geometric vertices in path order.
Methods inherited from Sumi::Geometry::Element
#at, #closed?, #ignorable?, lined
Instance Method Details
#[](i) ⇒ Sumi::Geometry::Line
Returns a line by index.
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.
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.
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
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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 |