Class: Sumi::Geometry::Point

Inherits:
Data
  • Object
show all
Includes:
Comparable
Defined in:
geometry/lib/sumi/geometry/point.rb,
geometry/lib/sumi/geometry/point.rb,
geometry/lib/sumi/geometry/equation.rb,
geometry/lib/sumi/geometry/predicate.rb

Overview

Immutable point in SVG/screen coordinates.

Use Point[x, y] to create a point from two coordinates. Public geometry methods that expect a point also accept [x, y]. Explicit Point values are most useful when a result will be transformed, compared, or reused.

Examples:

Measure and rotate a point in screen coordinates

point = Sumi::Geometry::Point[3, 4]
Sumi::Geometry::Point.length(Sumi::Geometry::Origin, point) # => 5.0
point.rotate(90).approx.deconstruct # => [-4.0, 3.0]

Compare positions in screen coordinates

upper = Sumi::Geometry::Point[4, 2]
lower = Sumi::Geometry::Point[4, 8]
upper.above?(lower) # => true
upper.left?([6, 2]) # => true

See Also:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x:, y:) ⇒ void

Creates a point.

Parameters:

  • x (Numeric) —

    x coordinate

  • y (Numeric) —

    y coordinate

Raises:



212
# File 'geometry/lib/sumi/geometry/point.rb', line 212

def initialize(x:, y:) = super(x: Real[:x, x], y: Real[:y, y])

Instance Attribute Details

#x ⇒ Float (readonly)

Returns x coordinate.

Returns:

  • (Float) —

    x coordinate



151
152
153
# File 'geometry/lib/sumi/geometry/point.rb', line 151

def x
  @x
end

#y ⇒ Float (readonly)

Returns y coordinate.

Returns:

  • (Float) —

    y coordinate



151
152
153
# File 'geometry/lib/sumi/geometry/point.rb', line 151

def y
  @y
end

Class Method Details

.[](x, y) ⇒ Sumi::Geometry::Point

Creates a point from two coordinates.

Examples:

Create a point with mathematical notation

Sumi::Geometry::Point[3, 5]

Parameters:

  • x (Numeric) —

    x coordinate

  • y (Numeric) —

    y coordinate

Returns:

Raises:



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'geometry/lib/sumi/geometry/point.rb', line 151

Point = Data.define(:x, :y) do
  include Comparable
  include Affinity

  # @!attribute [r] x
  #   @return [Float] x coordinate
  # @!attribute [r] y
  #   @return [Float] y coordinate

  # Returns the screen-space angle from one point to another.
  # @param starting [Sumi::Geometry::Point, Array<Numeric>] start point
  # @param ending [Sumi::Geometry::Point, Array<Numeric>] end point
  # @return [Float] clockwise angle in degrees
  # @raise [Sumi::Geometry::Error] when either point cannot be coerced
  def self.angle(starting, ending)
    starting, ending = Tuples[Point, starting, ending]
    F.atan2(ending.y - starting.y, ending.x - starting.x)
  end

  # Compares two points with optional numeric precision.
  # @param this [Sumi::Geometry::Point, Array<Numeric>] first point
  # @param that [Sumi::Geometry::Point, Array<Numeric>] second point
  # @param precision [Integer, nil] decimal precision, or nil for the current function default
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when either point cannot be coerced
  def self.eq?(this, that, precision: nil)
    this, that = Tuples[self, this, that]
    F.eq?(this.x, that.x, precision:) && F.eq?(this.y, that.y, precision:)
  end

  # Returns the Euclidean distance between two points.
  # @param starting [Sumi::Geometry::Point, Array<Numeric>] start point
  # @param ending [Sumi::Geometry::Point, Array<Numeric>] end point
  # @return [Float]
  # @raise [Sumi::Geometry::Error] when either point cannot be coerced
  def self.length(starting, ending)
    starting, ending = Tuples[Point, starting, ending]
    ::Math.hypot(starting.x - ending.x, starting.y - ending.y)
  end

  # Returns the midpoint between two points.
  # @param starting [Sumi::Geometry::Point, Array<Numeric>] first point
  # @param ending [Sumi::Geometry::Point, Array<Numeric>] second point
  # @return [Sumi::Geometry::Point] midpoint
  # @raise [Sumi::Geometry::Error] when either point cannot be coerced
  def self.midpoint(starting, ending)
    starting, ending = Tuples[Point, starting, ending]
    self[(starting.x / 2.0) + (ending.x / 2.0), (starting.y / 2.0) + (ending.y / 2.0)]
  end

  # Returns the origin point.
  # @return [Sumi::Geometry::Point]
  def self.origin
    new(x: 0.0, y: 0.0)
  end

  # Creates a point.
  # @param x [Numeric] x coordinate
  # @param y [Numeric] y coordinate
  # @return [void]
  # @raise [Sumi::Geometry::Error] when a coordinate is not a finite Numeric
  def initialize(x:, y:) = super(x: Real[:x, x], y: Real[:y, y])

  # Compares points by x, then y.
  # @param other [Object] point or two-item coordinate array to compare
  # @return [Integer, nil] comparison result, or nil when other is not a valid point value
  def <=>(other)
    deconstruct <=> Tuple[Point, other].deconstruct
  rescue Error
    nil
  end

  # Reports whether this point is at or above another point in screen coordinates.
  # @param other [Sumi::Geometry::Point, Array<Numeric>] point to compare
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when other cannot be coerced
  def above?(other) = y <= Tuple[Point, other].y

  # Returns a point rounded to precision.
  # @param precision [Integer, nil] decimal precision, or nil for the current function default
  # @return [Sumi::Geometry::Point]
  def approx(precision = nil) = with(x: F.approx(x, precision), y: F.approx(y, precision))

  # Reports whether this point is at or below another point in screen coordinates.
  # @param other [Sumi::Geometry::Point, Array<Numeric>] point to compare
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when other cannot be coerced
  def below?(other) = y >= Tuple[Point, other].y

  # Compares this point with optional numeric precision.
  # @param other [Sumi::Geometry::Point, Array<Numeric>] point to compare
  # @param precision [Integer, nil] decimal precision, or nil for the current function default
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when other cannot be coerced
  def eq?(other, precision: nil) = self.class.eq?(self, other, precision:)

  # Reports strict point equality.
  # @param other [Object] object to compare
  # @return [Boolean]
  def eql?(other) = self.class == other.class && deconstruct == other.deconstruct

  # Returns a hash compatible with strict equality.
  # @return [Integer]
  def hash = [self.class, *deconstruct].hash

  # Reports whether this point is at or left of another point.
  # @param other [Sumi::Geometry::Point, Array<Numeric>] point to compare
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when other cannot be coerced
  def left?(other) = x <= Tuple[Point, other].x

  # Reports whether this point is at or right of another point.
  # @param other [Sumi::Geometry::Point, Array<Numeric>] point to compare
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when other cannot be coerced
  def right?(other) = x >= Tuple[Point, other].x

  # Formats point coordinates for SVG coordinate-list attributes.
  # @return [String]
  def to_cs = "#{F.approx(x)},#{F.approx(y)}"

  # Formats the point for display.
  # @return [String]
  def to_s = "(#{to_cs})"

  alias_method :==, :eql?
