class Geom2D::Point
Represents a point.
Attributes
The x-coordinate.
The y-coordinate.
Public Class Methods
Creates a new Point
from the given coordinates.
# File lib/geom2d/point.rb, line 26 def initialize(x, y) @x = x @y = y end
Public Instance Methods
Depending on the type of the argument, either multiplies this point with the other point (dot product) or multiplies each coordinate with the given number.
# File lib/geom2d/point.rb, line 83 def *(other) case other when Point x * other.x + y * other.y when Numeric Point.new(x * other, y * other) when Array self * Geom2D::Point(other) else raise ArgumentError, "Invalid argument class, must be Numeric or Point" end end
Depending on the type of the argument, either adds a number to each coordinate or adds two points.
# File lib/geom2d/point.rb, line 53 def +(other) case other when Point Point.new(x + other.x, y + other.y) when Numeric Point.new(x + other, y + other) when Array self + Geom2D::Point(other) else raise ArgumentError, "Invalid argument class, must be Numeric or Point" end end
Returns self.
# File lib/geom2d/point.rb, line 42 def +@ self end
Depending on the type of the argument, either subtracts a number from each coordinate or subtracts the other point from this one.
# File lib/geom2d/point.rb, line 68 def -(other) case other when Point Point.new(x - other.x, y - other.y) when Numeric Point.new(x - other, y - other) when Array self - Geom2D::Point(other) else raise ArgumentError, "Invalid argument class, must be Numeric or Point" end end
Returns the point mirrored in the origin.
# File lib/geom2d/point.rb, line 47 def -@ Point.new(-x, -y) end
Divides each coordinate by the given number.
# File lib/geom2d/point.rb, line 108 def /(other) case other when Numeric Point.new(x / other.to_f, y / other.to_f) else raise ArgumentError, "Invalid argument class, must be Numeric" end end
Compares this point to the other point, using floating point equality.
See Utils#float_equal
.
# File lib/geom2d/point.rb, line 120 def ==(other) case other when Point float_equal(x, other.x) && float_equal(y, other.y) when Array self == Geom2D::Point(other) else false end end
Returns the point's bounding box (i.e. a bounding box containing only the point itself).
# File lib/geom2d/point.rb, line 32 def bbox BoundingBox.new(x, y, x, y) end
Returns the distance from this point to the given point.
# File lib/geom2d/point.rb, line 37 def distance(point) Math.hypot(point.x - x, point.y - y) end
Multiplies this point with the other point using the dot product.
# File lib/geom2d/point.rb, line 97 def dot(other) self * other end
Allows destructuring of a point into an array.
# File lib/geom2d/point.rb, line 132 def to_ary [x, y] end
Performs the wedge product of this point with the other point.
# File lib/geom2d/point.rb, line 102 def wedge(other) other = Geom2D::Point(other) x * other.y - other.x * y end