Module: Silicium::Plotter

Includes:
Geometry
Defined in:
lib/plotter.rb

Overview

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

Defined Under Namespace

Modules: Color Classes: Image

Constant Summary collapse

CENTER_X =
Window.width / 2
CENTER_Y =
Window.height / 2

Instance Method Summary collapse

Methods included from Geometry

#brute_min, #clockwise, #counter_clockwise, #cut_by_eq, #distance_point_line2d, #distance_point_line_normalized2d, #distance_point_to_point2d, #divide_min, #insert_eq, #minimal_convex_hull_2d, #needed_variables_order?, #not_polygon?, #oriented_area, #process_cf, #process_free_member, #process_line_by_coordinates, #put_point_in_part, #sign, #vector_length, #vectors_product

Instance Method Details

#Color(r, g, b, a) ⇒ Integer #Color(r, g, b) ⇒ Integer #Color(hex_value, opacity = nil) ⇒ Integer #Color(color_name, opacity = nil) ⇒ Integer #Color(color_value, opacity = nil) ⇒ Integer

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

Overloads:

  • #Color(r, g, b, a) ⇒ Integer

    Returns The rgba color value.

    Returns:

    • (Integer)

      The rgba color value.

  • #Color(r, g, b) ⇒ Integer

    Returns The rgb color value.

    Returns:

    • (Integer)

      The rgb color value.

  • #Color(hex_value, opacity = nil) ⇒ Integer

    Returns The hex color value, with the opacity applied if one was given.

    Returns:

    • (Integer)

      The hex color value, with the opacity applied if one was given.

  • #Color(color_name, opacity = nil) ⇒ Integer

    Returns The hex color value, with the opacity applied if one was given.

    Returns:

    • (Integer)

      The hex color value, with the opacity applied if one was given.

  • #Color(color_value, opacity = nil) ⇒ Integer

    Returns The color value, with the opacity applied if one was given.

    Parameters:

    • The (Integer, :to_i)

      color value.

    Returns:

    • (Integer)

      The color value, with the opacity applied if one was given.

Returns:

  • (Integer)

    The determined color value as RGBA integer.

Raises:

  • (ArgumentError)

    if the arguments weren’t understood as a color.



56
57
58
59
60
61
62
63
64
65
# 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_axesObject

draws axes



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# 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) ⇒ Object

Draws the function func at the interval from a to b



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# 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) ⇒ Object

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



196
197
198
199
200
201
202
203
204
# 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

#mulObject



250
251
252
# File 'lib/plotter.rb', line 250

def mul
  @mul || 100
end

#reduce_interval(a, b) ⇒ Object

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



208
209
210
211
212
# 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) ⇒ Object

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



182
183
184
185
186
187
188
189
190
191
# 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) ⇒ Object

Parameters:

  • sc (Integer)


246
247
248
# File 'lib/plotter.rb', line 246

def set_scale(sc)
  @mul = sc
end

#show_windowObject

show plot



241
242
243
# File 'lib/plotter.rb', line 241

def show_window
  show
end