Class: Geom2D::Point

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/geom2d/point.rb

Overview

Represents a point.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y) ⇒ Point

Creates a new Point from the given coordinates.



27
28
29
30
# File 'lib/geom2d/point.rb', line 27

def initialize(x, y)
  @x = x
  @y = y
end

Instance Attribute Details

#xObject (readonly)

The x-coordinate.



21
22
23
# File 'lib/geom2d/point.rb', line 21

def x
  @x
end

#yObject (readonly)

The y-coordinate.



24
25
26
# File 'lib/geom2d/point.rb', line 24

def y
  @y
end

Instance Method Details

#*(other) ⇒ Object

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.



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/geom2d/point.rb', line 84

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) ⇒ Object

Depending on the type of the argument, either adds a number to each coordinate or adds two points.



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/geom2d/point.rb', line 54

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

#+@Object

Returns self.



43
44
45
# File 'lib/geom2d/point.rb', line 43

def +@
  self
end

#-(other) ⇒ Object

Depending on the type of the argument, either subtracts a number from each coordinate or subtracts the other point from this one.



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/geom2d/point.rb', line 69

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

#-@Object

Returns the point mirrored in the origin.



48
49
50
# File 'lib/geom2d/point.rb', line 48

def -@
  Point.new(-x, -y)
end

#/(other) ⇒ Object

Divides each coordinate by the given number.



109
110
111
112
113
114
115
116
# File 'lib/geom2d/point.rb', line 109

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) ⇒ Object

Compares this point to the other point, using floating point equality.

See Utils#float_equal.



121
122
123
124
125
126
127
128
129
130
# File 'lib/geom2d/point.rb', line 121

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

#bboxObject

Returns the point’s bounding box (i.e. a bounding box containing only the point itself).



33
34
35
# File 'lib/geom2d/point.rb', line 33

def bbox
  BoundingBox.new(x, y, x, y)
end

#distance(point) ⇒ Object

Returns the distance from this point to the given point.



38
39
40
# File 'lib/geom2d/point.rb', line 38

def distance(point)
  Math.hypot(point.x - x, point.y - y)
end

#dot(other) ⇒ Object

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



98
99
100
# File 'lib/geom2d/point.rb', line 98

def dot(other)
  self * other
end

#inspectObject Also known as: to_s

:nodoc:



138
139
140
# File 'lib/geom2d/point.rb', line 138

def inspect #:nodoc:
  "(#{x}, #{y})"
end

#to_aryObject Also known as: to_a

Allows destructuring of a point into an array.



133
134
135
# File 'lib/geom2d/point.rb', line 133

def to_ary
  [x, y]
end

#wedge(other) ⇒ Object

Performs the wedge product of this point with the other point.



103
104
105
106
# File 'lib/geom2d/point.rb', line 103

def wedge(other)
  other = Geom2D::Point(other)
  x * other.y - other.x * y
end