class RTanque::Heading
A Heading
represents an angle. Basically a wrapper around `Float` bound to `(0..Math::PI * 2)`
0.0 == `RTanque::Heading::NORTH` is 'up'
##Basic Usage
RTanque::Heading.new(Math::PI) # => <RTanque::Heading: 1.0rad 180.0deg> RTanque::Heading.new(Math::PI) + RTanque::Heading.new(Math::PI) # => <RTanque::Heading: 0.0rad 0.0deg> RTanque::Heading.new(Math::PI / 2.0) + Math::PI # => <RTanque::Heading: 1.5rad 270.0deg> RTanque::Heading.new(0.0) == 0 # => true
##Utility Methods
RTanque::Heading.new_from_degrees(180.0) # => <RTanque::Heading: 1.0rad 180.0deg> RTanque::Heading.new(Math::PI).to_degrees # => 180.0 RTanque::Heading.new_between_points(RTanque::Point.new(0,0), RTanque::Point.new(2,3)) # => <RTanque::Heading: 0.1871670418109988rad 33.690067525979785deg> RTanque::Heading.new_from_degrees(1).delta(RTanque::Heading.new_from_degrees(359)) # => -0.034906585039886195
Constants
- EAST
- EIGHTH_ANGLE
- FULL_ANGLE
- FULL_RANGE
- HALF_ANGLE
- NORTH
- NORTH_EAST
- NORTH_WEST
- ONE_DEGREE
- SOUTH
- SOUTH_EAST
- SOUTH_WEST
- WEST
Attributes
Public Class Methods
# File lib/rtanque/heading.rb, line 55 def self.delta_between_points(from_point, from_point_heading, to_point) rel_heading = self.new_between_points(from_point, to_point) self.new(from_point_heading).delta(rel_heading) end
Creates a new RTanque::Heading
@param [#to_f] radians degree to wrap (in radians)
# File lib/rtanque/heading.rb, line 68 def initialize(radians = NORTH) @radians = radians.to_f % FULL_ANGLE @memoized = {} # allow memoization since @some_var ||= x doesn't work when frozen self.freeze end
# File lib/rtanque/heading.rb, line 51 def self.new_between_points(from_point, to_point) self.new(from_point == to_point ? 0.0 : Math.atan2(to_point.x - from_point.x, to_point.y - from_point.y)) end
# File lib/rtanque/heading.rb, line 47 def self.new_from_degrees(degrees) self.new((degrees / 180.0) * Math::PI) end
# File lib/rtanque/heading.rb, line 60 def self.rand self.new(Kernel.rand * FULL_ANGLE) end
Public Instance Methods
@param [#to_f] other_heading @return [RTanque::Heading]
# File lib/rtanque/heading.rb, line 121 def *(other_heading) self.class.new(self.radians * other_heading.to_f) end
@param [#to_f] other_heading @return [RTanque::Heading]
# File lib/rtanque/heading.rb, line 109 def +(other_heading) self.class.new(self.radians + other_heading.to_f) end
unary operator @return [RTanque::Heading]
# File lib/rtanque/heading.rb, line 133 def +@ self.class.new(+self.radians) end
@param [#to_f] other_heading @return [RTanque::Heading]
# File lib/rtanque/heading.rb, line 115 def -(other_heading) self.+(-other_heading) end
unary operator @return [RTanque::Heading]
# File lib/rtanque/heading.rb, line 139 def -@ self.class.new(-self.radians) end
@param [#to_f] other_heading @return [RTanque::Heading]
# File lib/rtanque/heading.rb, line 127 def /(other_heading) self.*(1.0 / other_heading) end
@param [#to_f] other_heading @return [Boolean]
# File lib/rtanque/heading.rb, line 103 def <=>(other_heading) self.to_f <=> other_heading.to_f end
@param [#to_f] other_heading @return [Boolean]
# File lib/rtanque/heading.rb, line 90 def ==(other_heading) self.to_f == other_heading.to_f end
@return [RTanque::Heading]
# File lib/rtanque/heading.rb, line 84 def clone self.class.new(self.radians) end
difference between `self` and `to` respecting negative angles @param [#to_f] to @return [Float]
# File lib/rtanque/heading.rb, line 77 def delta(to) diff = (to.to_f - self.to_f).abs % FULL_ANGLE diff = -(FULL_ANGLE - diff) if diff > Math::PI to.to_f < self.to_f ? -diff : diff end
continue with Numeric's pattern @param [#to_f] other_heading @return [Boolean]
# File lib/rtanque/heading.rb, line 97 def eql?(other_heading) other_heading.instance_of?(self.class) && self.==(other_heading) end
# File lib/rtanque/heading.rb, line 147 def inspect "<#{self.class.name}: #{self.radians}rad #{self.to_degrees}deg>" end
@return [Float]
# File lib/rtanque/heading.rb, line 157 def to_degrees @memoized[:to_degrees] ||= (self.radians * 180.0) / Math::PI end
@return [Float]
# File lib/rtanque/heading.rb, line 152 def to_f self.radians end
# File lib/rtanque/heading.rb, line 143 def to_s self.to_f end