end

.angle(starting, ending) ⇒ Float

Returns the screen-space angle from one point to another.

Parameters:

Returns:

  • (Float) —

    clockwise angle in degrees

Raises:



165
166
167
168
# File 'geometry/lib/sumi/geometry/point.rb', line 165

def self.angle(starting, ending)
  starting, ending = Tuples[Point, starting, ending]
  F.atan2(ending.y - starting.y, ending.x - starting.x)
end

.collinear?(*points, precision: nil) ⇒ Boolean

Reports whether three or more points lie on one infinite line.

Every three-point subset must have a cross product that rounds to zero at the selected decimal precision. The cross-product magnitude is twice the triangle area, not a distance tolerance. Repeated points are allowed. Input order does not affect the result, and accepted sets have accepted subsets. Triangle area is invariant under rigid motion, subject to floating-point error near the rounding threshold. The worst-case number of comparisons is cubic in the point count.

Examples:

Test a point set

Sumi::Geometry::Point.collinear?([0, 0], [1, 1], [2, 2]) # => true

Parameters:

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

    point-like values

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

    decimal precision, or nil for the current function default

Returns:

  • (Boolean)

Raises:



139
140
141
142
143
# File 'geometry/lib/sumi/geometry/predicate.rb', line 139

def self.collinear?(*points, precision: nil)
  ArgumentError.("At least three points required") if points.size < 3

  Predicate.collinear?(Tuples[self, *points], precision:)
end

.eq?(this, that, precision: nil) ⇒ Boolean

Compares two points with optional numeric precision.

Parameters:

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

    first point

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

    second point

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

    decimal precision, or nil for the current function default

Returns:

  • (Boolean)

Raises:



176
177
178
179
# File 'geometry/lib/sumi/geometry/point.rb', line 176

def self.eq?(this, that, precision: nil)
  this, that = Tuples[self, this, that]
  F.eq?(this.x, that.x, precision:) && F.eq?(this.y, that.y, precision:)
end

.length(starting, ending) ⇒ Float

Returns the Euclidean distance between two points.

Parameters:

Returns:

  • (Float)

Raises:



186
187
188
189
# File 'geometry/lib/sumi/geometry/point.rb', line 186

def self.length(starting, ending)
  starting, ending = Tuples[Point, starting, ending]
  ::Math.hypot(starting.x - ending.x, starting.y - ending.y)
end

.midpoint(starting, ending) ⇒ Sumi::Geometry::Point

Returns the midpoint between two points.

Parameters:

Returns:

Raises:



196
197
198
199
# File 'geometry/lib/sumi/geometry/point.rb', line 196

def self.midpoint(starting, ending)
  starting, ending = Tuples[Point, starting, ending]
  self[(starting.x / 2.0) + (ending.x / 2.0), (starting.y / 2.0) + (ending.y / 2.0)]
end

.origin ⇒ Sumi::Geometry::Point

Returns the origin point.



203
204
205
# File 'geometry/lib/sumi/geometry/point.rb', line 203

def self.origin
  new(x: 0.0, y: 0.0)
end

Instance Method Details

#<=>(other) ⇒ Integer?

Compares points by x, then y.

Parameters:

  • other (Object) —

    point or two-item coordinate array to compare

Returns:

  • (Integer, nil) —

    comparison result, or nil when other is not a valid point value



217
218
219
220
221
# File 'geometry/lib/sumi/geometry/point.rb', line 217

def <=>(other)
  deconstruct <=> Tuple[Point, other].deconstruct
rescue Error
  nil
end

#above?(other) ⇒ Boolean

Reports whether this point is at or above another point in screen coordinates.

Parameters:

Returns:

  • (Boolean)

Raises:



227
# File 'geometry/lib/sumi/geometry/point.rb', line 227

def above?(other) = y <= Tuple[Point, other].y

#approx(precision = nil) ⇒ Sumi::Geometry::Point

Returns a point rounded to precision.

Parameters:

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

    decimal precision, or nil for the current function default

Returns:



232
# File 'geometry/lib/sumi/geometry/point.rb', line 232

def approx(precision = nil) = with(x: F.approx(x, precision), y: F.approx(y, precision))

#below?(other) ⇒ Boolean

Reports whether this point is at or below another point in screen coordinates.

Parameters:

Returns:

  • (Boolean)

Raises:



238
# File 'geometry/lib/sumi/geometry/point.rb', line 238

def below?(other) = y >= Tuple[Point, other].y

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

Compares this point with optional numeric precision.

Parameters:

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

    point to compare

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

    decimal precision, or nil for the current function default

Returns:

  • (Boolean)

Raises:



245
# File 'geometry/lib/sumi/geometry/point.rb', line 245

def eq?(other, precision: nil) = self.class.eq?(self, other, precision:)

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

Reports strict point equality.

Parameters:

  • other (Object) —

    object to compare

Returns:

  • (Boolean)


250
# File 'geometry/lib/sumi/geometry/point.rb', line 250

def eql?(other) = self.class == other.class && deconstruct == other.deconstruct

#equation(angle) ⇒ Sumi::Geometry::Equation::Linear

Returns the linear equation passing through this point at an angle.

Examples:

Create axis-aligned equations through a point

point = Sumi::Geometry::Point[4, 3]
point.equation(0).y(20)  # => 3.0
point.equation(90).x(20) # => 4.0

Parameters:

  • angle (Numeric) —

    clockwise angle in degrees

