Sha256: a6f5a592e3e25765796b5e9fdda2b3ba1b6712fcf9a89fd8fb8b015066421069

Contents?: true

Size: 992 Bytes

Versions: 3

Compression:

Stored size: 992 Bytes

Contents

##
# Integer extensions for graphics

class Integer
  ##
  # Calculate a random chance using easy notation: 1 =~ 50 :: 1 in 50 chance

  def =~ n #
    rand(n) <= (self - 1)
  end
end

##
# Numeric extensions for graphics

class Numeric
  ##
  # Is M close to N within a certain delta?

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

  ##
  # Normalize a number to be within 0...360

  def degrees
    (self < 0 ? self + 360 : self) % 360
  end

  ##
  # 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.

  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
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
graphics-1.0.0b6 lib/graphics/extensions.rb
graphics-1.0.0b5 lib/graphics/extensions.rb
graphics-1.0.0b4 lib/graphics/extensions.rb