Advertisement
cherurg

Untitled

Feb 19th, 2015
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var container = new PlotContainer("plot");
  2. var controls = new app.Controls(container.addEmptyDiv());
  3.  
  4. var plot = container.addPlot({left:-10, right:10, top:100, bottom:0, height:400, width:500});
  5.  
  6. var mathFunction = function (x) {
  7.     return Math.pow(x, 2);
  8. };
  9.  
  10. drawGraph(mathFunction);
  11.  
  12. function drawGraph (mathematicalFunction) {
  13.     var options = {
  14.         left: -10,
  15.         right: 10,
  16.         top: 100,
  17.         bottom: 0
  18.     };
  19.  
  20.     var func = plot.addFunc(mathematicalFunction, options);
  21.     var n = 10;
  22.     var a = -10;
  23.     var b = 10;
  24.     var arr = [];
  25.     arr.push({x: a, y: 0});
  26.     arr.push({x: a, y: mathematicalFunction(a)});
  27.     arr.push({x: a + (b - a) / n, y: mathematicalFunction(a)});
  28.     arr.push({x: a + (b - a) / n, y: 0});
  29.  
  30.     var i;
  31.     for (i = 1; i < n; i++) {
  32.         arr.push({x: a + i * (b - a) / n, y: mathematicalFunction(a + i * (b - a) / n)});
  33.         arr.push({x: a + (i + 1) * (b - a) / n, y: mathematicalFunction(a + i * (b - a) / n)});
  34.         arr.push({x: a + (i + 1) * (b - a) / n, y: 0});
  35.     }
  36.     arr.push({x: b, y: 0});
  37.     var area = plot.addArea(arr, {strokeWidth: 2, color: 6, fillOpacity: 0.7, fill: 7});
  38.  
  39.  
  40.     function changeRange(value) {
  41.         range.setText(text + (value + 10));
  42.         plot.remove(func);
  43.         plot.remove(area);
  44.         var func = plot.addFunc(mathematicalFunction, options);
  45.  
  46.         var n = 10 + value;
  47.         var arr = [];
  48.         arr.push({x: a, y: 0});
  49.         arr.push({x: a, y: mathematicalFunction(a)});
  50.         arr.push({x: a + (b - a) / n, y: mathematicalFunction(a)});
  51.         arr.push({x: a + (b - a) / n, y: 0});
  52.  
  53.         for (i = 1; i < n; i++) {
  54.             arr.push({x: a + i * (b - a) / n, y: mathematicalFunction(a + i * (b - a) / n)});
  55.             arr.push({x: a + (i + 1) * (b - a) / n, y: mathematicalFunction(a + i * (b - a) / n)});
  56.             arr.push({x: a + (i + 1) * (b - a) / n, y: 0});
  57.         }
  58.         arr.push({x: b, y: 0});
  59.         var area = plot.addArea(arr, {strokeWidth: 2, color: 6, fillOpacity: 0.7, fill: 7});
  60.  
  61.     }
  62.  
  63.     var text = "n = ";
  64.     var text2 = " ,sum=";
  65.     var range = controls.addRange(changeRange, text + "10", 0, 120, 1, 0);
  66.  
  67. // controls.addButton(function () {
  68. //     }, "hello");
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement