Class AppMath::Graph
In: graph.rb
Parent: Object

class for representing a ‘window to the real world’ With the x-direction there is associated a real interval, and with the y-directon there is also associated a real interval. With hese intervals there are associated integer numbers width and height, which govern the ‘pixelation’ of these intervals.

Methods

bgr_color=   clear   draw   draw_func   grid_x   grid_y   ipos   ivx   ivy   new   set_size_x!   set_size_y!  

Attributes

bgr_color  [R] 
fac  [RW] 
grid_color  [RW] 
n_div  [RW] 
text_color  [RW] 

Public Class methods

[Source]

    # File graph.rb, line 39
39:   def initialize(parent,nx,ny,iv_x,iv_y)
40:     @fac = R.c 1.04
41:     @bgr_color = 'lightgreen'
42:     @n_div = 5
43:     @grid_color = 'darkgray'
44:     @text_color = 'black'
45:     
46:     @parent = parent
47:     @w = nx
48:     @h = ny
49:     set_size_x!(iv_x)
50:     set_size_y!(iv_y)
51:       # sets @a, @b, @c, @d, @ivx, @ivy
52:     col = @bgr_color
53:     @canvas = TkCanvas.new(parent){
54:       width nx
55:       height ny
56:       background col
57:     }
58:     @canvas.pack
59:   end

Public Instance methods

[Source]

    # File graph.rb, line 34
34:   def bgr_color=(color)  
35:     @@bgr = color
36:     clear(@@bgr)
37:   end

[Source]

    # File graph.rb, line 65
65:   def clear(color=@bgr_color)
66:     magic = 2 # not expected to need this
67:     TkcRectangle.new(@canvas, 0, 0, @w + magic, @h + magic, 
68:       'width'=> 0, 'fill' => color)
69:   end

[Source]

     # File graph.rb, line 89
 89:   def draw(arr_x, arr_y, color, auto_adjust = true)
 90:   #  clear(@bgr_color)
 91:     nx = arr_x.length
 92:     ny = arr_y.length
 93:     n = Basics.inf(nx,ny)
 94:     if auto_adjust
 95:       ivx_e = Iv.from_array(arr_x) * @fac # the values to be shown
 96:         # should stay a bit apat from the rim of the diagram
 97:       ivy_e = Iv.from_array(arr_y) * @fac
 98:       divx = ivx_e.axis_division(@n_div)
 99:       divy = ivy_e.axis_division(@n_div)
100:       d_x = divx[0]
101:       d_y = divy[0]
102:       v_x = divx[1]
103:       v_y = divy[1]
104:       ivxNew = Iv.new(v_x.first, v_x.last)  
105:       ivyNew = Iv.new(v_y.first, v_y.last)
106:       set_size_x!(ivxNew)
107:       set_size_y!(ivyNew)
108:       grid_x(v_x, grid_color)
109:       grid_y(v_y, grid_color)
110:     end
111:     
112:     for i in 1...n
113:       xa = arr_x[i-1]
114:       xb = arr_x[i]
115:       ya = arr_y[i-1]
116:       yb = arr_y[i]
117:       pa = ipos(xa,ya)
118:       pb = ipos(xb,yb)
119:       line = TkcLine.new(@canvas,pa[0],pa[1],pb[0],pb[1])
120:       line.fill(color)
121:     end
122:     if @bgr_color != @text_color # only then writing the text works, and
123:       # so we have a method to avoid writing text
124: # some magic numbers here    
125:       t_x = (@w * 0.1).to_i 
126:       t_y = (@h * 0.1).to_i
127:       t_w = (@w * 0.75).to_i
128:       t_h = (@h * 0.02).to_i
129:       t_h = Basics.sup(t_h,10)
130: 
131:       font_text = "LucidaConsole "+t_h.to_s
132: 
133:       xl = @ivx.low.to_s
134:       xu = @ivx.upp.to_s
135:       yl = @ivy.low.to_s
136:       yu = @ivy.upp.to_s
137:     
138:       x_text=xl + " < x < " + xu + " , dx_grid = " + d_x.to_s
139:       y_text=yl + " < y < " + yu + " , dy_grid = " + d_y.to_s
140:       full_text = x_text + "\n\n" + y_text
141:     
142:       txt=TkcText.new(@canvas,t_x,t_y){
143:         anchor "nw"
144:         width t_w 
145:         text full_text
146:         font font_text
147:       }
148:       txt.fill(@text_color)
149:     end
150:   end

[Source]

     # File graph.rb, line 152
152:   def draw_func(f, n, color = 'red', auto_adjust_y = true)
153:     xi = @ivx.low 
154:     arr_x = Array.new(n, xi)
155:     dx = @ivx.size/(n - 1.0)
156:     arr_y = Array.new(n, 0.0)
157:     for i in 0...n
158:       arr_x[i] = xi
159:       yi = f.at(xi)
160:       arr_y[i] = yi.to_f
161:       xi += dx
162:     end
163:     draw(arr_x, arr_y, color, auto_adjust_y)
164:   end

[Source]

    # File graph.rb, line 71
71:   def grid_x(anArray, color=@grid_color)
72:     anArray.each{ |x|
73:       pa = ipos(x,@ivy.low)
74:       pb = ipos(x,@ivy.upp)
75:       line = TkcLine.new(@canvas,pa[0],pa[1],pb[0],pb[1])
76:       line.fill(color)
77:     }
78:   end

[Source]

    # File graph.rb, line 80
80:   def grid_y(anArray, color=@grid_color)
81:     anArray.each{ |y|
82:       pa = ipos(@ivx.low,y)
83:       pb = ipos(@ivx.upp,y)
84:       line = TkcLine.new(@canvas,pa[0],pa[1],pb[0],pb[1])
85:       line.fill(color)
86:     }
87:   end

[Source]

     # File graph.rb, line 166
166:   def ipos x, y
167:     [@a + @b * x, @c + @d * y]
168:   end

[Source]

    # File graph.rb, line 61
61:   def ivx; @ivx; end

[Source]

    # File graph.rb, line 63
63:   def ivy; @ivy; end

[Source]

     # File graph.rb, line 170
170:   def set_size_x!(aIv)
171:     @ivx = aIv
172:     @b = (@w - 1.0)/@ivx.size
173:     @a = -@b * @ivx.low
174:   end

[Source]

     # File graph.rb, line 176
176:   def set_size_y!(aIv)
177:     @ivy = aIv
178:     @d = (1.0 - @h)/@ivy.size
179:     @c = -@d * @ivy.upp
180:   end

[Validate]