Inherits:
Numeric
• Object
show all
Defined in:

Overview

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)

# => true
``````

Utility Methods

``````RTanque::Heading.new_from_degrees(180.0)

# => 180.0

# => -0.034906585039886195
``````

Constant Summary

FULL_ANGLE =
```Math::PI * 2.0
```
HALF_ANGLE =
```Math::PI
```
EIGHTH_ANGLE =
```Math::PI / 4.0
```
ONE_DEGREE =
```FULL_ANGLE / 360.0
```
FULL_RANGE =
```(0..FULL_ANGLE)
```
NORTH =
```N =         0.0
```
NORTH_EAST =
```NE =   1.0 * EIGHTH_ANGLE
```
EAST =
```E =          2.0 * EIGHTH_ANGLE
```
SOUTH_EAST =
```SE =   3.0 * EIGHTH_ANGLE
```
SOUTH =
```S =         4.0 * EIGHTH_ANGLE
```
SOUTH_WEST =
```SW =   5.0 * EIGHTH_ANGLE
```
WEST =
```W =          6.0 * EIGHTH_ANGLE
```
NORTH_WEST =
```NW =   7.0 * EIGHTH_ANGLE
```

Instance Attribute Summary collapse

Returns the value of attribute radians.

Instance Method Summary collapse

• unary operator.

• unary operator.

• difference between `self` and `to` respecting negative angles.

• continue with Numeric's pattern.

• constructor

Constructor Details

Parameters:

• radians (#to_f) (defaults to: NORTH)

 ``` 69 70 71 72 73``` ```# File 'lib/rtanque/heading.rb', line 69 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 ```

Instance Attribute Details

Returns the value of attribute radians

 ``` 65 66 67``` ```# File 'lib/rtanque/heading.rb', line 65 def radians @radians end ```

Class Method Details

 ``` 56 57 58 59``` ```# File 'lib/rtanque/heading.rb', line 56 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 ```

.new_between_points(from_point, to_point) ⇒ Object

 ``` 52 53 54``` ```# File 'lib/rtanque/heading.rb', line 52 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 ```

.new_from_degrees(degrees) ⇒ Object

 ``` 48 49 50``` ```# File 'lib/rtanque/heading.rb', line 48 def self.new_from_degrees(degrees) self.new((degrees / 180.0) * Math::PI) end ```

.rand ⇒ Object

 ``` 61 62 63``` ```# File 'lib/rtanque/heading.rb', line 61 def self.rand self.new(Kernel.rand * FULL_ANGLE) end ```

Instance Method Details

Parameters:

Returns:

 ``` 122 123 124``` ```# File 'lib/rtanque/heading.rb', line 122 def *(other_heading) self.class.new(self.radians * other_heading.to_f) end ```

Parameters:

Returns:

 ``` 110 111 112``` ```# File 'lib/rtanque/heading.rb', line 110 def +(other_heading) self.class.new(self.radians + other_heading.to_f) end ```

unary operator

Returns:

 ``` 134 135 136``` ```# File 'lib/rtanque/heading.rb', line 134 def +@ self.class.new(+self.radians) end ```

Parameters:

Returns:

 ``` 116 117 118``` ```# File 'lib/rtanque/heading.rb', line 116 def -(other_heading) self.+(-other_heading) end ```

unary operator

Returns:

 ``` 140 141 142``` ```# File 'lib/rtanque/heading.rb', line 140 def -@ self.class.new(-self.radians) end ```

Parameters:

Returns:

 ``` 128 129 130``` ```# File 'lib/rtanque/heading.rb', line 128 def /(other_heading) self.*(1.0 / other_heading) end ```

Parameters:

Returns:

• (Boolean)
 ``` 104 105 106``` ```# File 'lib/rtanque/heading.rb', line 104 def <=>(other_heading) self.to_f <=> other_heading.to_f end ```

Parameters:

Returns:

• (Boolean)
 ``` 91 92 93``` ```# File 'lib/rtanque/heading.rb', line 91 def ==(other_heading) self.to_f == other_heading.to_f end ```

Returns:

 ``` 85 86 87``` ```# File 'lib/rtanque/heading.rb', line 85 def clone self.class.new(self.radians) end ```

#delta(to) ⇒ Float

difference between `self` and `to` respecting negative angles

Parameters:

Returns:

• (Float)
 ``` 78 79 80 81 82``` ```# File 'lib/rtanque/heading.rb', line 78 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

Parameters:

Returns:

• (Boolean)
 ``` 98 99 100``` ```# File 'lib/rtanque/heading.rb', line 98 def eql?(other_heading) other_heading.instance_of?(self.class) && self.==(other_heading) end ```

#inspect ⇒ Object

 ``` 148 149 150``` ```# File 'lib/rtanque/heading.rb', line 148 def inspect "<#{self.class.name}: #{self.radians}rad #{self.to_degrees}deg>" end ```

#to_degrees ⇒ Float

Returns:

• (Float)
 ``` 158 159 160``` ```# File 'lib/rtanque/heading.rb', line 158 def to_degrees @memoized[:to_degrees] ||= (self.radians * 180.0) / Math::PI end ```

#to_f ⇒ Float

Returns:

• (Float)
 ``` 153 154 155``` ```# File 'lib/rtanque/heading.rb', line 153 def to_f self.radians end ```

#to_s ⇒ Object

 ``` 144 145 146``` ```# File 'lib/rtanque/heading.rb', line 144 def to_s self.to_f end ```