Returns:

Raises:



130
131
132
133
134
135
136
137
# File 'geometry/lib/sumi/geometry/equation.rb', line 130

def equation(angle)
  angle = Real[:angle, angle]
  return Equation.horizontal(y) if (angle % 180.0).zero?
  return Equation.vertical(x) if (angle % 90.0).zero?

  slope = F.tan(angle)
  Equation::Linear::Diagonal.new(slope:, intercept: y - (slope * x), origin: self)
end

#hash ⇒ Integer

Returns a hash compatible with strict equality.

Returns:

  • (Integer)


254
# File 'geometry/lib/sumi/geometry/point.rb', line 254

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

#left?(other) ⇒ Boolean

Reports whether this point is at or left of another point.

Parameters:

Returns:

  • (Boolean)

Raises:



260
# File 'geometry/lib/sumi/geometry/point.rb', line 260

def left?(other) = x <= Tuple[Point, other].x

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

Returns a point 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:



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'geometry/lib/sumi/geometry/point.rb', line 151

Point = Data.define(:x, :y) do
  include Comparable
  include Affinity

  # @!attribute [r] x
  #   @return [Float] x coordinate
  # @!attribute [r] y
  #   @return [Float] y coordinate

  # Returns the screen-space angle from one point to another.
  # @param starting [Sumi::Geometry::Point, Array<Numeric>] start point
  # @param ending [Sumi::Geometry::Point, Array<Numeric>] end point
  # @return [Float] clockwise angle in degrees
  # @raise [Sumi::Geometry::Error] when either point cannot be coerced
  def self.angle(starting, ending)
    starting, ending = Tuples[Point, starting, ending]
    F.atan2(ending.y - starting.y, ending.x - starting.x)
  end

  # Compares two points with optional numeric precision.
  # @param this [Sumi::Geometry::Point, Array<Numeric>] first point
  # @param that [Sumi::Geometry::Point, Array<Numeric>] second point
  # @param precision [Integer, nil] decimal precision, or nil for the current function default
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when either point cannot be coerced
  def self.eq?(this, that, precision: nil)
    this, that = Tuples[self, this, that]
    F.eq?(this.x, that.x, precision:) && F.eq?(this.y, that.y, precision:)
  end

  # Returns the Euclidean distance between two points.
  # @param starting [Sumi::Geometry::Point, Array<Numeric>] start point
  # @param ending [Sumi::Geometry::Point, Array<Numeric>] end point
  # @return [Float]
  # @raise [Sumi::Geometry::Error] when either point cannot be coerced
  def self.length(starting, ending)
    starting, ending = Tuples[Point, starting, ending]
    ::Math.hypot(starting.x - ending.x, starting.y - ending.y)
  end

  # Returns the midpoint between two points.
  # @param starting [Sumi::Geometry::Point, Array<Numeric>] first point
  # @param ending [Sumi::Geometry::Point, Array<Numeric>] second point
  # @return [Sumi::Geometry::Point] midpoint
  # @raise [Sumi::Geometry::Error] when either point cannot be coerced
  def self.midpoint(starting, ending)
    starting, ending = Tuples[Point, starting, ending]
    self[(starting.x / 2.0) + (ending.x / 2.0), (starting.y / 2.0) + (ending.y / 2.0)]
  end

  # Returns the origin point.
  # @return [Sumi::Geometry::Point]
  def self.origin
    new(x: 0.0, y: 0.0)
  end

  # Creates a point.
  # @param x [Numeric] x coordinate
  # @param y [Numeric] y coordinate
  # @return [void]
  # @raise [Sumi::Geometry::Error] when a coordinate is not a finite Numeric
  def initialize(x:, y:) = super(x: Real[:x, x], y: Real[:y, y])

  # Compares points by x, then y.
  # @param other [Object] point or two-item coordinate array to compare
  # @return [Integer, nil] comparison result, or nil when other is not a valid point value
  def <=>(other)
    deconstruct <=> Tuple[Point, other].deconstruct
  rescue Error
    nil
  end

  # Reports whether this point is at or above another point in screen coordinates.
  # @param other [Sumi::Geometry::Point, Array<Numeric>] point to compare
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when other cannot be coerced
  def above?(other) = y <= Tuple[Point, other].y

  # Returns a point rounded to precision.
  # @param precision [Integer, nil] decimal precision, or nil for the current function default
  # @return [Sumi::Geometry::Point]
  def approx(precision = nil) = with(x: F.approx(x, precision), y: F.approx(y, precision))

  # Reports whether this point is at or below another point in screen coordinates.
  # @param other [Sumi::Geometry::Point, Array<Numeric>] point to compare
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when other cannot be coerced
  def below?(other) = y >= Tuple[Point, other].y

  # Compares this point with optional numeric precision.
  # @param other [Sumi::Geometry::Point, Array<Numeric>] point to compare
  # @param precision [Integer, nil] decimal precision, or nil for the current function default
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when other cannot be coerced
  def eq?(other, precision: nil) = self.class.eq?(self, other, precision:)

  # Reports strict point equality.
  # @param other [Object] object to compare
  # @return [Boolean]
  def eql?(other) = self.class == other.class && deconstruct == other.deconstruct

  # Returns a hash compatible with strict equality.
  # @return [Integer]
  def hash = [self.class, *deconstruct].hash

  # Reports whether this point is at or left of another point.
  # @param other [Sumi::Geometry::Point, Array<Numeric>] point to compare
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when other cannot be coerced
  def left?(other) = x <= Tuple[Point, other].x

  # Reports whether this point is at or right of another point.
  # @param other [Sumi::Geometry::Point, Array<Numeric>] point to compare
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when other cannot be coerced
  def right?(other) = x >= Tuple[Point, other].x

  # Formats point coordinates for SVG coordinate-list attributes.
  # @return [String]
  def to_cs = "#{F.approx(x)},#{F.approx(y)}"

  # Formats the point for display.
  # @return [String]
  def to_s = "(#{to_cs})"

  alias_method :==, :eql?
end

#right?(other) ⇒ Boolean

Reports whether this point is at or right of another point.

Parameters:

Returns:

  • (Boolean)

Raises:



266
# File 'geometry/lib/sumi/geometry/point.rb', line 266

def right?(other) = x >= Tuple[Point, other].x

#rotate(a) ⇒ Sumi::Geometry::Point

