module Silicium::Plotter

Plotter module Module contains classes, that are different kinds of plain plotters

Constants

CENTER_X
CENTER_Y

Public Instance Methods

color(*args) click to toggle source

Factory method to return a color value, based on the arguments given.

@overload Color(r, g, b, a)

@param (see ChunkyPNG::Color.rgba)
@return [Integer] The rgba color value.

@overload Color(r, g, b)

@param (see ChunkyPNG::Color.rgb)
@return [Integer] The rgb color value.

@overload Color(hex_value, opacity = nil)

@param (see ChunkyPNG::Color.from_hex)
@return [Integer] The hex color value, with the opacity applied if one
  was given.

@overload Color(color_name, opacity = nil)

@param (see ChunkyPNG::Color.html_color)
@return [Integer] The hex color value, with the opacity applied if one
  was given.

@overload Color(color_value, opacity = nil)

@param [Integer, :to_i] The color value.
@return [Integer] The color value, with the opacity applied if one was
  given.

@return [Integer] The determined color value as RGBA integer. @raise [ArgumentError] if the arguments weren't understood as a color.

# File lib/plotter.rb, line 56
def color(*args)
  case args.length
  when 1 then Color.parse(args.first)
  when 2 then (Color.parse(args.first) & 0xffffff00) | args[1].to_i
  when 3 then Color.rgb(*args)
  when 4 then Color.rgba(*args)
  else raise ArgumentError,
             "Don't know how to create a color from #{args.inspect}!"
  end
end
draw_axes() click to toggle source

draws axes

# File lib/plotter.rb, line 156
def draw_axes
  Line.new(x1: 0, y1: CENTER_Y, x2: (get :width), y2: CENTER_Y, width: 1, color: 'white', z: 20)
  Line.new(x1: CENTER_X, y1: 0, x2: CENTER_X, y2: (get :height), width: 1, color: 'white', z: 20)

  x1 = CENTER_X
  x2 = CENTER_X
  while (x1 < Window.width * 1.1) and (x2 > Window.width * -1.1) do
    Line.new(x1: x1, y1: CENTER_Y - 4, x2: x1, y2: CENTER_Y + 3, width: 1, color: 'white', z: 20)
    Line.new(x1: x2, y1: CENTER_Y - 4, x2: x2, y2: CENTER_Y + 3, width: 1, color: 'white', z: 20)
    x1 += mul
    x2 -= mul
  end

  y1 = CENTER_Y
  y2 = CENTER_Y
  while (y1 < Window.height * 1.1) and (y2 > Window.height * -1.1) do
    Line.new(x1: CENTER_X - 3, y1: y1, x2: CENTER_X + 3, y2: y1, width: 1, color: 'white', z: 20)
    Line.new(x1: CENTER_X - 3, y1: y2, x2: CENTER_X + 3, y2: y2, width: 1, color: 'white', z: 20)
    y1 += mul
    y2 -= mul
  end
end
draw_fn(a, b, &func) click to toggle source

Draws the function func at the interval from a to b

# File lib/plotter.rb, line 216
def draw_fn(a, b, &func)
  draw_axes

  a, b = reduce_interval(a, b)

  step = 0.38
  c_step = step
  arg = a

  while arg < b do
    c_step = step
    begin
      c_step = reset_step(arg, step) {|xx| fn(xx)}
    rescue Math::DomainError
      arg += c_step * 0.1
    else
      draw_point(arg, func.call(arg), mul, 'lime')
    ensure
      arg += c_step
    end
  end
end
draw_point(x, y, mul, col) click to toggle source

Draws a point on coordinates x and y with the scale mul and color col

# File lib/plotter.rb, line 196
def draw_point(x, y, mul, col)
  Line.new(
      x1: CENTER_X + x * mul, y1: CENTER_Y - y * mul,
      x2: CENTER_X + 1 + x * mul, y2: CENTER_Y + 2 - y * mul,
      width: 1,
      color: col,
      z: 20
  )
end
mul() click to toggle source
# File lib/plotter.rb, line 250
def mul
  @mul || 100
end
reduce_interval(a, b) click to toggle source

Reduces the interval to the window range. a and b that determine interval

# File lib/plotter.rb, line 208
def reduce_interval(a, b)
  a *= mul
  b *= mul
  return [a, -(get :width) * 1.1].max / mul, [b, (get :width) * 1.1].min / mul
end
reset_step(x, st, &f) click to toggle source

Changes the coordinates to draw the next pixel for the f function x - current argument. st - step to next point

# File lib/plotter.rb, line 182
def reset_step(x, st, &f)
  y1 = f.call(x)
  y2 = f.call(x + st)

  if (y1 - y2).abs / mul > 1.0
    [st / (y1 - y2).abs / mul, 0.001].max
  else
    st / mul * 2
  end
end
set_scale(sc) click to toggle source

@param [Integer] sc

# File lib/plotter.rb, line 246
def set_scale(sc)
  @mul = sc
end
show_window() click to toggle source

show plot

# File lib/plotter.rb, line 241
def show_window
  show
end