Class: Sumi::Geometry::Point
- Inherits:
-
Data
- Object
- Data
- Sumi::Geometry::Point
- 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.
Instance Attribute Summary collapse
-
#x ⇒ Float
readonly
X coordinate.
-
#y ⇒ Float
readonly
Y coordinate.
Class Method Summary collapse
-
.[](x, y) ⇒ Sumi::Geometry::Point
Creates a point from two coordinates.
-
.angle(starting, ending) ⇒ Float
Returns the screen-space angle from one point to another.
-
.collinear?(*points, precision: nil) ⇒ Boolean
Reports whether three or more points lie on one infinite line.
-
.eq?(this, that, precision: nil) ⇒ Boolean
Compares two points with optional numeric precision.
-
.length(starting, ending) ⇒ Float
Returns the Euclidean distance between two points.
-
.midpoint(starting, ending) ⇒ Sumi::Geometry::Point
Returns the midpoint between two points.
-
.origin ⇒ Sumi::Geometry::Point
Returns the origin point.
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer?
Compares points by x, then y.
-
#above?(other) ⇒ Boolean
Reports whether this point is at or above another point in screen coordinates.
-
#approx(precision = nil) ⇒ Sumi::Geometry::Point
Returns a point rounded to precision.
-
#below?(other) ⇒ Boolean
Reports whether this point is at or below another point in screen coordinates.
-
#eq?(other, precision: nil) ⇒ Boolean
Compares this point with optional numeric precision.
-
#eql?(other) ⇒ Boolean
(also: #==)
Reports strict point equality.
-
#equation(angle) ⇒ Sumi::Geometry::Equation::Linear
Returns the linear equation passing through this point at an angle.
-
#hash ⇒ Integer
Returns a hash compatible with strict equality.
-
#initialize(x:, y:) ⇒ void
constructor
Creates a point.
-
#left?(other) ⇒ Boolean
Reports whether this point is at or left of another point.
-
#reflect(x: true, y: true) ⇒ Sumi::Geometry::Point
Returns a point reflected across the selected axes.
-
#right?(other) ⇒ Boolean
Reports whether this point is at or right of another point.
-
#rotate(a) ⇒ Sumi::Geometry::Point
Returns a point rotated around the origin.
-
#scale(sx, sy = Undefined) ⇒ Sumi::Geometry::Point
Returns a point scaled from the origin.
-
#skew(ax, ay = Undefined) ⇒ Sumi::Geometry::Point
Returns a point skewed from the origin.
-
#skew_x(a) ⇒ Sumi::Geometry::Point
Returns a point skewed along x.
-
#skew_y(a) ⇒ Sumi::Geometry::Point
Returns a point skewed along y.
-
#to_cs ⇒ String
Formats point coordinates for SVG coordinate-list attributes.
-
#to_s ⇒ String
Formats the point for display.
-
#translate(dx, dy = Undefined) ⇒ Sumi::Geometry::Point
Returns a translated point.
Constructor Details
#initialize(x:, y:) ⇒ void
Creates a point.
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.
151 152 153 |
# File 'geometry/lib/sumi/geometry/point.rb', line 151 def x @x end |
#y ⇒ Float (readonly)
Returns 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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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 |