Returns a point rotated around the origin.

Parameters:

  • a (Numeric) —

    clockwise angle in degrees

Returns:

Raises:



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'geometry/lib/sumi/geometry/point.rb', line 151

Point = Data.define(:x, :y) do
  include Comparable
  include Affinity

  # @!attribute [r] x
  #   @return [Float] x coordinate
  # @!attribute [r] y
  #   @return [Float] y coordinate

  # Returns the screen-space angle from one point to another.
  # @param starting [Sumi::Geometry::Point, Array<Numeric>] start point
  # @param ending [Sumi::Geometry::Point, Array<Numeric>] end point
  # @return [Float] clockwise angle in degrees
  # @raise [Sumi::Geometry::Error] when either point cannot be coerced
  def self.angle(starting, ending)
    starting, ending = Tuples[Point, starting, ending]
    F.atan2(ending.y - starting.y, ending.x - starting.x)
  end

  # Compares two points with optional numeric precision.
  # @param this [Sumi::Geometry::Point, Array<Numeric>] first point
  # @param that [Sumi::Geometry::Point, Array<Numeric>] second point
  # @param precision [Integer, nil] decimal precision, or nil for the current function default
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when either point cannot be coerced
  def self.eq?(this, that, precision: nil)
    this, that = Tuples[self, this, that]
    F.eq?(this.x, that.x, precision:) && F.eq?(this.y, that.y, precision:)
  end

  # Returns the Euclidean distance between two points.
  # @param starting [Sumi::Geometry::Point, Array<Numeric>] start point
  # @param ending [Sumi::Geometry::Point, Array<Numeric>] end point
  # @return [Float]
  # @raise [Sumi::Geometry::Error] when either point cannot be coerced
  def self.length(starting, ending)
    starting, ending = Tuples[Point, starting, ending]
    ::Math.hypot(starting.x - ending.x, starting.y - ending.y)
  end

  # Returns the midpoint between two points.
  # @param starting [Sumi::Geometry::Point, Array<Numeric>] first point
  # @param ending [Sumi::Geometry::Point, Array<Numeric>] second point
  # @return [Sumi::Geometry::Point] midpoint
  # @raise [Sumi::Geometry::Error] when either point cannot be coerced
  def self.midpoint(starting, ending)
    starting, ending = Tuples[Point, starting, ending]
    self[(starting.x / 2.0) + (ending.x / 2.0), (starting.y / 2.0) + (ending.y / 2.0)]
  end

  # Returns the origin point.
  # @return [Sumi::Geometry::Point]
  def self.origin
    new(x: 0.0, y: 0.0)
  end

  # Creates a point.
  # @param x [Numeric] x coordinate
  # @param y [Numeric] y coordinate
  # @return [void]
  # @raise [Sumi::Geometry::Error] when a coordinate is not a finite Numeric
  def initialize(x:, y:) = super(x: Real[:x, x], y: Real[:y, y])

  # Compares points by x, then y.
  # @param other [Object] point or two-item coordinate array to compare
  # @return [Integer, nil] comparison result, or nil when other is not a valid point value
  def <=>(other)
    deconstruct <=> Tuple[Point, other].deconstruct
  rescue Error
    nil
  end

  # Reports whether this point is at or above another point in screen coordinates.
  # @param other [Sumi::Geometry::Point, Array<Numeric>] point to compare
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when other cannot be coerced
  def above?(other) = y <= Tuple[Point, other].y

  # Returns a point rounded to precision.
  # @param precision [Integer, nil] decimal precision, or nil for the current function default
  # @return [Sumi::Geometry::Point]
  def approx(precision = nil) = with(x: F.approx(x, precision), y: F.approx(y, precision))

  # Reports whether this point is at or below another point in screen coordinates.
  # @param other [Sumi::Geometry::Point, Array<Numeric>] point to compare
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when other cannot be coerced
  def below?(other) = y >= Tuple[Point, other].y

  # Compares this point with optional numeric precision.
  # @param other [Sumi::Geometry::Point, Array<Numeric>] point to compare
  # @param precision [Integer, nil] decimal precision, or nil for the current function default
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when other cannot be coerced
  def eq?(other, precision: nil) = self.class.eq?(self, other, precision:)

  # Reports strict point equality.
  # @param other [Object] object to compare
  # @return [Boolean]
  def eql?(other) = self.class == other.class && deconstruct == other.deconstruct

  # Returns a hash compatible with strict equality.
  # @return [Integer]
  def hash = [self.class, *deconstruct].hash

  # Reports whether this point is at or left of another point.
  # @param other [Sumi::Geometry::Point, Array<Numeric>] point to compare
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when other cannot be coerced
  def left?(other) = x <= Tuple[Point, other].x

  # Reports whether this point is at or right of another point.
  # @param other [Sumi::Geometry::Point, Array<Numeric>] point to compare
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when other cannot be coerced
  def right?(other) = x >= Tuple[Point, other].x

  # Formats point coordinates for SVG coordinate-list attributes.
  # @return [String]
  def to_cs = "#{F.approx(x)},#{F.approx(y)}"

  # Formats the point for display.
  # @return [String]
  def to_s = "(#{to_cs})"

  alias_method :==, :eql?
end

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

Returns a point 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:



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'geometry/lib/sumi/geometry/point.rb', line 151

