Class: AppMath::Graph
- Inherits:
-
Object
- Object
- AppMath::Graph
- Defined in:
- lib/graph.rb
Overview
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 these intervals there are associated integer numbers width and height, which govern the ‘pixelation’ of these intervals.
Instance Attribute Summary collapse
-
#bgr_color ⇒ Object
Returns the value of attribute bgr_color.
-
#fac ⇒ Object
Returns the value of attribute fac.
-
#grid_color ⇒ Object
Returns the value of attribute grid_color.
-
#n_div ⇒ Object
Returns the value of attribute n_div.
-
#text_color ⇒ Object
Returns the value of attribute text_color.
Instance Method Summary collapse
-
#clear(color = @bgr_color) ⇒ Object
Clearing the graphical window by painting it uniformly with a given color.
-
#draw(arr_x, arr_y, color, auto_adjust = true) ⇒ Object
Drawing a polygon which is determined by an array of x-values and an array of y-values (if these don’t have equal length, the smaller of he two sizes is active) in a given color.
-
#draw_func(f, n, color = 'red', auto_adjust_y = true) ⇒ Object
Drawing a polygon which represents function f.
-
#grid_x(anArray, color = @grid_color) ⇒ Object
Setting vertical lines associated with an array of x-values (given as the first argument) in a color (given as the second argument).
-
#grid_y(anArray, color = @grid_color) ⇒ Object
Setting horizontal lines associated with an array of y-values (given as the first argument) in a color (given as the second argument).
-
#initialize(parent, nx, ny, iv_x, iv_y) ⇒ Graph
constructor
Specifes the parent window, pixel sizes in x- and y-directons, and the intervals of x-values and y-alues associated with the graphical window.
-
#ipos(x, y) ⇒ Object
integer (pixel-wise) position associated with a point (x,y) in x,y-space.
-
#ivx ⇒ Object
Returns the interval of x-values.
-
#ivy ⇒ Object
Returns the interval of y-values.
-
#set_size_x!(aIv) ⇒ Object
Setting the active interval of x-values and the auxiliar quantities @a and @b.
-
#set_size_y!(aIv) ⇒ Object
Setting the active interval of y-values and the auxiliar quantities @c and @d.
Constructor Details
#initialize(parent, nx, ny, iv_x, iv_y) ⇒ Graph
Specifes the parent window, pixel sizes in x- and y-directons, and the intervals of x-values and y-alues associated with the graphical window.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/graph.rb', line 61 def initialize(parent,nx,ny,iv_x,iv_y) @fac = R.c 1.04 @bgr_color = 'lightgreen' @n_div = 5 @grid_color = 'darkgray' @text_color = 'black' @parent = parent @w = nx @h = ny set_size_x!(iv_x) set_size_y!(iv_y) # sets @a, @b, @c, @d, @ivx, @ivy col = @bgr_color @canvas = TkCanvas.new(parent){ width nx height ny background col } @canvas.pack end |
Instance Attribute Details
#bgr_color ⇒ Object
Returns the value of attribute bgr_color.
47 48 49 |
# File 'lib/graph.rb', line 47 def bgr_color @bgr_color end |
#fac ⇒ Object
Returns the value of attribute fac.
46 47 48 |
# File 'lib/graph.rb', line 46 def fac @fac end |
#grid_color ⇒ Object
Returns the value of attribute grid_color.
49 50 51 |
# File 'lib/graph.rb', line 49 def grid_color @grid_color end |
#n_div ⇒ Object
Returns the value of attribute n_div.
48 49 50 |
# File 'lib/graph.rb', line 48 def n_div @n_div end |
#text_color ⇒ Object
Returns the value of attribute text_color.
50 51 52 |
# File 'lib/graph.rb', line 50 def text_color @text_color end |
Instance Method Details
#clear(color = @bgr_color) ⇒ Object
Clearing the graphical window by painting it uniformly with a given color.
91 92 93 94 95 |
# File 'lib/graph.rb', line 91 def clear(color=@bgr_color) magic = 2 # not expected to need this TkcRectangle.new(@canvas, 0, 0, @w + magic, @h + magic, 'width'=> 0, 'fill' => color) end |
#draw(arr_x, arr_y, color, auto_adjust = true) ⇒ Object
Drawing a polygon which is determined by an array of x-values and an array of y-values (if these don’t have equal length, the smaller of he two sizes is active) in a given color. If the hird argument is true, the intervals of x-values and y-values get adjusted in a manner that the whole polygon can be represented and that the coordinate ranges can be described by simple numbers in the style as numerical ranges for printed diagrams are typically selected.
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/graph.rb', line 130 def draw(arr_x, arr_y, color, auto_adjust = true) # clear(@bgr_color) nx = arr_x.length ny = arr_y.length n = Basics.inf(nx,ny) if auto_adjust ivx_e = Iv.from_array(arr_x) * @fac # the values to be shown # should stay a bit apat from the rim of the diagram ivy_e = Iv.from_array(arr_y) * @fac divx = ivx_e.axis_division(@n_div) divy = ivy_e.axis_division(@n_div) d_x = divx[0] d_y = divy[0] v_x = divx[1] v_y = divy[1] ivxNew = Iv.new(v_x.first, v_x.last) ivyNew = Iv.new(v_y.first, v_y.last) set_size_x!(ivxNew) set_size_y!(ivyNew) grid_x(v_x, grid_color) grid_y(v_y, grid_color) end for i in 1...n xa = arr_x[i-1] xb = arr_x[i] ya = arr_y[i-1] yb = arr_y[i] pa = ipos(xa,ya) pb = ipos(xb,yb) line = TkcLine.new(@canvas,pa[0],pa[1],pb[0],pb[1]) line.fill(color) end if @bgr_color != @text_color # only then writing the text works, and # so we have a method to avoid writing text # some magic numbers here t_x = (@w * 0.1).to_i t_y = (@h * 0.1).to_i t_w = (@w * 0.75).to_i t_h = (@h * 0.02).to_i t_h = Basics.sup(t_h,10) font_text = "LucidaConsole "+t_h.to_s xl = @ivx.low.to_s xu = @ivx.upp.to_s yl = @ivy.low.to_s yu = @ivy.upp.to_s x_text=xl + " < x < " + xu + " , dx_grid = " + d_x.to_s y_text=yl + " < y < " + yu + " , dy_grid = " + d_y.to_s full_text = x_text + "\n\n" + y_text txt=TkcText.new(@canvas,t_x,t_y){ anchor "nw" width t_w text full_text font font_text } txt.fill(@text_color) end end |
#draw_func(f, n, color = 'red', auto_adjust_y = true) ⇒ Object
Drawing a polygon which represents function f. The x-values to be used for creating this polygon are determined by interval @ivx and the number n of points. f is assumed to be a ‘function object’ in the sense that it dfines a method ‘at’ so that f.at(x) is what one normally would write as f(x). So ‘at’ replaces C++‘s operator(). Of course, the last argument allows auto-scaling of the y-range.
201 202 203 204 205 206 207 208 209 210 |
# File 'lib/graph.rb', line 201 def draw_func(f, n, color = 'red', auto_adjust_y = true) arr_x = @ivx.to_array(n) arr_y = Array.new(n, 0.0) for i in 0...n xi = arr_x[i] yi = f.at(xi) arr_y[i] = yi.to_f end draw(arr_x, arr_y, color, auto_adjust_y) end |
#grid_x(anArray, color = @grid_color) ⇒ Object
Setting vertical lines associated with an array of x-values (given as the first argument) in a color (given as the second argument).
100 101 102 103 104 105 106 107 |
# File 'lib/graph.rb', line 100 def grid_x(anArray, color=@grid_color) anArray.each{ |x| pa = ipos(x,@ivy.low) pb = ipos(x,@ivy.upp) line = TkcLine.new(@canvas,pa[0],pa[1],pb[0],pb[1]) line.fill(color) } end |
#grid_y(anArray, color = @grid_color) ⇒ Object
Setting horizontal lines associated with an array of y-values (given as the first argument) in a color (given as the second argument).
112 113 114 115 116 117 118 119 |
# File 'lib/graph.rb', line 112 def grid_y(anArray, color=@grid_color) anArray.each{ |y| pa = ipos(@ivx.low,y) pb = ipos(@ivx.upp,y) line = TkcLine.new(@canvas,pa[0],pa[1],pb[0],pb[1]) line.fill(color) } end |
#ipos(x, y) ⇒ Object
integer (pixel-wise) position associated with a point (x,y) in x,y-space. Notice that this is given by a simple (and efficient) formula if the auxiliar quantities @a, @b, @c, @d got their value by means of the next two functions.
217 218 219 |
# File 'lib/graph.rb', line 217 def ipos x, y [@a + @b * x, @c + @d * y] end |
#ivx ⇒ Object
Returns the interval of x-values.
84 |
# File 'lib/graph.rb', line 84 def ivx; @ivx; end |
#ivy ⇒ Object
Returns the interval of y-values.
87 |
# File 'lib/graph.rb', line 87 def ivy; @ivy; end |
#set_size_x!(aIv) ⇒ Object
Setting the active interval of x-values and the auxiliar quantities @a and @b.
223 224 225 226 227 |
# File 'lib/graph.rb', line 223 def set_size_x!(aIv) @ivx = aIv @b = (@w - 1.0)/@ivx.size @a = -@b * @ivx.low end |
#set_size_y!(aIv) ⇒ Object
Setting the active interval of y-values and the auxiliar quantities @c and @d.
231 232 233 234 235 |
# File 'lib/graph.rb', line 231 def set_size_y!(aIv) @ivy = aIv @d = (1.0 - @h)/@ivy.size @c = -@d * @ivy.upp end |