Module: Vector2d::Transformations

Includes:
Contracts
Included in:
Vector2d
Defined in:
lib/vector2d/transformations.rb

Instance Method Summary collapse

Instance Method Details

#ceilObject



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

def ceil
  self.class.new(x.ceil, y.ceil)
end

#floorObject



21
22
23
# File 'lib/vector2d/transformations.rb', line 21

def floor
  self.class.new(x.floor, y.floor)
end

#normalizeObject



32
33
34
# File 'lib/vector2d/transformations.rb', line 32

def normalize
  resize(1.0)
end

#perpendicularObject



41
42
43
# File 'lib/vector2d/transformations.rb', line 41

def perpendicular
  Vector2d.new(-y, x)
end

#resize(new_length) ⇒ Object



50
51
52
# File 'lib/vector2d/transformations.rb', line 50

def resize(new_length)
  self * (new_length / self.length)
end

#reverseObject



59
60
61
# File 'lib/vector2d/transformations.rb', line 59

def reverse
  self.class.new(-x, -y)
end

#rotate(angle) ⇒ Object



68
69
70
71
72
73
# File 'lib/vector2d/transformations.rb', line 68

def rotate(angle)
  Vector2d.new(
    x * Math.cos(angle) - y * Math.sin(angle),
    x * Math.sin(angle) + y * Math.cos(angle)
  )
end

#round(digits = 0) ⇒ Object



81
82
83
# File 'lib/vector2d/transformations.rb', line 81

def round(digits=0)
  self.class.new(x.round(digits), y.round(digits))
end

#truncate(max) ⇒ Object



92
93
94
# File 'lib/vector2d/transformations.rb', line 92

def truncate(max)
  resize([max, self.length].min)
end