Point = Data.define(:x, :y) do
  include Comparable
  include Affinity

  # @!attribute [r] x
  #   @return [Float] x coordinate
  # @!attribute [r] y
  #   @return [Float] y coordinate

  # Returns the screen-space angle from one point to another.
  # @param starting [Sumi::Geometry::Point, Array<Numeric>] start point
  # @param ending [Sumi::Geometry::Point, Array<Numeric>] end point
  # @return [Float] clockwise angle in degrees
  # @raise [Sumi::Geometry::Error] when either point cannot be coerced
  def self.angle(starting, ending)
    starting, ending = Tuples[Point, starting, ending]
    F.atan2(ending.y - starting.y, ending.x - starting.x)
  end

  # Compares two points with optional numeric precision.
  # @param this [Sumi::Geometry::Point, Array<Numeric>] first point
  # @param that [Sumi::Geometry::Point, Array<Numeric>] second point
  # @param precision [Integer, nil] decimal precision, or nil for the current function default
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when either point cannot be coerced
  def self.eq?(this, that, precision: nil)
    this, that = Tuples[self, this, that]
    F.eq?(this.x, that.x, precision:) && F.eq?(this.y, that.y, precision:)
  end

  # Returns the Euclidean distance between two points.
  # @param starting [Sumi::Geometry::Point, Array<Numeric>] start point
  # @param ending [Sumi::Geometry::Point, Array<Numeric>] end point
  # @return [Float]
  # @raise [Sumi::Geometry::Error] when either point cannot be coerced
  def self.length(starting, ending)
    starting, ending = Tuples[Point, starting, ending]
    ::Math.hypot(starting.x - ending.x, starting.y - ending.y)
  end

  # Returns the midpoint between two points.
  # @param starting [Sumi::Geometry::Point, Array<Numeric>] first point
  # @param ending [Sumi::Geometry::Point, Array<Numeric>] second point
  # @return [Sumi::Geometry::Point] midpoint
  # @raise [Sumi::Geometry::Error] when either point cannot be coerced
  def self.midpoint(starting, ending)
    starting, ending = Tuples[Point, starting, ending]
    self[(starting.x / 2.0) + (ending.x / 2.0), (starting.y / 2.0) + (ending.y / 2.0)]
  end

  # Returns the origin point.
  # @return [Sumi::Geometry::Point]
  def self.origin
    new(x: 0.0, y: 0.0)
  end

  # Creates a point.
  # @param x [Numeric] x coordinate
  # @param y [Numeric] y coordinate
  # @return [void]
  # @raise [Sumi::Geometry::Error] when a coordinate is not a finite Numeric
  def initialize(x:, y:) = super(x: Real[:x, x], y: Real[:y, y])

  # Compares points by x, then y.
  # @param other [Object] point or two-item coordinate array to compare
  # @return [Integer, nil] comparison result, or nil when other is not a valid point value
  def <=>(other)
    deconstruct <=> Tuple[Point, other].deconstruct
  rescue Error
    nil
  end

  # Reports whether this point is at or above another point in screen coordinates.
  # @param other [Sumi::Geometry::Point, Array<Numeric>] point to compare
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when other cannot be coerced
  def above?(other) = y <= Tuple[Point, other].y

  # Returns a point rounded to precision.
  # @param precision [Integer, nil] decimal precision, or nil for the current function default
  # @return [Sumi::Geometry::Point]
  def approx(precision = nil) = with(x: F.approx(x, precision), y: F.approx(y, precision))

  # Reports whether this point is at or below another point in screen coordinates.
  # @param other [Sumi::Geometry::Point, Array<Numeric>] point to compare
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when other cannot be coerced
  def below?(other) = y >= Tuple[Point, other].y

  # Compares this point with optional numeric precision.
  # @param other [Sumi::Geometry::Point, Array<Numeric>] point to compare
  # @param precision [Integer, nil] decimal precision, or nil for the current function default
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when other cannot be coerced
  def eq?(other, precision: nil) = self.class.eq?(self, other, precision:)

  # Reports strict point equality.
  # @param other [Object] object to compare
  # @return [Boolean]
  def eql?(other) = self.class == other.class && deconstruct == other.deconstruct

  # Returns a hash compatible with strict equality.
  # @return [Integer]
  def hash = [self.class, *deconstruct].hash

  # Reports whether this point is at or left of another point.
  # @param other [Sumi::Geometry::Point, Array<Numeric>] point to compare
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when other cannot be coerced
  def left?(other) = x <= Tuple[Point, other].x

  # Reports whether this point is at or right of another point.
  # @param other [Sumi::Geometry::Point, Array<Numeric>] point to compare
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when other cannot be coerced
  def right?(other) = x >= Tuple[Point, other].x

  # Formats point coordinates for SVG coordinate-list attributes.
  # @return [String]
  def to_cs = "#{F.approx(x)},#{F.approx(y)}"

  # Formats the point for display.
  # @return [String]
  def to_s = "(#{to_cs})"

  alias_method :==, :eql?
end

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

Returns a point 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:



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'geometry/lib/sumi/geometry/point.rb', line 151

