Class: Geo2D::Vector

Inherits:
Object
  • Object
show all
Defined in:
lib/geo2d.rb

Overview

Planar vectors; used also to represent points of the plane

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x = 0, y = 0) ⇒ Vector

Returns a new instance of Vector.



7
8
9
10
# File 'lib/geo2d.rb', line 7

def initialize(x=0, y=0)
  @x = x.to_f
  @y = y.to_f
end

Instance Attribute Details

#xObject

Returns the value of attribute x.



12
13
14
# File 'lib/geo2d.rb', line 12

def x
  @x
end

#yObject

Returns the value of attribute y.



12
13
14
# File 'lib/geo2d.rb', line 12

def y
  @y
end

Instance Method Details

#*(scalar_or_vector) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/geo2d.rb', line 36

def *(scalar_or_vector)
  if Numeric===scalar_or_vector
    # scalar product
    Vector.new(scalar_or_vector*self.x, scalar_or_vector*self.y)
  else
    # dot product
    other = Geo2D.Vector(scalar_or_vector)
    self.x*other.x +  self.y*other.y
  end
end

#+(other) ⇒ Object



26
27
28
29
# File 'lib/geo2d.rb', line 26

def +(other)
  other = Geo2D.Vector(other)
  Vector.new(self.x+other.x, self.y+other.y)
end

#-(other) ⇒ Object



31
32
33
34
# File 'lib/geo2d.rb', line 31

def -(other)
  other = Geo2D.Vector(other)
  Vector.new(self.x-other.x, self.y-other.y)
end

#/(scalar) ⇒ Object



47
48
49
50
# File 'lib/geo2d.rb', line 47

def /(scalar)
  # self * 1.0/scalar
  Vector.new(self.x/scalar, self.y/scalar)
end

#==(other) ⇒ Object



61
62
63
# File 'lib/geo2d.rb', line 61

def ==(other)
  self.x == other.x && self.y == other.y
end

#aligned_with?(other) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/geo2d.rb', line 93

def aligned_with?(other)
  cross_z == 0
end

#angle_to(other) ⇒ Object

angle between two vectors



89
90
91
# File 'lib/geo2d.rb', line 89

def angle_to(other)
  Math.atan2(cross_z(other), dot(other))
end

#apply(prc, &blk) ⇒ Object

Apply arbitrary transformation (passed as a Proc or as a block)



110
111
112
113
# File 'lib/geo2d.rb', line 110

def apply(prc, &blk)
  prc ||= blk
  prc[self]
end

#argumentObject



22
23
24
# File 'lib/geo2d.rb', line 22

def argument
  Math.atan2(self.y, self.x)
end

#boundsObject



115
116
117
# File 'lib/geo2d.rb', line 115

def bounds
  [x,y,x,y]
end

#coerce(scalar) ⇒ Object



119
120
121
122
123
124
125
# File 'lib/geo2d.rb', line 119

def coerce(scalar)
  if scalar.kind_of?(Numeric)
    [self, scalar]
  else
    raise ArgumentError, "Vector: cannot coerce #{scalar.class}"
  end
end

#cross_z(other) ⇒ Object

z coordinate of cross product



53
54
55
# File 'lib/geo2d.rb', line 53

def cross_z(other)
  self.x*other.y - other.x*self.y
end

#distance_to(other) ⇒ Object



127
128
129
130
131
132
133
# File 'lib/geo2d.rb', line 127

def distance_to(other)
  if other.kind_of?(Vector)
    (other-self).modulus
  else
    other.distance_to?(self)
  end
end

#dot(other) ⇒ Object



57
58
59
# File 'lib/geo2d.rb', line 57

def dot(other)
  self.x*other.x + self.y*other.y
end

#lengthObject



18
19
20
# File 'lib/geo2d.rb', line 18

def length
  modulus
end

#modulusObject



14
15
16
# File 'lib/geo2d.rb', line 14

def modulus
  Math.hypot(self.x, self.y)
end

#orthoObject

vector rotated 90 degrees counter-clockwise



84
85
86
# File 'lib/geo2d.rb', line 84

def ortho
  Vector.new(-self.y, self.x)
end

#rotate(angle) ⇒ Object

vector rotation (center at origin); for a general rotation use Geo2D.rotation



105
106
107
# File 'lib/geo2d.rb', line 105

def rotate(angle)
  transform(*Geo2D.rotation_transform(angle))
end

#splitObject



74
75
76
# File 'lib/geo2d.rb', line 74

def split
  to_a
end

#to_aObject



65
66
67
# File 'lib/geo2d.rb', line 65

def to_a
  [self.x, self.y]
end

#to_sObject



69
70
71
# File 'lib/geo2d.rb', line 69

def to_s
  "(#{self.x}, #{self.y})"
end

#transform(*t) ⇒ Object

multiply by matrix [[a11, a12], [a21, a22]]



98
99
100
101
102
# File 'lib/geo2d.rb', line 98

def transform(*t)
  a11, a12, a21, a22 = t.flatten
  x, y = self.x, self.y
  Vector.new(a11*x + a12*y, a21*x + a22*y)
end

#unitaryObject

unitary vector in the direction of self



79
80
81
# File 'lib/geo2d.rb', line 79

def unitary
  self / self.modulus
end