Class: Numeric

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

Overview

Numeric extensions for graphics

Instance Method Summary collapse

Instance Method Details

#close_to?(n, delta = 0.01) ⇒ Boolean

Is M close to N within a certain delta?

Returns:

  • (Boolean)


20
21
22
# File 'lib/graphics/extensions.rb', line 20

def close_to? n, delta = 0.01
  (self - n).abs < delta
end

#degreesObject

Normalize a number to be within 0…360



27
28
29
# File 'lib/graphics/extensions.rb', line 27

def degrees
  self % 360
end

#relative_angle(n, max) ⇒ Object

I am honestly befuddled by this code, and I wrote it.

I should probably remove it and start over.

Consider this method private, even tho it is in use by the demos.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/graphics/extensions.rb', line 38

def relative_angle n, max
  delta_cw = (self - n).degrees
  delta_cc = (n - self).degrees

  return if delta_cc < 0.1 || delta_cw < 0.1

  if delta_cc.abs < max then
    delta_cc
  elsif delta_cw.close_to? 180 then
    [-max, max].sample
  elsif delta_cw < delta_cc then
    -max
  else
    max
  end
end