Point = Data.define(:x, :y) do
  include Comparable
  include Affinity

  # @!attribute [r] x
  #   @return [Float] x coordinate
  # @!attribute [r] y
  #   @return [Float] y coordinate

  # Returns the screen-space angle from one point to another.
  # @param starting [Sumi::Geometry::Point, Array<Numeric>] start point
  # @param ending [Sumi::Geometry::Point, Array<Numeric>] end point
  # @return [Float] clockwise angle in degrees
  # @raise [Sumi::Geometry::Error] when either point cannot be coerced
  def self.angle(starting, ending)
    starting, ending = Tuples[Point, starting, ending]
    F.atan2(ending.y - starting.y, ending.x - starting.x)
  end

  # Compares two points with optional numeric precision.
  # @param this [Sumi::Geometry::Point, Array<Numeric>] first point
  # @param that [Sumi::Geometry::Point, Array<Numeric>] second point
  # @param precision [Integer, nil] decimal precision, or nil for the current function default
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when either point cannot be coerced
  def self.eq?(this, that, precision: nil)
    this, that = Tuples[self, this, that]
    F.eq?(this.x, that.x, precision:) && F.eq?(this.y, that.y, precision:)
  end

  # Returns the Euclidean distance between two points.
  # @param starting [Sumi::Geometry::Point, Array<Numeric>] start point
  # @param ending [Sumi::Geometry::Point, Array<Numeric>] end point
  # @return [Float]
  # @raise [Sumi::Geometry::Error] when either point cannot be coerced
  def self.length(starting, ending)
    starting, ending = Tuples[Point, starting, ending]
    ::Math.hypot(starting.x - ending.x, starting.y - ending.y)
  end

  # Returns the midpoint between two points.
  # @param starting [Sumi::Geometry::Point, Array<Numeric>] first point
  # @param ending [Sumi::Geometry::Point, Array<Numeric>] second point
  # @return [Sumi::Geometry::Point] midpoint
  # @raise [Sumi::Geometry::Error] when either point cannot be coerced
  def self.midpoint(starting, ending)
    starting, ending = Tuples[Point, starting, ending]
    self[(starting.x / 2.0) + (ending.x / 2.0), (starting.y / 2.0) + (ending.y / 2.0)]
  end

  # Returns the origin point.
  # @return [Sumi::Geometry::Point]
  def self.origin
    new(x: 0.0, y: 0.0)
  end

  # Creates a point.
  # @param x [Numeric] x coordinate
  # @param y [Numeric] y coordinate
  # @return [void]
  # @raise [Sumi::Geometry::Error] when a coordinate is not a finite Numeric
  def initialize(x:, y:) = super(x: Real[:x, x], y: Real[:y, y])

  # Compares points by x, then y.
  # @param other [Object] point or two-item coordinate array to compare
  # @return [Integer, nil] comparison result, or nil when other is not a valid point value
  def <=>(other)
    deconstruct <=> Tuple[Point, other].deconstruct
  rescue Error
    nil
  end

  # Reports whether this point is at or above another point in screen coordinates.
  # @param other [Sumi::Geometry::Point, Array<Numeric>] point to compare
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when other cannot be coerced
  def above?(other) = y <= Tuple[Point, other].y

  # Returns a point rounded to precision.
  # @param precision [Integer, nil] decimal precision, or nil for the current function default
  # @return [Sumi::Geometry::Point]
  def approx(precision = nil) = with(x: F.approx(x, precision), y: F.approx(y, precision))

  # Reports whether this point is at or below another point in screen coordinates.
  # @param other [Sumi::Geometry::Point, Array<Numeric>] point to compare
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when other cannot be coerced
  def below?(other) = y >= Tuple[Point, other].y

  # Compares this point with optional numeric precision.
  # @param other [Sumi::Geometry::Point, Array<Numeric>] point to compare
  # @param precision [Integer, nil] decimal precision, or nil for the current function default
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when other cannot be coerced
  def eq?(other, precision: nil) = self.class.eq?(self, other, precision:)

  # Reports strict point equality.
  # @param other [Object] object to compare
  # @return [Boolean]
  def eql?(other) = self.class == other.class && deconstruct == other.deconstruct

  # Returns a hash compatible with strict equality.
  # @return [Integer]
  def hash = [self.class, *deconstruct].hash

  # Reports whether this point is at or left of another point.
  # @param other [Sumi::Geometry::Point, Array<Numeric>] point to compare
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when other cannot be coerced
  def left?(other) = x <= Tuple[Point, other].x

  # Reports whether this point is at or right of another point.
  # @param other [Sumi::Geometry::Point, Array<Numeric>] point to compare
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when other cannot be coerced
  def right?(other) = x >= Tuple[Point, other].x

  # Formats point coordinates for SVG coordinate-list attributes.
  # @return [String]
  def to_cs = "#{F.approx(x)},#{F.approx(y)}"

  # Formats the point for display.
  # @return [String]
  def to_s = "(#{to_cs})"

  alias_method :==, :eql?
end

#skew_x(a) ⇒ Sumi::Geometry::Point

Returns a point skewed along x.

Parameters:

  • a (Numeric) —

    skew angle in degrees

Returns:

Raises:



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'geometry/lib/sumi/geometry/point.rb', line 151

Point = Data.define(:x, :y) do
  include Comparable
  include Affinity

  # @!attribute [r] x
  #   @return [Float] x coordinate
  # @!attribute [r] y
  #   @return [Float] y coordinate

  # Returns the screen-space angle from one point to another.
  # @param starting [Sumi::Geometry::Point, Array<Numeric>] start point
  # @param ending [Sumi::Geometry::Point, Array<Numeric>] end point
  # @return [Float] clockwise angle in degrees
  # @raise [Sumi::Geometry::Error] when either point cannot be coerced
  def self.angle(starting, ending)
    starting, ending = Tuples[Point, starting, ending]
    F.atan2(ending.y - starting.y, ending.x - starting.x)
  end

  # Compares two points with optional numeric precision.
  # @param this [Sumi::Geometry::Point, Array<Numeric>] first point
  # @param that [Sumi::Geometry::Point, Array<Numeric>] second point
  # @param precision [Integer, nil] decimal precision, or nil for the current function default
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when either point cannot be coerced
  def self.eq?(this, that, precision: nil)
    this, that = Tuples[self, this, that]
    F.eq?(this.x, that.x, precision:) && F.eq?(this.y, that.y, precision:)
  end

  # Returns the Euclidean distance between two points.
  # @param starting [Sumi::Geometry::Point, Array<Numeric>] start point
  # @param ending [Sumi::Geometry::Point, Array<Numeric>] end point
  # @return [Float]
  # @raise [Sumi::Geometry::Error] when either point cannot be coerced
  def self.length(starting, ending)
    starting, ending = Tuples[Point, starting, ending]
    ::Math.hypot(starting.x - ending.x, starting.y - ending.y)
  end

  # Returns the midpoint between two points.
  # @param starting [Sumi::Geometry::Point, Array<Numeric>] first point
  # @param ending [Sumi::Geometry::Point, Array<Numeric>] second point
  # @return [Sumi::Geometry::Point] midpoint
  # @raise [Sumi::Geometry::Error] when either point cannot be coerced
  def self.midpoint(starting, ending)
    starting, ending = Tuples[Point, starting, ending]
    self[(starting.x / 2.0) + (ending.x / 2.0), (starting.y / 2.0) + (ending.y / 2.0)]
  end

  # Returns the origin point.
  # @return [Sumi::Geometry::Point]
  def self.origin
    new(x: 0.0, y: 0.0)
  end

  # Creates a point.
  # @param x [Numeric] x coordinate
  # @param y [Numeric] y coordinate
  # @return [void]
  # @raise [Sumi::Geometry::Error] when a coordinate is not a finite Numeric
  def initialize(x:, y:) = super(x: Real[:x, x], y: Real[:y, y])

  # Compares points by x, then y.
  # @param other [Object] point or two-item coordinate array to compare
  # @return [Integer, nil] comparison result, or nil when other is not a valid point value
  def <=>(other)
    deconstruct <=> Tuple[Point, other].deconstruct
  rescue Error
    nil
  end

  # Reports whether this point is at or above another point in screen coordinates.
  # @param other [Sumi::Geometry::Point, Array<Numeric>] point to compare
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when other cannot be coerced
  def above?(other) = y <= Tuple[Point, other].y

  # Returns a point rounded to precision.
  # @param precision [Integer, nil] decimal precision, or nil for the current function default
  # @return [Sumi::Geometry::Point]
  def approx(precision = nil) = with(x: F.approx(x, precision), y: F.approx(y, precision))

  # Reports whether this point is at or below another point in screen coordinates.
  # @param other [Sumi::Geometry::Point, Array<Numeric>] point to compare
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when other cannot be coerced
  def below?(other) = y >= Tuple[Point, other].y

  # Compares this point with optional numeric precision.
  # @param other [Sumi::Geometry::Point, Array<Numeric>] point to compare
  # @param precision [Integer, nil] decimal precision, or nil for the current function default
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when other cannot be coerced
  def eq?(other, precision: nil) = self.class.eq?(self, other, precision:)

  # Reports strict point equality.
  # @param other [Object] object to compare
  # @return [Boolean]
  def eql?(other) = self.class == other.class && deconstruct == other.deconstruct

  # Returns a hash compatible with strict equality.
  # @return [Integer]
  def hash = [self.class, *deconstruct].hash

  # Reports whether this point is at or left of another point.
  # @param other [Sumi::Geometry::Point, Array<Numeric>] point to compare
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when other cannot be coerced
  def left?(other) = x <= Tuple[Point, other].x

  # Reports whether this point is at or right of another point.
  # @param other [Sumi::Geometry::Point, Array<Numeric>] point to compare
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when other cannot be coerced
  def right?(other) = x >= Tuple[Point, other].x

  # Formats point coordinates for SVG coordinate-list attributes.
  # @return [String]
  def to_cs = "#{F.approx(x)},#{F.approx(y)}"

  # Formats the point for display.
  # @return [String]
  def to_s = "(#{to_cs})"

  alias_method :==, :eql?
