class Geom2D::Point

Represents a point.

Attributes

x[R]

The x-coordinate.

y[R]

The y-coordinate.

Public Class Methods

new(x, y) click to toggle source

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

*(other) click to toggle source

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
+(other) click to toggle source

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
+@() click to toggle source

Returns self.

# File lib/geom2d/point.rb, line 42
def +@
  self
end
-(other) click to toggle source

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
-@() click to toggle source

Returns the point mirrored in the origin.

# File lib/geom2d/point.rb, line 47
def -@
  Point.new(-x, -y)
end
/(other) click to toggle source

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
==(other) click to toggle source

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
bbox() click to toggle source

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
distance(point) click to toggle source

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
dot(other) click to toggle source

Multiplies this point with the other point using the dot product.

# File lib/geom2d/point.rb, line 97
def dot(other)
  self * other
end
to_a()
Alias for: to_ary
to_ary() click to toggle source

Allows destructuring of a point into an array.

# File lib/geom2d/point.rb, line 132
def to_ary
  [x, y]
end
Also aliased as: to_a
wedge(other) click to toggle source

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