File: sine.rb

package info (click to toggle)
libtioga-ruby 1.8-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 9,956 kB
  • ctags: 3,257
  • sloc: ansic: 31,801; ruby: 16,346; sh: 172; makefile: 114
file content (83 lines) | stat: -rw-r--r-- 2,035 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# sine.rb : minimal example of plotting with Tioga

# Original of this file is copyright Vincent Fourmond, 2005
# Various edits made by Bill Paxton, 2007 for use in Tutorial.

# You are free to use this file for whatever you find it fit to.

# this file demonstates the use of tioga for producing plots. 

require 'Tioga/FigureMaker'

class MyPlots
  
  include Math
  include Tioga
  include FigureConstants
  
  attr_reader :t  # t is to talk to tioga

  # initialize is called when we do MyPlots.new
  def initialize
    @t = FigureMaker.default
    # we will store in @data both X and Y columns
    @data = []
    @margin = 0.05
    # we define the plot: 
    @plot = t.def_figure("sine") { exec_plot }    
  end


  # in make_data, we create the data set to be plotted
  def make_data
    num = 35
    xAxis = Dvector.new(num) {|i| 2*PI*i/(num-1) }    
    yAxis = xAxis.sin
    @data=[xAxis,yAxis]
  end


  # function for automatically computing graph boundaries
  def plot_boundaries(xs,ys,margin,xmin=nil,xmax=nil,ymin=nil,ymax=nil)
    xmin = xs.min if xmin == nil
    xmax = xs.max if xmax == nil
    ymin = ys.min if ymin == nil
    ymax = ys.max if ymax == nil

    width = (xmax == xmin) ? 1 : xmax - xmin
    height = (ymax == ymin) ? 1 : ymax - ymin

    left_boundary = xmin - margin * width
    right_boundary = xmax + margin * width

    top_boundary = ymax + margin * height
    bottom_boundary = ymin - margin * height

    return [ left_boundary, right_boundary, top_boundary, bottom_boundary ]
  end


  # this is called when the figure needs to be created
  def exec_plot
    make_data
    t.do_box_labels('Curve $y = \sin x$', '$x$', '$y$')
    xs = @data[0]; ys = @data[1]
    t.show_plot(plot_boundaries(xs,ys,@margin)) {
      t.show_polyline(xs,ys,Blue) # plot lines
      if false # plot markers
        t.show_marker('Xs' => xs, 'Ys' => ys,
                    'marker' => Asterisk,
                    'scale' => 1.2, 
                    'color' => Blue)
      end
    }
  end

  
end


MyPlots.new