end

#skew_y(a) ⇒ Sumi::Geometry::Point

Returns a point skewed along y.

Parameters:

  • a (Numeric) —

    skew angle in degrees

Returns:

Raises:



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'geometry/lib/sumi/geometry/point.rb', line 151

Point = Data.define(:x, :y) do
  include Comparable
  include Affinity

  # @!attribute [r] x
  #   @return [Float] x coordinate
  # @!attribute [r] y
  #   @return [Float] y coordinate

  # Returns the screen-space angle from one point to another.
  # @param starting [Sumi::Geometry::Point, Array<Numeric>] start point
  # @param ending [Sumi::Geometry::Point, Array<Numeric>] end point
  # @return [Float] clockwise angle in degrees
  # @raise [Sumi::Geometry::Error] when either point cannot be coerced
  def self.angle(starting, ending)
    starting, ending = Tuples[Point, starting, ending]
    F.atan2(ending.y - starting.y, ending.x - starting.x)
  end

  # Compares two points with optional numeric precision.
  # @param this [Sumi::Geometry::Point, Array<Numeric>] first point
  # @param that [Sumi::Geometry::Point, Array<Numeric>] second point
  # @param precision [Integer, nil] decimal precision, or nil for the current function default
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when either point cannot be coerced
  def self.eq?(this, that, precision: nil)
    this, that = Tuples[self, this, that]
    F.eq?(this.x, that.x, precision:) && F.eq?(this.y, that.y, precision:)
  end

  # Returns the Euclidean distance between two points.
  # @param starting [Sumi::Geometry::Point, Array<Numeric>] start point
  # @param ending [Sumi::Geometry::Point, Array<Numeric>] end point
  # @return [Float]
  # @raise [Sumi::Geometry::Error] when either point cannot be coerced
  def self.length(starting, ending)
    starting, ending = Tuples[Point, starting, ending]
    ::Math.hypot(starting.x - ending.x, starting.y - ending.y)
  end

  # Returns the midpoint between two points.
  # @param starting [Sumi::Geometry::Point, Array<Numeric>] first point
  # @param ending [Sumi::Geometry::Point, Array<Numeric>] second point
  # @return [Sumi::Geometry::Point] midpoint
  # @raise [Sumi::Geometry::Error] when either point cannot be coerced
  def self.midpoint(starting, ending)
    starting, ending = Tuples[Point, starting, ending]
    self[(starting.x / 2.0) + (ending.x / 2.0), (starting.y / 2.0) + (ending.y / 2.0)]
  end

  # Returns the origin point.
  # @return [Sumi::Geometry::Point]
  def self.origin
    new(x: 0.0, y: 0.0)
  end

  # Creates a point.
  # @param x [Numeric] x coordinate
  # @param y [Numeric] y coordinate
  # @return [void]
  # @raise [Sumi::Geometry::Error] when a coordinate is not a finite Numeric
  def initialize(x:, y:) = super(x: Real[:x, x], y: Real[:y, y])

  # Compares points by x, then y.
  # @param other [Object] point or two-item coordinate array to compare
  # @return [Integer, nil] comparison result, or nil when other is not a valid point value
  def <=>(other)
    deconstruct <=> Tuple[Point, other].deconstruct
  rescue Error
    nil
  end

  # Reports whether this point is at or above another point in screen coordinates.
  # @param other [Sumi::Geometry::Point, Array<Numeric>] point to compare
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when other cannot be coerced
  def above?(other) = y <= Tuple[Point, other].y

  # Returns a point rounded to precision.
  # @param precision [Integer, nil] decimal precision, or nil for the current function default
  # @return [Sumi::Geometry::Point]
  def approx(precision = nil) = with(x: F.approx(x, precision), y: F.approx(y, precision))

  # Reports whether this point is at or below another point in screen coordinates.
  # @param other [Sumi::Geometry::Point, Array<Numeric>] point to compare
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when other cannot be coerced
  def below?(other) = y >= Tuple[Point, other].y

  # Compares this point with optional numeric precision.
  # @param other [Sumi::Geometry::Point, Array<Numeric>] point to compare
  # @param precision [Integer, nil] decimal precision, or nil for the current function default
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when other cannot be coerced
  def eq?(other, precision: nil) = self.class.eq?(self, other, precision:)

  # Reports strict point equality.
  # @param other [Object] object to compare
  # @return [Boolean]
  def eql?(other) = self.class == other.class && deconstruct == other.deconstruct

  # Returns a hash compatible with strict equality.
  # @return [Integer]
  def hash = [self.class, *deconstruct].hash

  # Reports whether this point is at or left of another point.
  # @param other [Sumi::Geometry::Point, Array<Numeric>] point to compare
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when other cannot be coerced
  def left?(other) = x <= Tuple[Point, other].x

  # Reports whether this point is at or right of another point.
  # @param other [Sumi::Geometry::Point, Array<Numeric>] point to compare
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when other cannot be coerced
  def right?(other) = x >= Tuple[Point, other].x

  # Formats point coordinates for SVG coordinate-list attributes.
  # @return [String]
  def to_cs = "#{F.approx(x)},#{F.approx(y)}"

  # Formats the point for display.
  # @return [String]
  def to_s = "(#{to_cs})"

  alias_method :==, :eql?
end

#to_cs ⇒ String

Formats point coordinates for SVG coordinate-list attributes.

Returns:

  • (String)


270
# File 'geometry/lib/sumi/geometry/point.rb', line 270

def to_cs = "#{F.approx(x)},#{F.approx(y)}"

#to_s ⇒ String

Formats the point for display.

Returns:

  • (String)


274
# File 'geometry/lib/sumi/geometry/point.rb', line 274

def to_s = "(#{to_cs})"

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

Returns a translated point.

Parameters:

  • dx (Numeric) —

    x offset

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

    y offset, defaulting to dx

Returns:

Raises:



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'geometry/lib/sumi/geometry/point.rb', line 151

Point = Data.define(:x, :y) do
  include Comparable
  include Affinity

  # @!attribute [r] x
  #   @return [Float] x coordinate
  # @!attribute [r] y
  #   @return [Float] y coordinate

  # Returns the screen-space angle from one point to another.
  # @param starting [Sumi::Geometry::Point, Array<Numeric>] start point
  # @param ending [Sumi::Geometry::Point, Array<Numeric>] end point
  # @return [Float] clockwise angle in degrees
  # @raise [Sumi::Geometry::Error] when either point cannot be coerced
  def self.angle(starting, ending)
    starting, ending = Tuples[Point, starting, ending]
    F.atan2(ending.y - starting.y, ending.x - starting.x)
  end

  # Compares two points with optional numeric precision.
  # @param this [Sumi::Geometry::Point, Array<Numeric>] first point
  # @param that [Sumi::Geometry::Point, Array<Numeric>] second point
  # @param precision [Integer, nil] decimal precision, or nil for the current function default
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when either point cannot be coerced
  def self.eq?(this, that, precision: nil)
    this, that = Tuples[self, this, that]
    F.eq?(this.x, that.x, precision:) && F.eq?(this.y, that.y, precision:)
  end

  # Returns the Euclidean distance between two points.
  # @param starting [Sumi::Geometry::Point, Array<Numeric>] start point
  # @param ending [Sumi::Geometry::Point, Array<Numeric>] end point
  # @return [Float]
  # @raise [Sumi::Geometry::Error] when either point cannot be coerced
  def self.length(starting, ending)
    starting, ending = Tuples[Point, starting, ending]
    ::Math.hypot(starting.x - ending.x, starting.y - ending.y)
  end

  # Returns the midpoint between two points.
  # @param starting [Sumi::Geometry::Point, Array<Numeric>] first point
  # @param ending [Sumi::Geometry::Point, Array<Numeric>] second point
  # @return [Sumi::Geometry::Point] midpoint
  # @raise [Sumi::Geometry::Error] when either point cannot be coerced
  def self.midpoint(starting, ending)
    starting, ending = Tuples[Point, starting, ending]
    self[(starting.x / 2.0) + (ending.x / 2.0), (starting.y / 2.0) + (ending.y / 2.0)]
  end

  # Returns the origin point.
  # @return [Sumi::Geometry::Point]
  def self.origin
    new(x: 0.0, y: 0.0)
  end

  # Creates a point.
  # @param x [Numeric] x coordinate
  # @param y [Numeric] y coordinate
  # @return [void]
  # @raise [Sumi::Geometry::Error] when a coordinate is not a finite Numeric
  def initialize(x:, y:) = super(x: Real[:x, x], y: Real[:y, y])

  # Compares points by x, then y.
  # @param other [Object] point or two-item coordinate array to compare
  # @return [Integer, nil] comparison result, or nil when other is not a valid point value
  def <=>(other)
    deconstruct <=> Tuple[Point, other].deconstruct
  rescue Error
    nil
  end

  # Reports whether this point is at or above another point in screen coordinates.
  # @param other [Sumi::Geometry::Point, Array<Numeric>] point to compare
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when other cannot be coerced
  def above?(other) = y <= Tuple[Point, other].y

  # Returns a point rounded to precision.
  # @param precision [Integer, nil] decimal precision, or nil for the current function default
  # @return [Sumi::Geometry::Point]
  def approx(precision = nil) = with(x: F.approx(x, precision), y: F.approx(y, precision))

  # Reports whether this point is at or below another point in screen coordinates.
  # @param other [Sumi::Geometry::Point, Array<Numeric>] point to compare
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when other cannot be coerced
  def below?(other) = y >= Tuple[Point, other].y

  # Compares this point with optional numeric precision.
  # @param other [Sumi::Geometry::Point, Array<Numeric>] point to compare
  # @param precision [Integer, nil] decimal precision, or nil for the current function default
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when other cannot be coerced
  def eq?(other, precision: nil) = self.class.eq?(self, other, precision:)

  # Reports strict point equality.
  # @param other [Object] object to compare
  # @return [Boolean]
  def eql?(other) = self.class == other.class && deconstruct == other.deconstruct

  # Returns a hash compatible with strict equality.
  # @return [Integer]
  def hash = [self.class, *deconstruct].hash

  # Reports whether this point is at or left of another point.
  # @param other [Sumi::Geometry::Point, Array<Numeric>] point to compare
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when other cannot be coerced
  def left?(other) = x <= Tuple[Point, other].x

  # Reports whether this point is at or right of another point.
  # @param other [Sumi::Geometry::Point, Array<Numeric>] point to compare
  # @return [Boolean]
  # @raise [Sumi::Geometry::Error] when other cannot be coerced
  def right?(other) = x >= Tuple[Point, other].x

  # Formats point coordinates for SVG coordinate-list attributes.
  # @return [String]
  def to_cs = "#{F.approx(x)},#{F.approx(y)}"

  # Formats the point for display.
  # @return [String]
  def to_s = "(#{to_cs})"

  alias_method :==, :eql?
end