1 /* 2 Copyright 2008-2024 3 Matthias Ehmann, 4 Michael Gerhaeuser, 5 Carsten Miller, 6 Bianca Valentin, 7 Alfred Wassermann, 8 Peter Wilfahrt 9 10 This file is part of JSXGraph. 11 12 JSXGraph is free software dual licensed under the GNU LGPL or MIT License. 13 14 You can redistribute it and/or modify it under the terms of the 15 16 * GNU Lesser General Public License as published by 17 the Free Software Foundation, either version 3 of the License, or 18 (at your option) any later version 19 OR 20 * MIT License: https://github.com/jsxgraph/jsxgraph/blob/master/LICENSE.MIT 21 22 JSXGraph is distributed in the hope that it will be useful, 23 but WITHOUT ANY WARRANTY; without even the implied warranty of 24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 25 GNU Lesser General Public License for more details. 26 27 You should have received a copy of the GNU Lesser General Public License and 28 the MIT License along with JSXGraph. If not, see <https://www.gnu.org/licenses/> 29 and <https://opensource.org/licenses/MIT/>. 30 */ 31 32 /*global JXG: true, define: true*/ 33 /*jslint nomen: true, plusplus: true*/ 34 35 /** 36 * @fileoverview In this file the geometry element Curve is defined. 37 */ 38 39 import JXG from "../jxg.js"; 40 import Clip from "../math/clip.js"; 41 import Const from "./constants.js"; 42 import Coords from "./coords.js"; 43 import Geometry from "../math/geometry.js"; 44 import GeometryElement from "./element.js"; 45 import GeonextParser from "../parser/geonext.js"; 46 import ImplicitPlot from "../math/implicitplot.js"; 47 import Mat from "../math/math.js"; 48 import Metapost from "../math/metapost.js"; 49 import Numerics from "../math/numerics.js"; 50 import Plot from "../math/plot.js"; 51 import QDT from "../math/qdt.js"; 52 import Type from "../utils/type.js"; 53 54 /** 55 * Curves are the common object for function graphs, parametric curves, polar curves, and data plots. 56 * @class Creates a new curve object. Do not use this constructor to create a curve. Use {@link JXG.Board#create} with 57 * type {@link Curve}, or {@link Functiongraph} instead. 58 * @augments JXG.GeometryElement 59 * @param {String|JXG.Board} board The board the new curve is drawn on. 60 * @param {Array} parents defining terms An array with the function terms or the data points of the curve. 61 * @param {Object} attributes Defines the visual appearance of the curve. 62 * @see JXG.Board#generateName 63 * @see JXG.Board#addCurve 64 */ 65 JXG.Curve = function (board, parents, attributes) { 66 this.constructor(board, attributes, Const.OBJECT_TYPE_CURVE, Const.OBJECT_CLASS_CURVE); 67 68 this.points = []; 69 /** 70 * Number of points on curves. This value changes 71 * between numberPointsLow and numberPointsHigh. 72 * It is set in {@link JXG.Curve#updateCurve}. 73 */ 74 this.numberPoints = this.evalVisProp('numberpointshigh'); 75 76 this.bezierDegree = 1; 77 78 /** 79 * Array holding the x-coordinates of a data plot. 80 * This array can be updated during run time by overwriting 81 * the method {@link JXG.Curve#updateDataArray}. 82 * @type array 83 */ 84 this.dataX = null; 85 86 /** 87 * Array holding the y-coordinates of a data plot. 88 * This array can be updated during run time by overwriting 89 * the method {@link JXG.Curve#updateDataArray}. 90 * @type array 91 */ 92 this.dataY = null; 93 94 /** 95 * Array of ticks storing all the ticks on this curve. Do not set this field directly and use 96 * {@link JXG.Curve#addTicks} and {@link JXG.Curve#removeTicks} to add and remove ticks to and 97 * from the curve. 98 * @type Array 99 * @see JXG.Ticks 100 */ 101 this.ticks = []; 102 103 /** 104 * Stores a quadtree if it is required. The quadtree is generated in the curve 105 * updates and can be used to speed up the hasPoint method. 106 * @type JXG.Math.Quadtree 107 */ 108 this.qdt = null; 109 110 if (Type.exists(parents[0])) { 111 this.varname = parents[0]; 112 } else { 113 this.varname = "x"; 114 } 115 116 // function graphs: "x" 117 this.xterm = parents[1]; 118 // function graphs: e.g. "x^2" 119 this.yterm = parents[2]; 120 121 // Converts GEONExT syntax into JavaScript syntax 122 this.generateTerm(this.varname, this.xterm, this.yterm, parents[3], parents[4]); 123 // First evaluation of the curve 124 this.updateCurve(); 125 126 this.id = this.board.setId(this, "G"); 127 this.board.renderer.drawCurve(this); 128 129 this.board.finalizeAdding(this); 130 131 this.createGradient(); 132 this.elType = "curve"; 133 this.createLabel(); 134 135 if (Type.isString(this.xterm)) { 136 this.notifyParents(this.xterm); 137 } 138 if (Type.isString(this.yterm)) { 139 this.notifyParents(this.yterm); 140 } 141 142 this.methodMap = Type.deepCopy(this.methodMap, { 143 generateTerm: "generateTerm", 144 setTerm: "generateTerm", 145 move: "moveTo", 146 moveTo: "moveTo", 147 MinX: "minX", 148 MaxX: "maxX" 149 }); 150 }; 151 152 JXG.Curve.prototype = new GeometryElement(); 153 154 JXG.extend( 155 JXG.Curve.prototype, 156 /** @lends JXG.Curve.prototype */ { 157 /** 158 * Gives the default value of the left bound for the curve. 159 * May be overwritten in {@link JXG.Curve#generateTerm}. 160 * @returns {Number} Left bound for the curve. 161 */ 162 minX: function () { 163 var leftCoords; 164 165 if (this.evalVisProp('curvetype') === "polar") { 166 return 0; 167 } 168 169 leftCoords = new Coords( 170 Const.COORDS_BY_SCREEN, 171 [-this.board.canvasWidth * 0.1, 0], 172 this.board, 173 false 174 ); 175 return leftCoords.usrCoords[1]; 176 }, 177 178 /** 179 * Gives the default value of the right bound for the curve. 180 * May be overwritten in {@link JXG.Curve#generateTerm}. 181 * @returns {Number} Right bound for the curve. 182 */ 183 maxX: function () { 184 var rightCoords; 185 186 if (this.evalVisProp('curvetype') === "polar") { 187 return 2 * Math.PI; 188 } 189 rightCoords = new Coords( 190 Const.COORDS_BY_SCREEN, 191 [this.board.canvasWidth * 1.1, 0], 192 this.board, 193 false 194 ); 195 196 return rightCoords.usrCoords[1]; 197 }, 198 199 /** 200 * The parametric function which defines the x-coordinate of the curve. 201 * @param {Number} t A number between {@link JXG.Curve#minX} and {@link JXG.Curve#maxX}. 202 * @param {Boolean} suspendUpdate A boolean flag which is false for the 203 * first call of the function during a fresh plot of the curve and true 204 * for all subsequent calls of the function. This may be used to speed up the 205 * plotting of the curve, if the e.g. the curve depends on some input elements. 206 * @returns {Number} x-coordinate of the curve at t. 207 */ 208 X: function (t) { 209 return NaN; 210 }, 211 212 /** 213 * The parametric function which defines the y-coordinate of the curve. 214 * @param {Number} t A number between {@link JXG.Curve#minX} and {@link JXG.Curve#maxX}. 215 * @param {Boolean} suspendUpdate A boolean flag which is false for the 216 * first call of the function during a fresh plot of the curve and true 217 * for all subsequent calls of the function. This may be used to speed up the 218 * plotting of the curve, if the e.g. the curve depends on some input elements. 219 * @returns {Number} y-coordinate of the curve at t. 220 */ 221 Y: function (t) { 222 return NaN; 223 }, 224 225 /** 226 * Treat the curve as curve with homogeneous coordinates. 227 * @param {Number} t A number between {@link JXG.Curve#minX} and {@link JXG.Curve#maxX}. 228 * @returns {Number} Always 1.0 229 */ 230 Z: function (t) { 231 return 1; 232 }, 233 234 /** 235 * Checks whether (x,y) is near the curve. 236 * @param {Number} x Coordinate in x direction, screen coordinates. 237 * @param {Number} y Coordinate in y direction, screen coordinates. 238 * @param {Number} start Optional start index for search on data plots. 239 * @returns {Boolean} True if (x,y) is near the curve, False otherwise. 240 */ 241 hasPoint: function (x, y, start) { 242 var t, c, i, tX, tY, 243 checkPoint, len, invMat, isIn, 244 res = [], 245 points, 246 qdt, 247 steps = this.evalVisProp('numberpointslow'), 248 d = (this.maxX() - this.minX()) / steps, 249 prec, type, 250 dist = Infinity, 251 ux2, uy2, 252 ev_ct, 253 mi, ma, 254 suspendUpdate = true; 255 256 if (Type.isObject(this.evalVisProp('precision'))) { 257 type = this.board._inputDevice; 258 prec = this.evalVisProp('precision.' + type); 259 } else { 260 // 'inherit' 261 prec = this.board.options.precision.hasPoint; 262 } 263 264 // From now on, x,y are usrCoords 265 checkPoint = new Coords(Const.COORDS_BY_SCREEN, [x, y], this.board, false); 266 x = checkPoint.usrCoords[1]; 267 y = checkPoint.usrCoords[2]; 268 269 // Handle inner points of the curve 270 if (this.bezierDegree === 1 && this.evalVisProp('hasinnerpoints')) { 271 isIn = Geometry.windingNumber([1, x, y], this.points, true); 272 if (isIn !== 0) { 273 return true; 274 } 275 } 276 277 // We use usrCoords. Only in the final distance calculation 278 // screen coords are used 279 prec += this.evalVisProp('strokewidth') * 0.5; 280 prec *= prec; // We do not want to take sqrt 281 ux2 = this.board.unitX * this.board.unitX; 282 uy2 = this.board.unitY * this.board.unitY; 283 284 mi = this.minX(); 285 ma = this.maxX(); 286 if (Type.exists(this._visibleArea)) { 287 mi = this._visibleArea[0]; 288 ma = this._visibleArea[1]; 289 d = (ma - mi) / steps; 290 } 291 292 ev_ct = this.evalVisProp('curvetype'); 293 if (ev_ct === "parameter" || ev_ct === "polar") { 294 // Transform the mouse/touch coordinates 295 // back to the original position of the curve. 296 // This is needed, because we work with the function terms, not the points. 297 if (this.transformations.length > 0) { 298 this.updateTransformMatrix(); 299 invMat = Mat.inverse(this.transformMat); 300 c = Mat.matVecMult(invMat, [1, x, y]); 301 x = c[1]; 302 y = c[2]; 303 } 304 305 // Brute force search for a point on the curve close to the mouse pointer 306 for (i = 0, t = mi; i < steps; i++) { 307 tX = this.X(t, suspendUpdate); 308 tY = this.Y(t, suspendUpdate); 309 310 dist = (x - tX) * (x - tX) * ux2 + (y - tY) * (y - tY) * uy2; 311 312 if (dist <= prec) { 313 return true; 314 } 315 316 t += d; 317 } 318 } else if (ev_ct === "plot" || ev_ct === "functiongraph") { 319 // Here, we can ignore transformations of the curve, 320 // since we are working directly with the points. 321 322 if (!Type.exists(start) || start < 0) { 323 start = 0; 324 } 325 326 if ( 327 Type.exists(this.qdt) && 328 this.evalVisProp('useqdt') && 329 this.bezierDegree !== 3 330 ) { 331 qdt = this.qdt.query(new Coords(Const.COORDS_BY_USER, [x, y], this.board)); 332 points = qdt.points; 333 len = points.length; 334 } else { 335 points = this.points; 336 len = this.numberPoints - 1; 337 } 338 339 for (i = start; i < len; i++) { 340 if (this.bezierDegree === 3) { 341 //res.push(Geometry.projectCoordsToBeziersegment([1, x, y], this, i)); 342 res = Geometry.projectCoordsToBeziersegment([1, x, y], this, i); 343 } else { 344 if (qdt) { 345 if (points[i].prev) { 346 res = Geometry.projectCoordsToSegment( 347 [1, x, y], 348 points[i].prev.usrCoords, 349 points[i].usrCoords 350 ); 351 } 352 353 // If the next point in the array is the same as the current points 354 // next neighbor we don't have to project it onto that segment because 355 // that will already be done in the next iteration of this loop. 356 if (points[i].next && points[i + 1] !== points[i].next) { 357 res = Geometry.projectCoordsToSegment( 358 [1, x, y], 359 points[i].usrCoords, 360 points[i].next.usrCoords 361 ); 362 } 363 } else { 364 res = Geometry.projectCoordsToSegment( 365 [1, x, y], 366 points[i].usrCoords, 367 points[i + 1].usrCoords 368 ); 369 } 370 } 371 372 if ( 373 res[1] >= 0 && 374 res[1] <= 1 && 375 (x - res[0][1]) * (x - res[0][1]) * ux2 + 376 (y - res[0][2]) * (y - res[0][2]) * uy2 <= 377 prec 378 ) { 379 return true; 380 } 381 } 382 return false; 383 } 384 return dist < prec; 385 }, 386 387 /** 388 * Allocate points in the Coords array this.points 389 */ 390 allocatePoints: function () { 391 var i, len; 392 393 len = this.numberPoints; 394 395 if (this.points.length < this.numberPoints) { 396 for (i = this.points.length; i < len; i++) { 397 this.points[i] = new Coords( 398 Const.COORDS_BY_USER, 399 [0, 0], 400 this.board, 401 false 402 ); 403 } 404 } 405 }, 406 407 /** 408 * Computes for equidistant points on the x-axis the values of the function 409 * @returns {JXG.Curve} Reference to the curve object. 410 * @see JXG.Curve#updateCurve 411 */ 412 update: function () { 413 if (this.needsUpdate) { 414 if (this.evalVisProp('trace')) { 415 this.cloneToBackground(true); 416 } 417 this.updateCurve(); 418 } 419 420 return this; 421 }, 422 423 /** 424 * Updates the visual contents of the curve. 425 * @returns {JXG.Curve} Reference to the curve object. 426 */ 427 updateRenderer: function () { 428 //var wasReal; 429 430 if (!this.needsUpdate) { 431 return this; 432 } 433 434 if (this.visPropCalc.visible) { 435 // wasReal = this.isReal; 436 437 this.isReal = Plot.checkReal(this.points); 438 439 if ( 440 //wasReal && 441 !this.isReal 442 ) { 443 this.updateVisibility(false); 444 } 445 } 446 447 if (this.visPropCalc.visible) { 448 this.board.renderer.updateCurve(this); 449 } 450 451 /* Update the label if visible. */ 452 if ( 453 this.hasLabel && 454 this.visPropCalc.visible && 455 this.label && 456 this.label.visPropCalc.visible && 457 this.isReal 458 ) { 459 this.label.update(); 460 this.board.renderer.updateText(this.label); 461 } 462 463 // Update rendNode display 464 this.setDisplayRendNode(); 465 // if (this.visPropCalc.visible !== this.visPropOld.visible) { 466 // this.board.renderer.display(this, this.visPropCalc.visible); 467 // this.visPropOld.visible = this.visPropCalc.visible; 468 // 469 // if (this.hasLabel) { 470 // this.board.renderer.display(this.label, this.label.visPropCalc.visible); 471 // } 472 // } 473 474 this.needsUpdate = false; 475 return this; 476 }, 477 478 /** 479 * For dynamic dataplots updateCurve can be used to compute new entries 480 * for the arrays {@link JXG.Curve#dataX} and {@link JXG.Curve#dataY}. It 481 * is used in {@link JXG.Curve#updateCurve}. Default is an empty method, can 482 * be overwritten by the user. 483 * 484 * 485 * @example 486 * // This example overwrites the updateDataArray method. 487 * // There, new values for the arrays JXG.Curve.dataX and JXG.Curve.dataY 488 * // are computed from the value of the slider N 489 * 490 * var N = board.create('slider', [[0,1.5],[3,1.5],[1,3,40]], {name:'n',snapWidth:1}); 491 * var circ = board.create('circle',[[4,-1.5],1],{strokeWidth:1, strokecolor:'black', strokeWidth:2, 492 * fillColor:'#0055ff13'}); 493 * 494 * var c = board.create('curve', [[0],[0]],{strokecolor:'red', strokeWidth:2}); 495 * c.updateDataArray = function() { 496 * var r = 1, n = Math.floor(N.Value()), 497 * x = [0], y = [0], 498 * phi = Math.PI/n, 499 * h = r*Math.cos(phi), 500 * s = r*Math.sin(phi), 501 * i, j, 502 * px = 0, py = 0, sgn = 1, 503 * d = 16, 504 * dt = phi/d, 505 * pt; 506 * 507 * for (i = 0; i < n; i++) { 508 * for (j = -d; j <= d; j++) { 509 * pt = dt*j; 510 * x.push(px + r*Math.sin(pt)); 511 * y.push(sgn*r*Math.cos(pt) - (sgn-1)*h*0.5); 512 * } 513 * px += s; 514 * sgn *= (-1); 515 * } 516 * x.push((n - 1)*s); 517 * y.push(h + (sgn - 1)*h*0.5); 518 * this.dataX = x; 519 * this.dataY = y; 520 * } 521 * 522 * var c2 = board.create('curve', [[0],[0]],{strokecolor:'red', strokeWidth:1}); 523 * c2.updateDataArray = function() { 524 * var r = 1, n = Math.floor(N.Value()), 525 * px = circ.midpoint.X(), py = circ.midpoint.Y(), 526 * x = [px], y = [py], 527 * phi = Math.PI/n, 528 * s = r*Math.sin(phi), 529 * i, j, 530 * d = 16, 531 * dt = phi/d, 532 * pt = Math.PI*0.5+phi; 533 * 534 * for (i = 0; i < n; i++) { 535 * for (j= -d; j <= d; j++) { 536 * x.push(px + r*Math.cos(pt)); 537 * y.push(py + r*Math.sin(pt)); 538 * pt -= dt; 539 * } 540 * x.push(px); 541 * y.push(py); 542 * pt += dt; 543 * } 544 * this.dataX = x; 545 * this.dataY = y; 546 * } 547 * board.update(); 548 * 549 * </pre><div id="JXG20bc7802-e69e-11e5-b1bf-901b0e1b8723" class="jxgbox" style="width: 600px; height: 400px;"></div> 550 * <script type="text/javascript"> 551 * (function() { 552 * var board = JXG.JSXGraph.initBoard('JXG20bc7802-e69e-11e5-b1bf-901b0e1b8723', 553 * {boundingbox: [-1.5,2,8,-3], keepaspectratio: true, axis: true, showcopyright: false, shownavigation: false}); 554 * var N = board.create('slider', [[0,1.5],[3,1.5],[1,3,40]], {name:'n',snapWidth:1}); 555 * var circ = board.create('circle',[[4,-1.5],1],{strokeWidth:1, strokecolor:'black', 556 * strokeWidth:2, fillColor:'#0055ff13'}); 557 * 558 * var c = board.create('curve', [[0],[0]],{strokecolor:'red', strokeWidth:2}); 559 * c.updateDataArray = function() { 560 * var r = 1, n = Math.floor(N.Value()), 561 * x = [0], y = [0], 562 * phi = Math.PI/n, 563 * h = r*Math.cos(phi), 564 * s = r*Math.sin(phi), 565 * i, j, 566 * px = 0, py = 0, sgn = 1, 567 * d = 16, 568 * dt = phi/d, 569 * pt; 570 * 571 * for (i=0;i<n;i++) { 572 * for (j=-d;j<=d;j++) { 573 * pt = dt*j; 574 * x.push(px+r*Math.sin(pt)); 575 * y.push(sgn*r*Math.cos(pt)-(sgn-1)*h*0.5); 576 * } 577 * px += s; 578 * sgn *= (-1); 579 * } 580 * x.push((n-1)*s); 581 * y.push(h+(sgn-1)*h*0.5); 582 * this.dataX = x; 583 * this.dataY = y; 584 * } 585 * 586 * var c2 = board.create('curve', [[0],[0]],{strokecolor:'red', strokeWidth:1}); 587 * c2.updateDataArray = function() { 588 * var r = 1, n = Math.floor(N.Value()), 589 * px = circ.midpoint.X(), py = circ.midpoint.Y(), 590 * x = [px], y = [py], 591 * phi = Math.PI/n, 592 * s = r*Math.sin(phi), 593 * i, j, 594 * d = 16, 595 * dt = phi/d, 596 * pt = Math.PI*0.5+phi; 597 * 598 * for (i=0;i<n;i++) { 599 * for (j=-d;j<=d;j++) { 600 * x.push(px+r*Math.cos(pt)); 601 * y.push(py+r*Math.sin(pt)); 602 * pt -= dt; 603 * } 604 * x.push(px); 605 * y.push(py); 606 * pt += dt; 607 * } 608 * this.dataX = x; 609 * this.dataY = y; 610 * } 611 * board.update(); 612 * 613 * })(); 614 * 615 * </script><pre> 616 * 617 * @example 618 * // This is an example which overwrites updateDataArray and produces 619 * // a Bezier curve of degree three. 620 * var A = board.create('point', [-3,3]); 621 * var B = board.create('point', [3,-2]); 622 * var line = board.create('segment', [A,B]); 623 * 624 * var height = 0.5; // height of the curly brace 625 * 626 * // Curly brace 627 * var crl = board.create('curve', [[0],[0]], {strokeWidth:1, strokeColor:'black'}); 628 * crl.bezierDegree = 3; 629 * crl.updateDataArray = function() { 630 * var d = [B.X()-A.X(), B.Y()-A.Y()], 631 * dl = Math.sqrt(d[0]*d[0]+d[1]*d[1]), 632 * mid = [(A.X()+B.X())*0.5, (A.Y()+B.Y())*0.5]; 633 * 634 * d[0] *= height/dl; 635 * d[1] *= height/dl; 636 * 637 * this.dataX = [ A.X(), A.X()-d[1], mid[0], mid[0]-d[1], mid[0], B.X()-d[1], B.X() ]; 638 * this.dataY = [ A.Y(), A.Y()+d[0], mid[1], mid[1]+d[0], mid[1], B.Y()+d[0], B.Y() ]; 639 * }; 640 * 641 * // Text 642 * var txt = board.create('text', [ 643 * function() { 644 * var d = [B.X()-A.X(), B.Y()-A.Y()], 645 * dl = Math.sqrt(d[0]*d[0]+d[1]*d[1]), 646 * mid = (A.X()+B.X())*0.5; 647 * 648 * d[1] *= height/dl; 649 * return mid-d[1]+0.1; 650 * }, 651 * function() { 652 * var d = [B.X()-A.X(), B.Y()-A.Y()], 653 * dl = Math.sqrt(d[0]*d[0]+d[1]*d[1]), 654 * mid = (A.Y()+B.Y())*0.5; 655 * 656 * d[0] *= height/dl; 657 * return mid+d[0]+0.1; 658 * }, 659 * function() { return "length=" + JXG.toFixed(B.Dist(A), 2); } 660 * ]); 661 * 662 * 663 * board.update(); // This update is necessary to call updateDataArray the first time. 664 * 665 * </pre><div id="JXGa61a4d66-e69f-11e5-b1bf-901b0e1b8723" class="jxgbox" style="width: 300px; height: 300px;"></div> 666 * <script type="text/javascript"> 667 * (function() { 668 * var board = JXG.JSXGraph.initBoard('JXGa61a4d66-e69f-11e5-b1bf-901b0e1b8723', 669 * {boundingbox: [-4, 4, 4,-4], axis: true, showcopyright: false, shownavigation: false}); 670 * var A = board.create('point', [-3,3]); 671 * var B = board.create('point', [3,-2]); 672 * var line = board.create('segment', [A,B]); 673 * 674 * var height = 0.5; // height of the curly brace 675 * 676 * // Curly brace 677 * var crl = board.create('curve', [[0],[0]], {strokeWidth:1, strokeColor:'black'}); 678 * crl.bezierDegree = 3; 679 * crl.updateDataArray = function() { 680 * var d = [B.X()-A.X(), B.Y()-A.Y()], 681 * dl = Math.sqrt(d[0]*d[0]+d[1]*d[1]), 682 * mid = [(A.X()+B.X())*0.5, (A.Y()+B.Y())*0.5]; 683 * 684 * d[0] *= height/dl; 685 * d[1] *= height/dl; 686 * 687 * this.dataX = [ A.X(), A.X()-d[1], mid[0], mid[0]-d[1], mid[0], B.X()-d[1], B.X() ]; 688 * this.dataY = [ A.Y(), A.Y()+d[0], mid[1], mid[1]+d[0], mid[1], B.Y()+d[0], B.Y() ]; 689 * }; 690 * 691 * // Text 692 * var txt = board.create('text', [ 693 * function() { 694 * var d = [B.X()-A.X(), B.Y()-A.Y()], 695 * dl = Math.sqrt(d[0]*d[0]+d[1]*d[1]), 696 * mid = (A.X()+B.X())*0.5; 697 * 698 * d[1] *= height/dl; 699 * return mid-d[1]+0.1; 700 * }, 701 * function() { 702 * var d = [B.X()-A.X(), B.Y()-A.Y()], 703 * dl = Math.sqrt(d[0]*d[0]+d[1]*d[1]), 704 * mid = (A.Y()+B.Y())*0.5; 705 * 706 * d[0] *= height/dl; 707 * return mid+d[0]+0.1; 708 * }, 709 * function() { return "length="+JXG.toFixed(B.Dist(A), 2); } 710 * ]); 711 * 712 * 713 * board.update(); // This update is necessary to call updateDataArray the first time. 714 * 715 * })(); 716 * 717 * </script><pre> 718 * 719 * 720 */ 721 updateDataArray: function () { 722 // this used to return this, but we shouldn't rely on the user to implement it. 723 }, 724 725 /** 726 * Computes the curve path 727 * @see JXG.Curve#update 728 * @returns {JXG.Curve} Reference to the curve object. 729 */ 730 updateCurve: function () { 731 var i, len, mi, ma, 732 x, y, 733 version = this.visProp.plotversion, 734 //t1, t2, l1, 735 suspendUpdate = false; 736 737 this.updateTransformMatrix(); 738 this.updateDataArray(); 739 mi = this.minX(); 740 ma = this.maxX(); 741 742 // Discrete data points 743 // x-coordinates are in an array 744 if (Type.exists(this.dataX)) { 745 this.numberPoints = this.dataX.length; 746 len = this.numberPoints; 747 748 // It is possible, that the array length has increased. 749 this.allocatePoints(); 750 751 for (i = 0; i < len; i++) { 752 x = i; 753 754 // y-coordinates are in an array 755 if (Type.exists(this.dataY)) { 756 y = i; 757 // The last parameter prevents rounding in usr2screen(). 758 this.points[i].setCoordinates( 759 Const.COORDS_BY_USER, 760 [this.dataX[i], this.dataY[i]], 761 false 762 ); 763 } else { 764 // discrete x data, continuous y data 765 y = this.X(x); 766 // The last parameter prevents rounding in usr2screen(). 767 this.points[i].setCoordinates( 768 Const.COORDS_BY_USER, 769 [this.dataX[i], this.Y(y, suspendUpdate)], 770 false 771 ); 772 } 773 this.points[i]._t = i; 774 775 // this.updateTransform(this.points[i]); 776 suspendUpdate = true; 777 } 778 // continuous x data 779 } else { 780 if (this.evalVisProp('doadvancedplot')) { 781 // console.time("plot"); 782 783 if (version === 1 || this.evalVisProp('doadvancedplotold')) { 784 Plot.updateParametricCurveOld(this, mi, ma); 785 } else if (version === 2) { 786 Plot.updateParametricCurve_v2(this, mi, ma); 787 } else if (version === 3) { 788 Plot.updateParametricCurve_v3(this, mi, ma); 789 } else if (version === 4) { 790 Plot.updateParametricCurve_v4(this, mi, ma); 791 } else { 792 Plot.updateParametricCurve_v2(this, mi, ma); 793 } 794 // console.timeEnd("plot"); 795 } else { 796 if (this.board.updateQuality === this.board.BOARD_QUALITY_HIGH) { 797 this.numberPoints = this.evalVisProp('numberpointshigh'); 798 } else { 799 this.numberPoints = this.evalVisProp('numberpointslow'); 800 } 801 802 // It is possible, that the array length has increased. 803 this.allocatePoints(); 804 Plot.updateParametricCurveNaive(this, mi, ma, this.numberPoints); 805 } 806 len = this.numberPoints; 807 808 if ( 809 this.evalVisProp('useqdt') && 810 this.board.updateQuality === this.board.BOARD_QUALITY_HIGH 811 ) { 812 this.qdt = new QDT(this.board.getBoundingBox()); 813 for (i = 0; i < this.points.length; i++) { 814 this.qdt.insert(this.points[i]); 815 816 if (i > 0) { 817 this.points[i].prev = this.points[i - 1]; 818 } 819 820 if (i < len - 1) { 821 this.points[i].next = this.points[i + 1]; 822 } 823 } 824 } 825 826 // for (i = 0; i < len; i++) { 827 // this.updateTransform(this.points[i]); 828 // } 829 } 830 831 if ( 832 this.evalVisProp('curvetype') !== "plot" && 833 this.evalVisProp('rdpsmoothing') 834 ) { 835 // console.time("rdp"); 836 this.points = Numerics.RamerDouglasPeucker(this.points, 0.2); 837 this.numberPoints = this.points.length; 838 // console.timeEnd("rdp"); 839 // console.log(this.numberPoints); 840 } 841 842 len = this.numberPoints; 843 for (i = 0; i < len; i++) { 844 this.updateTransform(this.points[i]); 845 } 846 847 return this; 848 }, 849 850 updateTransformMatrix: function () { 851 var t, 852 i, 853 len = this.transformations.length; 854 855 this.transformMat = [ 856 [1, 0, 0], 857 [0, 1, 0], 858 [0, 0, 1] 859 ]; 860 861 for (i = 0; i < len; i++) { 862 t = this.transformations[i]; 863 t.update(); 864 this.transformMat = Mat.matMatMult(t.matrix, this.transformMat); 865 } 866 867 return this; 868 }, 869 870 /** 871 * Applies the transformations of the curve to the given point <tt>p</tt>. 872 * Before using it, {@link JXG.Curve#updateTransformMatrix} has to be called. 873 * @param {JXG.Point} p 874 * @returns {JXG.Point} The given point. 875 */ 876 updateTransform: function (p) { 877 var c, 878 len = this.transformations.length; 879 880 if (len > 0) { 881 c = Mat.matVecMult(this.transformMat, p.usrCoords); 882 p.setCoordinates(Const.COORDS_BY_USER, c, false, true); 883 } 884 885 return p; 886 }, 887 888 /** 889 * Add transformations to this curve. 890 * @param {JXG.Transformation|Array} transform Either one {@link JXG.Transformation} or an array of {@link JXG.Transformation}s. 891 * @returns {JXG.Curve} Reference to the curve object. 892 */ 893 addTransform: function (transform) { 894 var i, 895 list = Type.isArray(transform) ? transform : [transform], 896 len = list.length; 897 898 for (i = 0; i < len; i++) { 899 this.transformations.push(list[i]); 900 } 901 902 return this; 903 }, 904 905 /** 906 * Generate the method curve.X() in case curve.dataX is an array 907 * and generate the method curve.Y() in case curve.dataY is an array. 908 * @private 909 * @param {String} which Either 'X' or 'Y' 910 * @returns {function} 911 **/ 912 interpolationFunctionFromArray: function (which) { 913 var data = "data" + which, 914 that = this; 915 916 return function (t, suspendedUpdate) { 917 var i, 918 j, 919 t0, 920 t1, 921 arr = that[data], 922 len = arr.length, 923 last, 924 f = []; 925 926 if (isNaN(t)) { 927 return NaN; 928 } 929 930 if (t < 0) { 931 if (Type.isFunction(arr[0])) { 932 return arr[0](); 933 } 934 935 return arr[0]; 936 } 937 938 if (that.bezierDegree === 3) { 939 last = (len - 1) / 3; 940 941 if (t >= last) { 942 if (Type.isFunction(arr[arr.length - 1])) { 943 return arr[arr.length - 1](); 944 } 945 946 return arr[arr.length - 1]; 947 } 948 949 i = Math.floor(t) * 3; 950 t0 = t % 1; 951 t1 = 1 - t0; 952 953 for (j = 0; j < 4; j++) { 954 if (Type.isFunction(arr[i + j])) { 955 f[j] = arr[i + j](); 956 } else { 957 f[j] = arr[i + j]; 958 } 959 } 960 961 return ( 962 t1 * t1 * (t1 * f[0] + 3 * t0 * f[1]) + 963 (3 * t1 * f[2] + t0 * f[3]) * t0 * t0 964 ); 965 } 966 967 if (t > len - 2) { 968 i = len - 2; 969 } else { 970 i = parseInt(Math.floor(t), 10); 971 } 972 973 if (i === t) { 974 if (Type.isFunction(arr[i])) { 975 return arr[i](); 976 } 977 return arr[i]; 978 } 979 980 for (j = 0; j < 2; j++) { 981 if (Type.isFunction(arr[i + j])) { 982 f[j] = arr[i + j](); 983 } else { 984 f[j] = arr[i + j]; 985 } 986 } 987 return f[0] + (f[1] - f[0]) * (t - i); 988 }; 989 }, 990 991 /** 992 * Converts the JavaScript/JessieCode/GEONExT syntax of the defining function term into JavaScript. 993 * New methods X() and Y() for the Curve object are generated, further 994 * new methods for minX() and maxX(). 995 * If mi or ma are not supplied, default functions are set. 996 * 997 * @param {String} varname Name of the parameter in xterm and yterm, e.g. 'x' or 't' 998 * @param {String|Number|Function|Array} xterm Term for the x coordinate. Can also be an array consisting of discrete values. 999 * @param {String|Number|Function|Array} yterm Term for the y coordinate. Can also be an array consisting of discrete values. 1000 * @param {String|Number|Function} [mi] Lower bound on the parameter 1001 * @param {String|Number|Function} [ma] Upper bound on the parameter 1002 * @see JXG.GeonextParser.geonext2JS 1003 */ 1004 generateTerm: function (varname, xterm, yterm, mi, ma) { 1005 var fx, fy; 1006 1007 // Generate the methods X() and Y() 1008 if (Type.isArray(xterm)) { 1009 // Discrete data 1010 this.dataX = xterm; 1011 1012 this.numberPoints = this.dataX.length; 1013 this.X = this.interpolationFunctionFromArray.apply(this, ["X"]); 1014 this.visProp.curvetype = "plot"; 1015 this.isDraggable = true; 1016 } else { 1017 // Continuous data 1018 this.X = Type.createFunction(xterm, this.board, varname); 1019 if (Type.isString(xterm)) { 1020 this.visProp.curvetype = "functiongraph"; 1021 } else if (Type.isFunction(xterm) || Type.isNumber(xterm)) { 1022 this.visProp.curvetype = "parameter"; 1023 } 1024 1025 this.isDraggable = true; 1026 } 1027 1028 if (Type.isArray(yterm)) { 1029 this.dataY = yterm; 1030 this.Y = this.interpolationFunctionFromArray.apply(this, ["Y"]); 1031 } else { 1032 this.Y = Type.createFunction(yterm, this.board, varname); 1033 } 1034 1035 /** 1036 * Polar form 1037 * Input data is function xterm() and offset coordinates yterm 1038 */ 1039 if (Type.isFunction(xterm) && Type.isArray(yterm)) { 1040 // Xoffset, Yoffset 1041 fx = Type.createFunction(yterm[0], this.board, ""); 1042 fy = Type.createFunction(yterm[1], this.board, ""); 1043 1044 this.X = function (phi) { 1045 return xterm(phi) * Math.cos(phi) + fx(); 1046 }; 1047 this.X.deps = fx.deps; 1048 1049 this.Y = function (phi) { 1050 return xterm(phi) * Math.sin(phi) + fy(); 1051 }; 1052 this.Y.deps = fy.deps; 1053 1054 this.visProp.curvetype = "polar"; 1055 } 1056 1057 // Set the upper and lower bounds for the parameter of the curve. 1058 // If not defined, reset the bounds to the default values 1059 // given in Curve.prototype.minX, Curve.prototype.maxX 1060 if (Type.exists(mi)) { 1061 this.minX = Type.createFunction(mi, this.board, ""); 1062 } else { 1063 delete this.minX; 1064 } 1065 if (Type.exists(ma)) { 1066 this.maxX = Type.createFunction(ma, this.board, ""); 1067 } else { 1068 delete this.maxX; 1069 } 1070 1071 this.addParentsFromJCFunctions([this.X, this.Y, this.minX, this.maxX]); 1072 }, 1073 1074 /** 1075 * Finds dependencies in a given term and notifies the parents by adding the 1076 * dependent object to the found objects child elements. 1077 * @param {String} contentStr String containing dependencies for the given object. 1078 */ 1079 notifyParents: function (contentStr) { 1080 var fstr, 1081 dep, 1082 isJessieCode = false, 1083 obj; 1084 1085 // Read dependencies found by the JessieCode parser 1086 obj = { xterm: 1, yterm: 1 }; 1087 for (fstr in obj) { 1088 if ( 1089 obj.hasOwnProperty(fstr) && 1090 this.hasOwnProperty(fstr) && 1091 this[fstr].origin 1092 ) { 1093 isJessieCode = true; 1094 for (dep in this[fstr].origin.deps) { 1095 if (this[fstr].origin.deps.hasOwnProperty(dep)) { 1096 this[fstr].origin.deps[dep].addChild(this); 1097 } 1098 } 1099 } 1100 } 1101 1102 if (!isJessieCode) { 1103 GeonextParser.findDependencies(this, contentStr, this.board); 1104 } 1105 }, 1106 1107 // documented in geometry element 1108 getLabelAnchor: function () { 1109 var x, y, pos, 1110 xy, lbda, e, 1111 t, dx, dy, d, 1112 dist = 1.5, 1113 c, 1114 ax = 0.05 * this.board.canvasWidth, 1115 ay = 0.05 * this.board.canvasHeight, 1116 bx = 0.95 * this.board.canvasWidth, 1117 by = 0.95 * this.board.canvasHeight; 1118 1119 if (!Type.exists(this.label)) { 1120 return new Coords(Const.COORDS_BY_SCREEN, [NaN, NaN], this.board); 1121 } 1122 pos = this.label.evalVisProp('position'); 1123 if (!Type.isString(pos)) { 1124 return new Coords(Const.COORDS_BY_SCREEN, [NaN, NaN], this.board); 1125 } 1126 1127 if (pos.indexOf('right') < 0 && pos.indexOf('left') < 0) { 1128 switch (this.evalVisProp('label.position')) { 1129 case "ulft": 1130 x = ax; 1131 y = ay; 1132 break; 1133 case "llft": 1134 x = ax; 1135 y = by; 1136 break; 1137 case "rt": 1138 x = bx; 1139 y = 0.5 * by; 1140 break; 1141 case "lrt": 1142 x = bx; 1143 y = by; 1144 break; 1145 case "urt": 1146 x = bx; 1147 y = ay; 1148 break; 1149 case "top": 1150 x = 0.5 * bx; 1151 y = ay; 1152 break; 1153 case "bot": 1154 x = 0.5 * bx; 1155 y = by; 1156 break; 1157 default: 1158 // includes case 'lft' 1159 x = ax; 1160 y = 0.5 * by; 1161 } 1162 } else { 1163 // New positioning 1164 xy = Type.parsePosition(pos); 1165 lbda = Type.parseNumber(xy.pos, this.maxX() - this.minX(), 1); 1166 1167 if (xy.pos.indexOf('fr') < 0 && 1168 xy.pos.indexOf('%') < 0) { 1169 // 'px' or numbers are not supported 1170 lbda = 0; 1171 } 1172 1173 t = this.minX() + lbda; 1174 x = this.X(t); 1175 y = this.Y(t); 1176 c = (new Coords(Const.COORDS_BY_USER, [x, y], this.board)).scrCoords; 1177 1178 e = Mat.eps; 1179 if (t < this.minX() + e) { 1180 dx = (this.X(t + e) - this.X(t)) / e; 1181 dy = (this.Y(t + e) - this.Y(t)) / e; 1182 } else if (t > this.maxX() - e) { 1183 dx = (this.X(t) - this.X(t - e)) / e; 1184 dy = (this.Y(t) - this.Y(t - e)) / e; 1185 } else { 1186 dx = 0.5 * (this.X(t + e) - this.X(t - e)) / e; 1187 dy = 0.5 * (this.Y(t + e) - this.Y(t - e)) / e; 1188 } 1189 d = Mat.hypot(dx, dy); 1190 1191 if (xy.side === 'left') { 1192 dy *= -1; 1193 } else { 1194 dx *= -1; 1195 } 1196 1197 // Position left or right 1198 1199 if (Type.exists(this.label)) { 1200 dist = 0.5 * this.label.evalVisProp('distance') / d; 1201 } 1202 1203 x = c[1] + dy * this.label.size[0] * dist; 1204 y = c[2] - dx * this.label.size[1] * dist; 1205 1206 return new Coords(Const.COORDS_BY_SCREEN, [x, y], this.board); 1207 1208 } 1209 c = new Coords(Const.COORDS_BY_SCREEN, [x, y], this.board, false); 1210 return Geometry.projectCoordsToCurve( 1211 c.usrCoords[1], c.usrCoords[2], 0, this, this.board 1212 )[0]; 1213 }, 1214 1215 // documented in geometry element 1216 cloneToBackground: function () { 1217 var er, 1218 copy = Type.getCloneObject(this); 1219 1220 copy.points = this.points.slice(0); 1221 copy.bezierDegree = this.bezierDegree; 1222 copy.numberPoints = this.numberPoints; 1223 1224 er = this.board.renderer.enhancedRendering; 1225 this.board.renderer.enhancedRendering = true; 1226 this.board.renderer.drawCurve(copy); 1227 this.board.renderer.enhancedRendering = er; 1228 this.traces[copy.id] = copy.rendNode; 1229 1230 return this; 1231 }, 1232 1233 // Already documented in GeometryElement 1234 bounds: function () { 1235 var minX = Infinity, 1236 maxX = -Infinity, 1237 minY = Infinity, 1238 maxY = -Infinity, 1239 l = this.points.length, 1240 i, 1241 bezier, 1242 up; 1243 1244 if (this.bezierDegree === 3) { 1245 // Add methods X(), Y() 1246 for (i = 0; i < l; i++) { 1247 this.points[i].X = Type.bind(function () { 1248 return this.usrCoords[1]; 1249 }, this.points[i]); 1250 this.points[i].Y = Type.bind(function () { 1251 return this.usrCoords[2]; 1252 }, this.points[i]); 1253 } 1254 bezier = Numerics.bezier(this.points); 1255 up = bezier[3](); 1256 minX = Numerics.fminbr( 1257 function (t) { 1258 return bezier[0](t); 1259 }, 1260 [0, up] 1261 ); 1262 maxX = Numerics.fminbr( 1263 function (t) { 1264 return -bezier[0](t); 1265 }, 1266 [0, up] 1267 ); 1268 minY = Numerics.fminbr( 1269 function (t) { 1270 return bezier[1](t); 1271 }, 1272 [0, up] 1273 ); 1274 maxY = Numerics.fminbr( 1275 function (t) { 1276 return -bezier[1](t); 1277 }, 1278 [0, up] 1279 ); 1280 1281 minX = bezier[0](minX); 1282 maxX = bezier[0](maxX); 1283 minY = bezier[1](minY); 1284 maxY = bezier[1](maxY); 1285 return [minX, maxY, maxX, minY]; 1286 } 1287 1288 // Linear segments 1289 for (i = 0; i < l; i++) { 1290 if (minX > this.points[i].usrCoords[1]) { 1291 minX = this.points[i].usrCoords[1]; 1292 } 1293 1294 if (maxX < this.points[i].usrCoords[1]) { 1295 maxX = this.points[i].usrCoords[1]; 1296 } 1297 1298 if (minY > this.points[i].usrCoords[2]) { 1299 minY = this.points[i].usrCoords[2]; 1300 } 1301 1302 if (maxY < this.points[i].usrCoords[2]) { 1303 maxY = this.points[i].usrCoords[2]; 1304 } 1305 } 1306 1307 return [minX, maxY, maxX, minY]; 1308 }, 1309 1310 // documented in element.js 1311 getParents: function () { 1312 var p = [this.xterm, this.yterm, this.minX(), this.maxX()]; 1313 1314 if (this.parents.length !== 0) { 1315 p = this.parents; 1316 } 1317 1318 return p; 1319 }, 1320 1321 /** 1322 * Shift the curve by the vector 'where'. 1323 * 1324 * @param {Array} where Array containing the x and y coordinate of the target location. 1325 * @returns {JXG.Curve} Reference to itself. 1326 */ 1327 moveTo: function (where) { 1328 // TODO add animation 1329 var delta = [], 1330 p; 1331 if (this.points.length > 0 && !this.evalVisProp('fixed')) { 1332 p = this.points[0]; 1333 if (where.length === 3) { 1334 delta = [ 1335 where[0] - p.usrCoords[0], 1336 where[1] - p.usrCoords[1], 1337 where[2] - p.usrCoords[2] 1338 ]; 1339 } else { 1340 delta = [where[0] - p.usrCoords[1], where[1] - p.usrCoords[2]]; 1341 } 1342 this.setPosition(Const.COORDS_BY_USER, delta); 1343 return this.board.update(this); 1344 } 1345 return this; 1346 }, 1347 1348 /** 1349 * If the curve is the result of a transformation applied 1350 * to a continuous curve, the glider projection has to be done 1351 * on the original curve. Otherwise there will be problems 1352 * when changing between high and low precision plotting, 1353 * since there number of points changes. 1354 * 1355 * @private 1356 * @returns {Array} [Boolean, curve]: Array contining 'true' if curve is result of a transformation, 1357 * and the source curve of the transformation. 1358 */ 1359 getTransformationSource: function () { 1360 var isTransformed, curve_org; 1361 if (Type.exists(this._transformationSource)) { 1362 curve_org = this._transformationSource; 1363 if ( 1364 curve_org.elementClass === Const.OBJECT_CLASS_CURVE //&& 1365 //curve_org.evalVisProp('curvetype') !== 'plot' 1366 ) { 1367 isTransformed = true; 1368 } 1369 } 1370 return [isTransformed, curve_org]; 1371 } 1372 1373 // See JXG.Math.Geometry.pnpoly 1374 // pnpoly: function (x_in, y_in, coord_type) { 1375 // var i, 1376 // j, 1377 // len, 1378 // x, 1379 // y, 1380 // crds, 1381 // v = this.points, 1382 // isIn = false; 1383 1384 // if (coord_type === Const.COORDS_BY_USER) { 1385 // crds = new Coords(Const.COORDS_BY_USER, [x_in, y_in], this.board); 1386 // x = crds.scrCoords[1]; 1387 // y = crds.scrCoords[2]; 1388 // } else { 1389 // x = x_in; 1390 // y = y_in; 1391 // } 1392 1393 // len = this.points.length; 1394 // for (i = 0, j = len - 2; i < len - 1; j = i++) { 1395 // if ( 1396 // v[i].scrCoords[2] > y !== v[j].scrCoords[2] > y && 1397 // x < 1398 // ((v[j].scrCoords[1] - v[i].scrCoords[1]) * (y - v[i].scrCoords[2])) / 1399 // (v[j].scrCoords[2] - v[i].scrCoords[2]) + 1400 // v[i].scrCoords[1] 1401 // ) { 1402 // isIn = !isIn; 1403 // } 1404 // } 1405 1406 // return isIn; 1407 // } 1408 } 1409 ); 1410 1411 /** 1412 * @class This element is used to provide a constructor for curve, which is just a wrapper for element {@link Curve}. 1413 * A curve is a mapping from R to R^2. t mapsto (x(t),y(t)). The graph is drawn for t in the interval [a,b]. 1414 * <p> 1415 * The following types of curves can be plotted: 1416 * <ul> 1417 * <li> parametric curves: t mapsto (x(t),y(t)), where x() and y() are univariate functions. 1418 * <li> polar curves: curves commonly written with polar equations like spirals and cardioids. 1419 * <li> data plots: plot line segments through a given list of coordinates. 1420 * </ul> 1421 * @pseudo 1422 * @name Curve 1423 * @augments JXG.Curve 1424 * @constructor 1425 * @type Object 1426 * @description JXG.Curve 1427 1428 * @param {function,number_function,number_function,number_function,number} x,y,a_,b_ Parent elements for Parametric Curves. 1429 * <p> 1430 * x describes the x-coordinate of the curve. It may be a function term in one variable, e.g. x(t). 1431 * In case of x being of type number, x(t) is set to a constant function. 1432 * this function at the values of the array. 1433 * </p> 1434 * <p> 1435 * y describes the y-coordinate of the curve. In case of a number, y(t) is set to the constant function 1436 * returning this number. 1437 * </p> 1438 * <p> 1439 * Further parameters are an optional number or function for the left interval border a, 1440 * and an optional number or function for the right interval border b. 1441 * </p> 1442 * <p> 1443 * Default values are a=-10 and b=10. 1444 * </p> 1445 * 1446 * @param {array_array,function,number} 1447 * 1448 * @description x,y Parent elements for Data Plots. 1449 * <p> 1450 * x and y are arrays contining the x and y coordinates of the data points which are connected by 1451 * line segments. The individual entries of x and y may also be functions. 1452 * In case of x being an array the curve type is data plot, regardless of the second parameter and 1453 * if additionally the second parameter y is a function term the data plot evaluates. 1454 * </p> 1455 * @param {function_array,function,number_function,number_function,number} 1456 * @description r,offset_,a_,b_ Parent elements for Polar Curves. 1457 * <p> 1458 * The first parameter is a function term r(phi) describing the polar curve. 1459 * </p> 1460 * <p> 1461 * The second parameter is the offset of the curve. It has to be 1462 * an array containing numbers or functions describing the offset. Default value is the origin [0,0]. 1463 * </p> 1464 * <p> 1465 * Further parameters are an optional number or function for the left interval border a, 1466 * and an optional number or function for the right interval border b. 1467 * </p> 1468 * <p> 1469 * Default values are a=-10 and b=10. 1470 * </p> 1471 * <p> 1472 * Additionally, a curve can be created by providing a curve and a transformation (or an array of transformations). 1473 * The result is a curve which is the transformation of the supplied curve. 1474 * 1475 * @see JXG.Curve 1476 * @example 1477 * // Parametric curve 1478 * // Create a curve of the form (t-sin(t), 1-cos(t), i.e. 1479 * // the cycloid curve. 1480 * var graph = board.create('curve', 1481 * [function(t){ return t-Math.sin(t);}, 1482 * function(t){ return 1-Math.cos(t);}, 1483 * 0, 2*Math.PI] 1484 * ); 1485 * </pre><div class="jxgbox" id="JXGaf9f818b-f3b6-4c4d-8c4c-e4a4078b726d" style="width: 300px; height: 300px;"></div> 1486 * <script type="text/javascript"> 1487 * var c1_board = JXG.JSXGraph.initBoard('JXGaf9f818b-f3b6-4c4d-8c4c-e4a4078b726d', {boundingbox: [-1, 5, 7, -1], axis: true, showcopyright: false, shownavigation: false}); 1488 * var graph1 = c1_board.create('curve', [function(t){ return t-Math.sin(t);},function(t){ return 1-Math.cos(t);},0, 2*Math.PI]); 1489 * </script><pre> 1490 * @example 1491 * // Data plots 1492 * // Connect a set of points given by coordinates with dashed line segments. 1493 * // The x- and y-coordinates of the points are given in two separate 1494 * // arrays. 1495 * var x = [0,1,2,3,4,5,6,7,8,9]; 1496 * var y = [9.2,1.3,7.2,-1.2,4.0,5.3,0.2,6.5,1.1,0.0]; 1497 * var graph = board.create('curve', [x,y], {dash:2}); 1498 * </pre><div class="jxgbox" id="JXG7dcbb00e-b6ff-481d-b4a8-887f5d8c6a83" style="width: 300px; height: 300px;"></div> 1499 * <script type="text/javascript"> 1500 * var c3_board = JXG.JSXGraph.initBoard('JXG7dcbb00e-b6ff-481d-b4a8-887f5d8c6a83', {boundingbox: [-1,10,10,-1], axis: true, showcopyright: false, shownavigation: false}); 1501 * var x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; 1502 * var y = [9.2, 1.3, 7.2, -1.2, 4.0, 5.3, 0.2, 6.5, 1.1, 0.0]; 1503 * var graph3 = c3_board.create('curve', [x,y], {dash:2}); 1504 * </script><pre> 1505 * @example 1506 * // Polar plot 1507 * // Create a curve with the equation r(phi)= a*(1+phi), i.e. 1508 * // a cardioid. 1509 * var a = board.create('slider',[[0,2],[2,2],[0,1,2]]); 1510 * var graph = board.create('curve', 1511 * [function(phi){ return a.Value()*(1-Math.cos(phi));}, 1512 * [1,0], 1513 * 0, 2*Math.PI], 1514 * {curveType: 'polar'} 1515 * ); 1516 * </pre><div class="jxgbox" id="JXGd0bc7a2a-8124-45ca-a6e7-142321a8f8c2" style="width: 300px; height: 300px;"></div> 1517 * <script type="text/javascript"> 1518 * var c2_board = JXG.JSXGraph.initBoard('JXGd0bc7a2a-8124-45ca-a6e7-142321a8f8c2', {boundingbox: [-3,3,3,-3], axis: true, showcopyright: false, shownavigation: false}); 1519 * var a = c2_board.create('slider',[[0,2],[2,2],[0,1,2]]); 1520 * var graph2 = c2_board.create('curve', [function(phi){ return a.Value()*(1-Math.cos(phi));}, [1,0], 0, 2*Math.PI], {curveType: 'polar'}); 1521 * </script><pre> 1522 * 1523 * @example 1524 * // Draggable Bezier curve 1525 * var col, p, c; 1526 * col = 'blue'; 1527 * p = []; 1528 * p.push(board.create('point',[-2, -1 ], {size: 5, strokeColor:col, fillColor:col})); 1529 * p.push(board.create('point',[1, 2.5 ], {size: 5, strokeColor:col, fillColor:col})); 1530 * p.push(board.create('point',[-1, -2.5 ], {size: 5, strokeColor:col, fillColor:col})); 1531 * p.push(board.create('point',[2, -2], {size: 5, strokeColor:col, fillColor:col})); 1532 * 1533 * c = board.create('curve', JXG.Math.Numerics.bezier(p), 1534 * {strokeColor:'red', name:"curve", strokeWidth:5, fixed: false}); // Draggable curve 1535 * c.addParents(p); 1536 * </pre><div class="jxgbox" id="JXG7bcc6280-f6eb-433e-8281-c837c3387849" style="width: 300px; height: 300px;"></div> 1537 * <script type="text/javascript"> 1538 * (function(){ 1539 * var board, col, p, c; 1540 * board = JXG.JSXGraph.initBoard('JXG7bcc6280-f6eb-433e-8281-c837c3387849', {boundingbox: [-3,3,3,-3], axis: true, showcopyright: false, shownavigation: false}); 1541 * col = 'blue'; 1542 * p = []; 1543 * p.push(board.create('point',[-2, -1 ], {size: 5, strokeColor:col, fillColor:col})); 1544 * p.push(board.create('point',[1, 2.5 ], {size: 5, strokeColor:col, fillColor:col})); 1545 * p.push(board.create('point',[-1, -2.5 ], {size: 5, strokeColor:col, fillColor:col})); 1546 * p.push(board.create('point',[2, -2], {size: 5, strokeColor:col, fillColor:col})); 1547 * 1548 * c = board.create('curve', JXG.Math.Numerics.bezier(p), 1549 * {strokeColor:'red', name:"curve", strokeWidth:5, fixed: false}); // Draggable curve 1550 * c.addParents(p); 1551 * })(); 1552 * </script><pre> 1553 * 1554 * @example 1555 * // The curve cu2 is the reflection of cu1 against line li 1556 * var li = board.create('line', [1,1,1], {strokeColor: '#aaaaaa'}); 1557 * var reflect = board.create('transform', [li], {type: 'reflect'}); 1558 * var cu1 = board.create('curve', [[-1, -1, -0.5, -1, -1, -0.5], [-3, -2, -2, -2, -2.5, -2.5]]); 1559 * var cu2 = board.create('curve', [cu1, reflect], {strokeColor: 'red'}); 1560 * 1561 * </pre><div id="JXG866dc7a2-d448-11e7-93b3-901b0e1b8723" class="jxgbox" style="width: 300px; height: 300px;"></div> 1562 * <script type="text/javascript"> 1563 * (function() { 1564 * var board = JXG.JSXGraph.initBoard('JXG866dc7a2-d448-11e7-93b3-901b0e1b8723', 1565 * {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false}); 1566 * var li = board.create('line', [1,1,1], {strokeColor: '#aaaaaa'}); 1567 * var reflect = board.create('transform', [li], {type: 'reflect'}); 1568 * var cu1 = board.create('curve', [[-1, -1, -0.5, -1, -1, -0.5], [-3, -2, -2, -2, -2.5, -2.5]]); 1569 * var cu2 = board.create('curve', [cu1, reflect], {strokeColor: 'red'}); 1570 * 1571 * })(); 1572 * 1573 * </script><pre> 1574 */ 1575 JXG.createCurve = function (board, parents, attributes) { 1576 var obj, 1577 cu, 1578 attr = Type.copyAttributes(attributes, board.options, "curve"); 1579 1580 obj = board.select(parents[0], true); 1581 if ( 1582 Type.isTransformationOrArray(parents[1]) && 1583 Type.isObject(obj) && 1584 (obj.type === Const.OBJECT_TYPE_CURVE || 1585 obj.type === Const.OBJECT_TYPE_ANGLE || 1586 obj.type === Const.OBJECT_TYPE_ARC || 1587 obj.type === Const.OBJECT_TYPE_CONIC || 1588 obj.type === Const.OBJECT_TYPE_SECTOR) 1589 ) { 1590 if (obj.type === Const.OBJECT_TYPE_SECTOR) { 1591 attr = Type.copyAttributes(attributes, board.options, "sector"); 1592 } else if (obj.type === Const.OBJECT_TYPE_ARC) { 1593 attr = Type.copyAttributes(attributes, board.options, "arc"); 1594 } else if (obj.type === Const.OBJECT_TYPE_ANGLE) { 1595 if (!Type.exists(attributes.withLabel)) { 1596 attributes.withLabel = false; 1597 } 1598 attr = Type.copyAttributes(attributes, board.options, "angle"); 1599 } else { 1600 attr = Type.copyAttributes(attributes, board.options, "curve"); 1601 } 1602 attr = Type.copyAttributes(attr, board.options, "curve"); 1603 1604 cu = new JXG.Curve(board, ["x", [], []], attr); 1605 /** 1606 * @class 1607 * @ignore 1608 */ 1609 cu.updateDataArray = function () { 1610 var i, 1611 le = obj.numberPoints; 1612 this.bezierDegree = obj.bezierDegree; 1613 this.dataX = []; 1614 this.dataY = []; 1615 for (i = 0; i < le; i++) { 1616 this.dataX.push(obj.points[i].usrCoords[1]); 1617 this.dataY.push(obj.points[i].usrCoords[2]); 1618 } 1619 return this; 1620 }; 1621 cu.addTransform(parents[1]); 1622 obj.addChild(cu); 1623 cu.setParents([obj]); 1624 cu._transformationSource = obj; 1625 1626 return cu; 1627 } 1628 attr = Type.copyAttributes(attributes, board.options, "curve"); 1629 return new JXG.Curve(board, ["x"].concat(parents), attr); 1630 }; 1631 1632 JXG.registerElement("curve", JXG.createCurve); 1633 1634 /** 1635 * @class This element is used to provide a constructor for functiongraph, 1636 * which is just a wrapper for element {@link Curve} with {@link JXG.Curve#X}() 1637 * set to x. The graph is drawn for x in the interval [a,b]. 1638 * @pseudo 1639 * @name Functiongraph 1640 * @augments JXG.Curve 1641 * @constructor 1642 * @type JXG.Curve 1643 * @param {function_number,function_number,function} f,a_,b_ Parent elements are a function term f(x) describing the function graph. 1644 * <p> 1645 * Further, an optional number or function for the left interval border a, 1646 * and an optional number or function for the right interval border b. 1647 * <p> 1648 * Default values are a=-10 and b=10. 1649 * @see JXG.Curve 1650 * @example 1651 * // Create a function graph for f(x) = 0.5*x*x-2*x 1652 * var graph = board.create('functiongraph', 1653 * [function(x){ return 0.5*x*x-2*x;}, -2, 4] 1654 * ); 1655 * </pre><div class="jxgbox" id="JXGefd432b5-23a3-4846-ac5b-b471e668b437" style="width: 300px; height: 300px;"></div> 1656 * <script type="text/javascript"> 1657 * var alex1_board = JXG.JSXGraph.initBoard('JXGefd432b5-23a3-4846-ac5b-b471e668b437', {boundingbox: [-3, 7, 5, -3], axis: true, showcopyright: false, shownavigation: false}); 1658 * var graph = alex1_board.create('functiongraph', [function(x){ return 0.5*x*x-2*x;}, -2, 4]); 1659 * </script><pre> 1660 * @example 1661 * // Create a function graph for f(x) = 0.5*x*x-2*x with variable interval 1662 * var s = board.create('slider',[[0,4],[3,4],[-2,4,5]]); 1663 * var graph = board.create('functiongraph', 1664 * [function(x){ return 0.5*x*x-2*x;}, 1665 * -2, 1666 * function(){return s.Value();}] 1667 * ); 1668 * </pre><div class="jxgbox" id="JXG4a203a84-bde5-4371-ad56-44619690bb50" style="width: 300px; height: 300px;"></div> 1669 * <script type="text/javascript"> 1670 * var alex2_board = JXG.JSXGraph.initBoard('JXG4a203a84-bde5-4371-ad56-44619690bb50', {boundingbox: [-3, 7, 5, -3], axis: true, showcopyright: false, shownavigation: false}); 1671 * var s = alex2_board.create('slider',[[0,4],[3,4],[-2,4,5]]); 1672 * var graph = alex2_board.create('functiongraph', [function(x){ return 0.5*x*x-2*x;}, -2, function(){return s.Value();}]); 1673 * </script><pre> 1674 */ 1675 JXG.createFunctiongraph = function (board, parents, attributes) { 1676 var attr, 1677 par = ["x", "x"].concat(parents); // variable name and identity function for x-coordinate 1678 // par = ["x", function(x) { return x; }].concat(parents); 1679 1680 attr = Type.copyAttributes(attributes, board.options, "functiongraph"); 1681 attr = Type.copyAttributes(attr, board.options, "curve"); 1682 attr.curvetype = "functiongraph"; 1683 return new JXG.Curve(board, par, attr); 1684 }; 1685 1686 JXG.registerElement("functiongraph", JXG.createFunctiongraph); 1687 JXG.registerElement("plot", JXG.createFunctiongraph); 1688 1689 /** 1690 * @class This element is used to provide a constructor for (natural) cubic spline curves. 1691 * Create a dynamic spline interpolated curve given by sample points p_1 to p_n. 1692 * @pseudo 1693 * @name Spline 1694 * @augments JXG.Curve 1695 * @constructor 1696 * @type JXG.Curve 1697 * @param {JXG.Board} board Reference to the board the spline is drawn on. 1698 * @param {Array} parents Array of points the spline interpolates. This can be 1699 * <ul> 1700 * <li> an array of JSXGraph points</li> 1701 * <li> an array of coordinate pairs</li> 1702 * <li> an array of functions returning coordinate pairs</li> 1703 * <li> an array consisting of an array with x-coordinates and an array of y-coordinates</li> 1704 * </ul> 1705 * All individual entries of coordinates arrays may be numbers or functions returning numbers. 1706 * @param {Object} attributes Define color, width, ... of the spline 1707 * @returns {JXG.Curve} Returns reference to an object of type JXG.Curve. 1708 * @see JXG.Curve 1709 * @example 1710 * 1711 * var p = []; 1712 * p[0] = board.create('point', [-2,2], {size: 4, face: 'o'}); 1713 * p[1] = board.create('point', [0,-1], {size: 4, face: 'o'}); 1714 * p[2] = board.create('point', [2,0], {size: 4, face: 'o'}); 1715 * p[3] = board.create('point', [4,1], {size: 4, face: 'o'}); 1716 * 1717 * var c = board.create('spline', p, {strokeWidth:3}); 1718 * </pre><div id="JXG6c197afc-e482-11e5-b1bf-901b0e1b8723" style="width: 300px; height: 300px;"></div> 1719 * <script type="text/javascript"> 1720 * (function() { 1721 * var board = JXG.JSXGraph.initBoard('JXG6c197afc-e482-11e5-b1bf-901b0e1b8723', 1722 * {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false}); 1723 * 1724 * var p = []; 1725 * p[0] = board.create('point', [-2,2], {size: 4, face: 'o'}); 1726 * p[1] = board.create('point', [0,-1], {size: 4, face: 'o'}); 1727 * p[2] = board.create('point', [2,0], {size: 4, face: 'o'}); 1728 * p[3] = board.create('point', [4,1], {size: 4, face: 'o'}); 1729 * 1730 * var c = board.create('spline', p, {strokeWidth:3}); 1731 * })(); 1732 * 1733 * </script><pre> 1734 * 1735 */ 1736 JXG.createSpline = function (board, parents, attributes) { 1737 var el, funcs, ret; 1738 1739 funcs = function () { 1740 var D, 1741 x = [], 1742 y = []; 1743 1744 return [ 1745 function (t, suspended) { 1746 // Function term 1747 var i, j, c; 1748 1749 if (!suspended) { 1750 x = []; 1751 y = []; 1752 1753 // given as [x[], y[]] 1754 if ( 1755 parents.length === 2 && 1756 Type.isArray(parents[0]) && 1757 Type.isArray(parents[1]) && 1758 parents[0].length === parents[1].length 1759 ) { 1760 for (i = 0; i < parents[0].length; i++) { 1761 if (Type.isFunction(parents[0][i])) { 1762 x.push(parents[0][i]()); 1763 } else { 1764 x.push(parents[0][i]); 1765 } 1766 1767 if (Type.isFunction(parents[1][i])) { 1768 y.push(parents[1][i]()); 1769 } else { 1770 y.push(parents[1][i]); 1771 } 1772 } 1773 } else { 1774 for (i = 0; i < parents.length; i++) { 1775 if (Type.isPoint(parents[i])) { 1776 x.push(parents[i].X()); 1777 y.push(parents[i].Y()); 1778 // given as [[x1,y1], [x2, y2], ...] 1779 } else if (Type.isArray(parents[i]) && parents[i].length === 2) { 1780 for (j = 0; j < parents.length; j++) { 1781 if (Type.isFunction(parents[j][0])) { 1782 x.push(parents[j][0]()); 1783 } else { 1784 x.push(parents[j][0]); 1785 } 1786 1787 if (Type.isFunction(parents[j][1])) { 1788 y.push(parents[j][1]()); 1789 } else { 1790 y.push(parents[j][1]); 1791 } 1792 } 1793 } else if ( 1794 Type.isFunction(parents[i]) && 1795 parents[i]().length === 2 1796 ) { 1797 c = parents[i](); 1798 x.push(c[0]); 1799 y.push(c[1]); 1800 } 1801 } 1802 } 1803 1804 // The array D has only to be calculated when the position of one or more sample points 1805 // changes. Otherwise D is always the same for all points on the spline. 1806 D = Numerics.splineDef(x, y); 1807 } 1808 1809 return Numerics.splineEval(t, x, y, D); 1810 }, 1811 // minX() 1812 function () { 1813 return x[0]; 1814 }, 1815 //maxX() 1816 function () { 1817 return x[x.length - 1]; 1818 } 1819 ]; 1820 }; 1821 1822 attributes = Type.copyAttributes(attributes, board.options, "curve"); 1823 attributes.curvetype = "functiongraph"; 1824 ret = funcs(); 1825 el = new JXG.Curve(board, ["x", "x", ret[0], ret[1], ret[2]], attributes); 1826 el.setParents(parents); 1827 el.elType = "spline"; 1828 1829 return el; 1830 }; 1831 1832 /** 1833 * Register the element type spline at JSXGraph 1834 * @private 1835 */ 1836 JXG.registerElement("spline", JXG.createSpline); 1837 1838 /** 1839 * @class This element is used to provide a constructor for cardinal spline curves. 1840 * Create a dynamic cardinal spline interpolated curve given by sample points p_1 to p_n. 1841 * @pseudo 1842 * @name Cardinalspline 1843 * @augments JXG.Curve 1844 * @constructor 1845 * @type JXG.Curve 1846 * @param {JXG.Board} board Reference to the board the cardinal spline is drawn on. 1847 * @param {Array} parents Array with three entries. 1848 * <p> 1849 * First entry: Array of points the spline interpolates. This can be 1850 * <ul> 1851 * <li> an array of JSXGraph points</li> 1852 * <li> an array of coordinate pairs</li> 1853 * <li> an array of functions returning coordinate pairs</li> 1854 * <li> an array consisting of an array with x-coordinates and an array of y-coordinates</li> 1855 * </ul> 1856 * All individual entries of coordinates arrays may be numbers or functions returning numbers. 1857 * <p> 1858 * Second entry: tau number or function 1859 * <p> 1860 * Third entry: type string containing 'uniform' (default) or 'centripetal'. 1861 * @param {Object} attributes Define color, width, ... of the cardinal spline 1862 * @returns {JXG.Curve} Returns reference to an object of type JXG.Curve. 1863 * @see JXG.Curve 1864 * @example 1865 * //create a cardinal spline out of an array of JXG points with adjustable tension 1866 * //create array of points 1867 * var p1 = board.create('point',[0,0]) 1868 * var p2 = board.create('point',[1,4]) 1869 * var p3 = board.create('point',[4,5]) 1870 * var p4 = board.create('point',[2,3]) 1871 * var p5 = board.create('point',[3,0]) 1872 * var p = [p1,p2,p3,p4,p5] 1873 * 1874 * // tension 1875 * tau = board.create('slider', [[4,3],[9,3],[0.001,0.5,1]], {name:'tau'}); 1876 * c = board.create('curve', JXG.Math.Numerics.CardinalSpline(p, function(){ return tau.Value();}), {strokeWidth:3}); 1877 * </pre><div id="JXG6c197afc-e482-11e5-b2af-901b0e1b8723" style="width: 300px; height: 300px;"></div> 1878 * <script type="text/javascript"> 1879 * (function() { 1880 * var board = JXG.JSXGraph.initBoard('JXG6c197afc-e482-11e5-b2af-901b0e1b8723', 1881 * {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false}); 1882 * 1883 * var p = []; 1884 * p[0] = board.create('point', [-2,2], {size: 4, face: 'o'}); 1885 * p[1] = board.create('point', [0,-1], {size: 4, face: 'o'}); 1886 * p[2] = board.create('point', [2,0], {size: 4, face: 'o'}); 1887 * p[3] = board.create('point', [4,1], {size: 4, face: 'o'}); 1888 * 1889 * var c = board.create('spline', p, {strokeWidth:3}); 1890 * })(); 1891 * 1892 * </script><pre> 1893 */ 1894 JXG.createCardinalSpline = function (board, parents, attributes) { 1895 var el, 1896 getPointLike, 1897 points, 1898 tau, 1899 type, 1900 p, 1901 q, 1902 i, 1903 le, 1904 splineArr, 1905 errStr = "\nPossible parent types: [points:array, tau:number|function, type:string]"; 1906 1907 if (!Type.exists(parents[0]) || !Type.isArray(parents[0])) { 1908 throw new Error( 1909 "JSXGraph: JXG.createCardinalSpline: argument 1 'points' has to be array of points or coordinate pairs" + 1910 errStr 1911 ); 1912 } 1913 if ( 1914 !Type.exists(parents[1]) || 1915 (!Type.isNumber(parents[1]) && !Type.isFunction(parents[1])) 1916 ) { 1917 throw new Error( 1918 "JSXGraph: JXG.createCardinalSpline: argument 2 'tau' has to be number between [0,1] or function'" + 1919 errStr 1920 ); 1921 } 1922 if (!Type.exists(parents[2]) || !Type.isString(parents[2])) { 1923 throw new Error( 1924 "JSXGraph: JXG.createCardinalSpline: argument 3 'type' has to be string 'uniform' or 'centripetal'" + 1925 errStr 1926 ); 1927 } 1928 1929 attributes = Type.copyAttributes(attributes, board.options, "curve"); 1930 attributes = Type.copyAttributes(attributes, board.options, "cardinalspline"); 1931 attributes.curvetype = "parameter"; 1932 1933 p = parents[0]; 1934 q = []; 1935 1936 // Given as [x[], y[]] 1937 if ( 1938 !attributes.isarrayofcoordinates && 1939 p.length === 2 && 1940 Type.isArray(p[0]) && 1941 Type.isArray(p[1]) && 1942 p[0].length === p[1].length 1943 ) { 1944 for (i = 0; i < p[0].length; i++) { 1945 q[i] = []; 1946 if (Type.isFunction(p[0][i])) { 1947 q[i].push(p[0][i]()); 1948 } else { 1949 q[i].push(p[0][i]); 1950 } 1951 1952 if (Type.isFunction(p[1][i])) { 1953 q[i].push(p[1][i]()); 1954 } else { 1955 q[i].push(p[1][i]); 1956 } 1957 } 1958 } else { 1959 // given as [[x0, y0], [x1, y1], point, ...] 1960 for (i = 0; i < p.length; i++) { 1961 if (Type.isString(p[i])) { 1962 q.push(board.select(p[i])); 1963 } else if (Type.isPoint(p[i])) { 1964 q.push(p[i]); 1965 // given as [[x0,y0], [x1, y2], ...] 1966 } else if (Type.isArray(p[i]) && p[i].length === 2) { 1967 q[i] = []; 1968 if (Type.isFunction(p[i][0])) { 1969 q[i].push(p[i][0]()); 1970 } else { 1971 q[i].push(p[i][0]); 1972 } 1973 1974 if (Type.isFunction(p[i][1])) { 1975 q[i].push(p[i][1]()); 1976 } else { 1977 q[i].push(p[i][1]); 1978 } 1979 } else if (Type.isFunction(p[i]) && p[i]().length === 2) { 1980 q.push(parents[i]()); 1981 } 1982 } 1983 } 1984 1985 if (attributes.createpoints === true) { 1986 points = Type.providePoints(board, q, attributes, "cardinalspline", ["points"]); 1987 } else { 1988 points = []; 1989 1990 /** 1991 * @ignore 1992 */ 1993 getPointLike = function (ii) { 1994 return { 1995 X: function () { 1996 return q[ii][0]; 1997 }, 1998 Y: function () { 1999 return q[ii][1]; 2000 }, 2001 Dist: function (p) { 2002 var dx = this.X() - p.X(), 2003 dy = this.Y() - p.Y(); 2004 2005 return Mat.hypot(dx, dy); 2006 } 2007 }; 2008 }; 2009 2010 for (i = 0; i < q.length; i++) { 2011 if (Type.isPoint(q[i])) { 2012 points.push(q[i]); 2013 } else { 2014 points.push(getPointLike(i)); 2015 } 2016 } 2017 } 2018 2019 tau = parents[1]; 2020 type = parents[2]; 2021 2022 splineArr = ["x"].concat(Numerics.CardinalSpline(points, tau, type)); 2023 2024 el = new JXG.Curve(board, splineArr, attributes); 2025 le = points.length; 2026 el.setParents(points); 2027 for (i = 0; i < le; i++) { 2028 p = points[i]; 2029 if (Type.isPoint(p)) { 2030 if (Type.exists(p._is_new)) { 2031 el.addChild(p); 2032 delete p._is_new; 2033 } else { 2034 p.addChild(el); 2035 } 2036 } 2037 } 2038 el.elType = "cardinalspline"; 2039 2040 return el; 2041 }; 2042 2043 /** 2044 * Register the element type cardinalspline at JSXGraph 2045 * @private 2046 */ 2047 JXG.registerElement("cardinalspline", JXG.createCardinalSpline); 2048 2049 /** 2050 * @class This element is used to provide a constructor for metapost spline curves. 2051 * Create a dynamic metapost spline interpolated curve given by sample points p_1 to p_n. 2052 * @pseudo 2053 * @name Metapostspline 2054 * @augments JXG.Curve 2055 * @constructor 2056 * @type JXG.Curve 2057 * @param {JXG.Board} board Reference to the board the metapost spline is drawn on. 2058 * @param {Array} parents Array with two entries. 2059 * <p> 2060 * First entry: Array of points the spline interpolates. This can be 2061 * <ul> 2062 * <li> an array of JSXGraph points</li> 2063 * <li> an object of coordinate pairs</li> 2064 * <li> an array of functions returning coordinate pairs</li> 2065 * <li> an array consisting of an array with x-coordinates and an array of y-coordinates</li> 2066 * </ul> 2067 * All individual entries of coordinates arrays may be numbers or functions returning numbers. 2068 * <p> 2069 * Second entry: JavaScript object containing the control values like tension, direction, curl. 2070 * @param {Object} attributes Define color, width, ... of the metapost spline 2071 * @returns {JXG.Curve} Returns reference to an object of type JXG.Curve. 2072 * @see JXG.Curve 2073 * @example 2074 * var po = [], 2075 * attr = { 2076 * size: 5, 2077 * color: 'red' 2078 * }, 2079 * controls; 2080 * 2081 * var tension = board.create('slider', [[-3, 6], [3, 6], [0, 1, 20]], {name: 'tension'}); 2082 * var curl = board.create('slider', [[-3, 5], [3, 5], [0, 1, 30]], {name: 'curl A, D'}); 2083 * var dir = board.create('slider', [[-3, 4], [3, 4], [-180, 0, 180]], {name: 'direction B'}); 2084 * 2085 * po.push(board.create('point', [-3, -3])); 2086 * po.push(board.create('point', [0, -3])); 2087 * po.push(board.create('point', [4, -5])); 2088 * po.push(board.create('point', [6, -2])); 2089 * 2090 * var controls = { 2091 * tension: function() {return tension.Value(); }, 2092 * direction: { 1: function() {return dir.Value(); } }, 2093 * curl: { 0: function() {return curl.Value(); }, 2094 * 3: function() {return curl.Value(); } 2095 * }, 2096 * isClosed: false 2097 * }; 2098 * 2099 * // Plot a metapost curve 2100 * var cu = board.create('metapostspline', [po, controls], {strokeColor: 'blue', strokeWidth: 2}); 2101 * 2102 * 2103 * </pre><div id="JXGb8c6ffed-7419-41a3-9e55-3754b2327ae9" class="jxgbox" style="width: 300px; height: 300px;"></div> 2104 * <script type="text/javascript"> 2105 * (function() { 2106 * var board = JXG.JSXGraph.initBoard('JXGb8c6ffed-7419-41a3-9e55-3754b2327ae9', 2107 * {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false}); 2108 * var po = [], 2109 * attr = { 2110 * size: 5, 2111 * color: 'red' 2112 * }, 2113 * controls; 2114 * 2115 * var tension = board.create('slider', [[-3, 6], [3, 6], [0, 1, 20]], {name: 'tension'}); 2116 * var curl = board.create('slider', [[-3, 5], [3, 5], [0, 1, 30]], {name: 'curl A, D'}); 2117 * var dir = board.create('slider', [[-3, 4], [3, 4], [-180, 0, 180]], {name: 'direction B'}); 2118 * 2119 * po.push(board.create('point', [-3, -3])); 2120 * po.push(board.create('point', [0, -3])); 2121 * po.push(board.create('point', [4, -5])); 2122 * po.push(board.create('point', [6, -2])); 2123 * 2124 * var controls = { 2125 * tension: function() {return tension.Value(); }, 2126 * direction: { 1: function() {return dir.Value(); } }, 2127 * curl: { 0: function() {return curl.Value(); }, 2128 * 3: function() {return curl.Value(); } 2129 * }, 2130 * isClosed: false 2131 * }; 2132 * 2133 * // Plot a metapost curve 2134 * var cu = board.create('metapostspline', [po, controls], {strokeColor: 'blue', strokeWidth: 2}); 2135 * 2136 * 2137 * })(); 2138 * 2139 * </script><pre> 2140 * 2141 */ 2142 JXG.createMetapostSpline = function (board, parents, attributes) { 2143 var el, 2144 getPointLike, 2145 points, 2146 controls, 2147 p, 2148 q, 2149 i, 2150 le, 2151 errStr = "\nPossible parent types: [points:array, controls:object"; 2152 2153 if (!Type.exists(parents[0]) || !Type.isArray(parents[0])) { 2154 throw new Error( 2155 "JSXGraph: JXG.createMetapostSpline: argument 1 'points' has to be array of points or coordinate pairs" + 2156 errStr 2157 ); 2158 } 2159 if (!Type.exists(parents[1]) || !Type.isObject(parents[1])) { 2160 throw new Error( 2161 "JSXGraph: JXG.createMetapostSpline: argument 2 'controls' has to be a JavaScript object'" + 2162 errStr 2163 ); 2164 } 2165 2166 attributes = Type.copyAttributes(attributes, board.options, "curve"); 2167 attributes = Type.copyAttributes(attributes, board.options, "metapostspline"); 2168 attributes.curvetype = "parameter"; 2169 2170 p = parents[0]; 2171 q = []; 2172 2173 // given as [x[], y[]] 2174 if ( 2175 !attributes.isarrayofcoordinates && 2176 p.length === 2 && 2177 Type.isArray(p[0]) && 2178 Type.isArray(p[1]) && 2179 p[0].length === p[1].length 2180 ) { 2181 for (i = 0; i < p[0].length; i++) { 2182 q[i] = []; 2183 if (Type.isFunction(p[0][i])) { 2184 q[i].push(p[0][i]()); 2185 } else { 2186 q[i].push(p[0][i]); 2187 } 2188 2189 if (Type.isFunction(p[1][i])) { 2190 q[i].push(p[1][i]()); 2191 } else { 2192 q[i].push(p[1][i]); 2193 } 2194 } 2195 } else { 2196 // given as [[x0, y0], [x1, y1], point, ...] 2197 for (i = 0; i < p.length; i++) { 2198 if (Type.isString(p[i])) { 2199 q.push(board.select(p[i])); 2200 } else if (Type.isPoint(p[i])) { 2201 q.push(p[i]); 2202 // given as [[x0,y0], [x1, y2], ...] 2203 } else if (Type.isArray(p[i]) && p[i].length === 2) { 2204 q[i] = []; 2205 if (Type.isFunction(p[i][0])) { 2206 q[i].push(p[i][0]()); 2207 } else { 2208 q[i].push(p[i][0]); 2209 } 2210 2211 if (Type.isFunction(p[i][1])) { 2212 q[i].push(p[i][1]()); 2213 } else { 2214 q[i].push(p[i][1]); 2215 } 2216 } else if (Type.isFunction(p[i]) && p[i]().length === 2) { 2217 q.push(parents[i]()); 2218 } 2219 } 2220 } 2221 2222 if (attributes.createpoints === true) { 2223 points = Type.providePoints(board, q, attributes, 'metapostspline', ['points']); 2224 } else { 2225 points = []; 2226 2227 /** 2228 * @ignore 2229 */ 2230 getPointLike = function (ii) { 2231 return { 2232 X: function () { 2233 return q[ii][0]; 2234 }, 2235 Y: function () { 2236 return q[ii][1]; 2237 } 2238 }; 2239 }; 2240 2241 for (i = 0; i < q.length; i++) { 2242 if (Type.isPoint(q[i])) { 2243 points.push(q[i]); 2244 } else { 2245 points.push(getPointLike); 2246 } 2247 } 2248 } 2249 2250 controls = parents[1]; 2251 2252 el = new JXG.Curve(board, ["t", [], [], 0, p.length - 1], attributes); 2253 /** 2254 * @class 2255 * @ignore 2256 */ 2257 el.updateDataArray = function () { 2258 var res, 2259 i, 2260 len = points.length, 2261 p = []; 2262 2263 for (i = 0; i < len; i++) { 2264 p.push([points[i].X(), points[i].Y()]); 2265 } 2266 2267 res = Metapost.curve(p, controls); 2268 this.dataX = res[0]; 2269 this.dataY = res[1]; 2270 }; 2271 el.bezierDegree = 3; 2272 2273 le = points.length; 2274 el.setParents(points); 2275 for (i = 0; i < le; i++) { 2276 if (Type.isPoint(points[i])) { 2277 points[i].addChild(el); 2278 } 2279 } 2280 el.elType = "metapostspline"; 2281 2282 return el; 2283 }; 2284 2285 JXG.registerElement("metapostspline", JXG.createMetapostSpline); 2286 2287 /** 2288 * @class This element is used to provide a constructor for Riemann sums, which is realized as a special curve. 2289 * The returned element has the method Value() which returns the sum of the areas of the bars. 2290 * <p> 2291 * In case of type "simpson" and "trapezoidal", the horizontal line approximating the function value 2292 * is replaced by a parabola or a secant. IN case of "simpson", 2293 * the parabola is approximated visually by a polygonal chain of fixed step width. 2294 * 2295 * @pseudo 2296 * @name Riemannsum 2297 * @augments JXG.Curve 2298 * @constructor 2299 * @type Curve 2300 * @param {function,array_number,function_string,function_function,number_function,number} f,n,type_,a_,b_ Parent elements of Riemannsum are a 2301 * Either a function term f(x) describing the function graph which is filled by the Riemann bars, or 2302 * an array consisting of two functions and the area between is filled by the Riemann bars. 2303 * <p> 2304 * n determines the number of bars, it is either a fixed number or a function. 2305 * <p> 2306 * type is a string or function returning one of the values: 'left', 'right', 'middle', 'lower', 'upper', 'random', 'simpson', or 'trapezoidal'. 2307 * Default value is 'left'. "simpson" is Simpson's 1/3 rule. 2308 * <p> 2309 * Further parameters are an optional number or function for the left interval border a, 2310 * and an optional number or function for the right interval border b. 2311 * <p> 2312 * Default values are a=-10 and b=10. 2313 * @see JXG.Curve 2314 * @example 2315 * // Create Riemann sums for f(x) = 0.5*x*x-2*x. 2316 * var s = board.create('slider',[[0,4],[3,4],[0,4,10]],{snapWidth:1}); 2317 * var f = function(x) { return 0.5*x*x-2*x; }; 2318 * var r = board.create('riemannsum', 2319 * [f, function(){return s.Value();}, 'upper', -2, 5], 2320 * {fillOpacity:0.4} 2321 * ); 2322 * var g = board.create('functiongraph',[f, -2, 5]); 2323 * var t = board.create('text',[-2,-2, function(){ return 'Sum=' + JXG.toFixed(r.Value(), 4); }]); 2324 * </pre><div class="jxgbox" id="JXG940f40cc-2015-420d-9191-c5d83de988cf" style="width: 300px; height: 300px;"></div> 2325 * <script type="text/javascript"> 2326 * (function(){ 2327 * var board = JXG.JSXGraph.initBoard('JXG940f40cc-2015-420d-9191-c5d83de988cf', {boundingbox: [-3, 7, 5, -3], axis: true, showcopyright: false, shownavigation: false}); 2328 * var f = function(x) { return 0.5*x*x-2*x; }; 2329 * var s = board.create('slider',[[0,4],[3,4],[0,4,10]],{snapWidth:1}); 2330 * var r = board.create('riemannsum', [f, function(){return s.Value();}, 'upper', -2, 5], {fillOpacity:0.4}); 2331 * var g = board.create('functiongraph', [f, -2, 5]); 2332 * var t = board.create('text',[-2,-2, function(){ return 'Sum=' + JXG.toFixed(r.Value(), 4); }]); 2333 * })(); 2334 * </script><pre> 2335 * 2336 * @example 2337 * // Riemann sum between two functions 2338 * var s = board.create('slider',[[0,4],[3,4],[0,4,10]],{snapWidth:1}); 2339 * var g = function(x) { return 0.5*x*x-2*x; }; 2340 * var f = function(x) { return -x*(x-4); }; 2341 * var r = board.create('riemannsum', 2342 * [[g,f], function(){return s.Value();}, 'lower', 0, 4], 2343 * {fillOpacity:0.4} 2344 * ); 2345 * var f = board.create('functiongraph',[f, -2, 5]); 2346 * var g = board.create('functiongraph',[g, -2, 5]); 2347 * var t = board.create('text',[-2,-2, function(){ return 'Sum=' + JXG.toFixed(r.Value(), 4); }]); 2348 * </pre><div class="jxgbox" id="JXGf9a7ba38-b50f-4a32-a873-2f3bf9caee79" style="width: 300px; height: 300px;"></div> 2349 * <script type="text/javascript"> 2350 * (function(){ 2351 * var board = JXG.JSXGraph.initBoard('JXGf9a7ba38-b50f-4a32-a873-2f3bf9caee79', {boundingbox: [-3, 7, 5, -3], axis: true, showcopyright: false, shownavigation: false}); 2352 * var s = board.create('slider',[[0,4],[3,4],[0,4,10]],{snapWidth:1}); 2353 * var g = function(x) { return 0.5*x*x-2*x; }; 2354 * var f = function(x) { return -x*(x-4); }; 2355 * var r = board.create('riemannsum', 2356 * [[g,f], function(){return s.Value();}, 'lower', 0, 4], 2357 * {fillOpacity:0.4} 2358 * ); 2359 * var f = board.create('functiongraph',[f, -2, 5]); 2360 * var g = board.create('functiongraph',[g, -2, 5]); 2361 * var t = board.create('text',[-2,-2, function(){ return 'Sum=' + JXG.toFixed(r.Value(), 4); }]); 2362 * })(); 2363 * </script><pre> 2364 */ 2365 JXG.createRiemannsum = function (board, parents, attributes) { 2366 var n, type, f, par, c, attr; 2367 2368 attr = Type.copyAttributes(attributes, board.options, "riemannsum"); 2369 attr.curvetype = "plot"; 2370 2371 f = parents[0]; 2372 n = Type.createFunction(parents[1], board, ""); 2373 2374 if (!Type.exists(n)) { 2375 throw new Error( 2376 "JSXGraph: JXG.createRiemannsum: argument '2' n has to be number or function." + 2377 "\nPossible parent types: [function,n:number|function,type,start:number|function,end:number|function]" 2378 ); 2379 } 2380 2381 if (typeof parents[2] === 'string') { 2382 parents[2] = '\'' + parents[2] + '\''; 2383 } 2384 2385 type = Type.createFunction(parents[2], board, ""); 2386 if (!Type.exists(type)) { 2387 throw new Error( 2388 "JSXGraph: JXG.createRiemannsum: argument 3 'type' has to be string or function." + 2389 "\nPossible parent types: [function,n:number|function,type,start:number|function,end:number|function]" 2390 ); 2391 } 2392 2393 par = [[0], [0]].concat(parents.slice(3)); 2394 2395 c = board.create("curve", par, attr); 2396 2397 c.sum = 0.0; 2398 /** 2399 * Returns the value of the Riemann sum, i.e. the sum of the (signed) areas of the rectangles. 2400 * @name Value 2401 * @memberOf Riemannsum.prototype 2402 * @function 2403 * @returns {Number} value of Riemann sum. 2404 */ 2405 c.Value = function () { 2406 return this.sum; 2407 }; 2408 2409 /** 2410 * @class 2411 * @ignore 2412 */ 2413 c.updateDataArray = function () { 2414 var u = Numerics.riemann(f, n(), type(), this.minX(), this.maxX()); 2415 this.dataX = u[0]; 2416 this.dataY = u[1]; 2417 2418 // Update "Riemann sum" 2419 this.sum = u[2]; 2420 }; 2421 2422 c.addParentsFromJCFunctions([n, type]); 2423 2424 return c; 2425 }; 2426 2427 JXG.registerElement("riemannsum", JXG.createRiemannsum); 2428 2429 /** 2430 * @class This element is used to provide a constructor for trace curve (simple locus curve), which is realized as a special curve. 2431 * @pseudo 2432 * @name Tracecurve 2433 * @augments JXG.Curve 2434 * @constructor 2435 * @type Object 2436 * @descript JXG.Curve 2437 * @param {Point} Parent elements of Tracecurve are a 2438 * glider point and a point whose locus is traced. 2439 * @param {point} 2440 * @see JXG.Curve 2441 * @example 2442 * // Create trace curve. 2443 * var c1 = board.create('circle',[[0, 0], [2, 0]]), 2444 * p1 = board.create('point',[-3, 1]), 2445 * g1 = board.create('glider',[2, 1, c1]), 2446 * s1 = board.create('segment',[g1, p1]), 2447 * p2 = board.create('midpoint',[s1]), 2448 * curve = board.create('tracecurve', [g1, p2]); 2449 * 2450 * </pre><div class="jxgbox" id="JXG5749fb7d-04fc-44d2-973e-45c1951e29ad" style="width: 300px; height: 300px;"></div> 2451 * <script type="text/javascript"> 2452 * var tc1_board = JXG.JSXGraph.initBoard('JXG5749fb7d-04fc-44d2-973e-45c1951e29ad', {boundingbox: [-4, 4, 4, -4], axis: false, showcopyright: false, shownavigation: false}); 2453 * var c1 = tc1_board.create('circle',[[0, 0], [2, 0]]), 2454 * p1 = tc1_board.create('point',[-3, 1]), 2455 * g1 = tc1_board.create('glider',[2, 1, c1]), 2456 * s1 = tc1_board.create('segment',[g1, p1]), 2457 * p2 = tc1_board.create('midpoint',[s1]), 2458 * curve = tc1_board.create('tracecurve', [g1, p2]); 2459 * </script><pre> 2460 */ 2461 JXG.createTracecurve = function (board, parents, attributes) { 2462 var c, glider, tracepoint, attr; 2463 2464 if (parents.length !== 2) { 2465 throw new Error( 2466 "JSXGraph: Can't create trace curve with given parent'" + 2467 "\nPossible parent types: [glider, point]" 2468 ); 2469 } 2470 2471 glider = board.select(parents[0]); 2472 tracepoint = board.select(parents[1]); 2473 2474 if (glider.type !== Const.OBJECT_TYPE_GLIDER || !Type.isPoint(tracepoint)) { 2475 throw new Error( 2476 "JSXGraph: Can't create trace curve with parent types '" + 2477 typeof parents[0] + 2478 "' and '" + 2479 typeof parents[1] + 2480 "'." + 2481 "\nPossible parent types: [glider, point]" 2482 ); 2483 } 2484 2485 attr = Type.copyAttributes(attributes, board.options, "tracecurve"); 2486 attr.curvetype = "plot"; 2487 c = board.create("curve", [[0], [0]], attr); 2488 2489 /** 2490 * @class 2491 * @ignore 2492 */ 2493 c.updateDataArray = function () { 2494 var i, step, t, el, pEl, x, y, from, 2495 savetrace, 2496 le = this.visProp.numberpoints, 2497 savePos = glider.position, 2498 slideObj = glider.slideObject, 2499 mi = slideObj.minX(), 2500 ma = slideObj.maxX(); 2501 2502 // set step width 2503 step = (ma - mi) / le; 2504 this.dataX = []; 2505 this.dataY = []; 2506 2507 /* 2508 * For gliders on circles and lines a closed curve is computed. 2509 * For gliders on curves the curve is not closed. 2510 */ 2511 if (slideObj.elementClass !== Const.OBJECT_CLASS_CURVE) { 2512 le++; 2513 } 2514 2515 // Loop over all steps 2516 for (i = 0; i < le; i++) { 2517 t = mi + i * step; 2518 x = slideObj.X(t) / slideObj.Z(t); 2519 y = slideObj.Y(t) / slideObj.Z(t); 2520 2521 // Position the glider 2522 glider.setPositionDirectly(Const.COORDS_BY_USER, [x, y]); 2523 from = false; 2524 2525 // Update all elements from the glider up to the trace element 2526 for (el in this.board.objects) { 2527 if (this.board.objects.hasOwnProperty(el)) { 2528 pEl = this.board.objects[el]; 2529 2530 if (pEl === glider) { 2531 from = true; 2532 } 2533 2534 if (from && pEl.needsRegularUpdate) { 2535 // Save the trace mode of the element 2536 savetrace = pEl.visProp.trace; 2537 pEl.visProp.trace = false; 2538 pEl.needsUpdate = true; 2539 pEl.update(true); 2540 2541 // Restore the trace mode 2542 pEl.visProp.trace = savetrace; 2543 if (pEl === tracepoint) { 2544 break; 2545 } 2546 } 2547 } 2548 } 2549 2550 // Store the position of the trace point 2551 this.dataX[i] = tracepoint.X(); 2552 this.dataY[i] = tracepoint.Y(); 2553 } 2554 2555 // Restore the original position of the glider 2556 glider.position = savePos; 2557 from = false; 2558 2559 // Update all elements from the glider to the trace point 2560 for (el in this.board.objects) { 2561 if (this.board.objects.hasOwnProperty(el)) { 2562 pEl = this.board.objects[el]; 2563 if (pEl === glider) { 2564 from = true; 2565 } 2566 2567 if (from && pEl.needsRegularUpdate) { 2568 savetrace = pEl.visProp.trace; 2569 pEl.visProp.trace = false; 2570 pEl.needsUpdate = true; 2571 pEl.update(true); 2572 pEl.visProp.trace = savetrace; 2573 2574 if (pEl === tracepoint) { 2575 break; 2576 } 2577 } 2578 } 2579 } 2580 }; 2581 2582 return c; 2583 }; 2584 2585 JXG.registerElement("tracecurve", JXG.createTracecurve); 2586 2587 /** 2588 * @class This element is used to provide a constructor for step function, which is realized as a special curve. 2589 * 2590 * In case the data points should be updated after creation time, they can be accessed by curve.xterm and curve.yterm. 2591 * @pseudo 2592 * @name Stepfunction 2593 * @augments JXG.Curve 2594 * @constructor 2595 * @type Curve 2596 * @description JXG.Curve 2597 * @param {Array|Function} Parent1 elements of Stepfunction are two arrays containing the coordinates. 2598 * @param {Array|Function} Parent2 2599 * @see JXG.Curve 2600 * @example 2601 * // Create step function. 2602 var curve = board.create('stepfunction', [[0,1,2,3,4,5], [1,3,0,2,2,1]]); 2603 2604 * </pre><div class="jxgbox" id="JXG32342ec9-ad17-4339-8a97-ff23dc34f51a" style="width: 300px; height: 300px;"></div> 2605 * <script type="text/javascript"> 2606 * var sf1_board = JXG.JSXGraph.initBoard('JXG32342ec9-ad17-4339-8a97-ff23dc34f51a', {boundingbox: [-1, 5, 6, -2], axis: true, showcopyright: false, shownavigation: false}); 2607 * var curve = sf1_board.create('stepfunction', [[0,1,2,3,4,5], [1,3,0,2,2,1]]); 2608 * </script><pre> 2609 */ 2610 JXG.createStepfunction = function (board, parents, attributes) { 2611 var c, attr; 2612 if (parents.length !== 2) { 2613 throw new Error( 2614 "JSXGraph: Can't create step function with given parent'" + 2615 "\nPossible parent types: [array, array|function]" 2616 ); 2617 } 2618 2619 attr = Type.copyAttributes(attributes, board.options, "stepfunction"); 2620 c = board.create("curve", parents, attr); 2621 /** 2622 * @class 2623 * @ignore 2624 */ 2625 c.updateDataArray = function () { 2626 var i, 2627 j = 0, 2628 len = this.xterm.length; 2629 2630 this.dataX = []; 2631 this.dataY = []; 2632 2633 if (len === 0) { 2634 return; 2635 } 2636 2637 this.dataX[j] = this.xterm[0]; 2638 this.dataY[j] = this.yterm[0]; 2639 ++j; 2640 2641 for (i = 1; i < len; ++i) { 2642 this.dataX[j] = this.xterm[i]; 2643 this.dataY[j] = this.dataY[j - 1]; 2644 ++j; 2645 this.dataX[j] = this.xterm[i]; 2646 this.dataY[j] = this.yterm[i]; 2647 ++j; 2648 } 2649 }; 2650 2651 return c; 2652 }; 2653 2654 JXG.registerElement("stepfunction", JXG.createStepfunction); 2655 2656 /** 2657 * @class This element is used to provide a constructor for the graph showing 2658 * the (numerical) derivative of a given curve. 2659 * 2660 * @pseudo 2661 * @name Derivative 2662 * @augments JXG.Curve 2663 * @constructor 2664 * @type JXG.Curve 2665 * @param {JXG.Curve} Parent Curve for which the derivative is generated. 2666 * @see JXG.Curve 2667 * @example 2668 * var cu = board.create('cardinalspline', [[[-3,0], [-1,2], [0,1], [2,0], [3,1]], 0.5, 'centripetal'], {createPoints: false}); 2669 * var d = board.create('derivative', [cu], {dash: 2}); 2670 * 2671 * </pre><div id="JXGb9600738-1656-11e8-8184-901b0e1b8723" class="jxgbox" style="width: 300px; height: 300px;"></div> 2672 * <script type="text/javascript"> 2673 * (function() { 2674 * var board = JXG.JSXGraph.initBoard('JXGb9600738-1656-11e8-8184-901b0e1b8723', 2675 * {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false}); 2676 * var cu = board.create('cardinalspline', [[[-3,0], [-1,2], [0,1], [2,0], [3,1]], 0.5, 'centripetal'], {createPoints: false}); 2677 * var d = board.create('derivative', [cu], {dash: 2}); 2678 * 2679 * })(); 2680 * 2681 * </script><pre> 2682 * 2683 */ 2684 JXG.createDerivative = function (board, parents, attributes) { 2685 var c, curve, dx, dy, attr; 2686 2687 if (parents.length !== 1 && parents[0].class !== Const.OBJECT_CLASS_CURVE) { 2688 throw new Error( 2689 "JSXGraph: Can't create derivative curve with given parent'" + 2690 "\nPossible parent types: [curve]" 2691 ); 2692 } 2693 2694 attr = Type.copyAttributes(attributes, board.options, "curve"); 2695 2696 curve = parents[0]; 2697 dx = Numerics.D(curve.X); 2698 dy = Numerics.D(curve.Y); 2699 2700 c = board.create( 2701 "curve", 2702 [ 2703 function (t) { 2704 return curve.X(t); 2705 }, 2706 function (t) { 2707 return dy(t) / dx(t); 2708 }, 2709 curve.minX(), 2710 curve.maxX() 2711 ], 2712 attr 2713 ); 2714 2715 c.setParents(curve); 2716 2717 return c; 2718 }; 2719 2720 JXG.registerElement("derivative", JXG.createDerivative); 2721 2722 /** 2723 * @class Intersection of two closed path elements. The elements may be of type curve, circle, polygon, inequality. 2724 * If one element is a curve, it has to be closed. 2725 * The resulting element is of type curve. 2726 * @pseudo 2727 * @name CurveIntersection 2728 * @param {JXG.Curve|JXG.Polygon|JXG.Circle} curve1 First element which is intersected 2729 * @param {JXG.Curve|JXG.Polygon|JXG.Circle} curve2 Second element which is intersected 2730 * @augments JXG.Curve 2731 * @constructor 2732 * @type JXG.Curve 2733 * 2734 * @example 2735 * var f = board.create('functiongraph', ['cos(x)']); 2736 * var ineq = board.create('inequality', [f], {inverse: true, fillOpacity: 0.1}); 2737 * var circ = board.create('circle', [[0,0], 4]); 2738 * var clip = board.create('curveintersection', [ineq, circ], {fillColor: 'yellow', fillOpacity: 0.6}); 2739 * 2740 * </pre><div id="JXGe2948257-8835-4276-9164-8acccb48e8d4" class="jxgbox" style="width: 300px; height: 300px;"></div> 2741 * <script type="text/javascript"> 2742 * (function() { 2743 * var board = JXG.JSXGraph.initBoard('JXGe2948257-8835-4276-9164-8acccb48e8d4', 2744 * {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false}); 2745 * var f = board.create('functiongraph', ['cos(x)']); 2746 * var ineq = board.create('inequality', [f], {inverse: true, fillOpacity: 0.1}); 2747 * var circ = board.create('circle', [[0,0], 4]); 2748 * var clip = board.create('curveintersection', [ineq, circ], {fillColor: 'yellow', fillOpacity: 0.6}); 2749 * 2750 * })(); 2751 * 2752 * </script><pre> 2753 * 2754 */ 2755 JXG.createCurveIntersection = function (board, parents, attributes) { 2756 var c; 2757 2758 if (parents.length !== 2) { 2759 throw new Error( 2760 "JSXGraph: Can't create curve intersection with given parent'" + 2761 "\nPossible parent types: [array, array|function]" 2762 ); 2763 } 2764 2765 c = board.create("curve", [[], []], attributes); 2766 /** 2767 * @class 2768 * @ignore 2769 */ 2770 c.updateDataArray = function () { 2771 var a = Clip.intersection(parents[0], parents[1], this.board); 2772 this.dataX = a[0]; 2773 this.dataY = a[1]; 2774 }; 2775 return c; 2776 }; 2777 2778 /** 2779 * @class Union of two closed path elements. The elements may be of type curve, circle, polygon, inequality. 2780 * If one element is a curve, it has to be closed. 2781 * The resulting element is of type curve. 2782 * @pseudo 2783 * @name CurveUnion 2784 * @param {JXG.Curve|JXG.Polygon|JXG.Circle} curve1 First element defining the union 2785 * @param {JXG.Curve|JXG.Polygon|JXG.Circle} curve2 Second element defining the union 2786 * @augments JXG.Curve 2787 * @constructor 2788 * @type JXG.Curve 2789 * 2790 * @example 2791 * var f = board.create('functiongraph', ['cos(x)']); 2792 * var ineq = board.create('inequality', [f], {inverse: true, fillOpacity: 0.1}); 2793 * var circ = board.create('circle', [[0,0], 4]); 2794 * var clip = board.create('curveunion', [ineq, circ], {fillColor: 'yellow', fillOpacity: 0.6}); 2795 * 2796 * </pre><div id="JXGe2948257-8835-4276-9164-8acccb48e8d4" class="jxgbox" style="width: 300px; height: 300px;"></div> 2797 * <script type="text/javascript"> 2798 * (function() { 2799 * var board = JXG.JSXGraph.initBoard('JXGe2948257-8835-4276-9164-8acccb48e8d4', 2800 * {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false}); 2801 * var f = board.create('functiongraph', ['cos(x)']); 2802 * var ineq = board.create('inequality', [f], {inverse: true, fillOpacity: 0.1}); 2803 * var circ = board.create('circle', [[0,0], 4]); 2804 * var clip = board.create('curveunion', [ineq, circ], {fillColor: 'yellow', fillOpacity: 0.6}); 2805 * 2806 * })(); 2807 * 2808 * </script><pre> 2809 * 2810 */ 2811 JXG.createCurveUnion = function (board, parents, attributes) { 2812 var c; 2813 2814 if (parents.length !== 2) { 2815 throw new Error( 2816 "JSXGraph: Can't create curve union with given parent'" + 2817 "\nPossible parent types: [array, array|function]" 2818 ); 2819 } 2820 2821 c = board.create("curve", [[], []], attributes); 2822 /** 2823 * @class 2824 * @ignore 2825 */ 2826 c.updateDataArray = function () { 2827 var a = Clip.union(parents[0], parents[1], this.board); 2828 this.dataX = a[0]; 2829 this.dataY = a[1]; 2830 }; 2831 return c; 2832 }; 2833 2834 /** 2835 * @class Difference of two closed path elements. The elements may be of type curve, circle, polygon, inequality. 2836 * If one element is a curve, it has to be closed. 2837 * The resulting element is of type curve. 2838 * @pseudo 2839 * @name CurveDifference 2840 * @param {JXG.Curve|JXG.Polygon|JXG.Circle} curve1 First element from which the second element is "subtracted" 2841 * @param {JXG.Curve|JXG.Polygon|JXG.Circle} curve2 Second element which is subtracted from the first element 2842 * @augments JXG.Curve 2843 * @constructor 2844 * @type JXG.Curve 2845 * 2846 * @example 2847 * var f = board.create('functiongraph', ['cos(x)']); 2848 * var ineq = board.create('inequality', [f], {inverse: true, fillOpacity: 0.1}); 2849 * var circ = board.create('circle', [[0,0], 4]); 2850 * var clip = board.create('curvedifference', [ineq, circ], {fillColor: 'yellow', fillOpacity: 0.6}); 2851 * 2852 * </pre><div id="JXGe2948257-8835-4276-9164-8acccb48e8d4" class="jxgbox" style="width: 300px; height: 300px;"></div> 2853 * <script type="text/javascript"> 2854 * (function() { 2855 * var board = JXG.JSXGraph.initBoard('JXGe2948257-8835-4276-9164-8acccb48e8d4', 2856 * {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false}); 2857 * var f = board.create('functiongraph', ['cos(x)']); 2858 * var ineq = board.create('inequality', [f], {inverse: true, fillOpacity: 0.1}); 2859 * var circ = board.create('circle', [[0,0], 4]); 2860 * var clip = board.create('curvedifference', [ineq, circ], {fillColor: 'yellow', fillOpacity: 0.6}); 2861 * 2862 * })(); 2863 * 2864 * </script><pre> 2865 * 2866 */ 2867 JXG.createCurveDifference = function (board, parents, attributes) { 2868 var c; 2869 2870 if (parents.length !== 2) { 2871 throw new Error( 2872 "JSXGraph: Can't create curve difference with given parent'" + 2873 "\nPossible parent types: [array, array|function]" 2874 ); 2875 } 2876 2877 c = board.create("curve", [[], []], attributes); 2878 /** 2879 * @class 2880 * @ignore 2881 */ 2882 c.updateDataArray = function () { 2883 var a = Clip.difference(parents[0], parents[1], this.board); 2884 this.dataX = a[0]; 2885 this.dataY = a[1]; 2886 }; 2887 return c; 2888 }; 2889 2890 JXG.registerElement("curvedifference", JXG.createCurveDifference); 2891 JXG.registerElement("curveintersection", JXG.createCurveIntersection); 2892 JXG.registerElement("curveunion", JXG.createCurveUnion); 2893 2894 // /** 2895 // * @class Concat of two path elements, in general neither is a closed path. The parent elements have to be curves, too. 2896 // * The resulting element is of type curve. The curve points are simply concatenated. 2897 // * @pseudo 2898 // * @name CurveConcat 2899 // * @param {JXG.Curve} curve1 First curve element. 2900 // * @param {JXG.Curve} curve2 Second curve element. 2901 // * @augments JXG.Curve 2902 // * @constructor 2903 // * @type JXG.Curve 2904 // */ 2905 // JXG.createCurveConcat = function (board, parents, attributes) { 2906 // var c; 2907 2908 // if (parents.length !== 2) { 2909 // throw new Error( 2910 // "JSXGraph: Can't create curve difference with given parent'" + 2911 // "\nPossible parent types: [array, array|function]" 2912 // ); 2913 // } 2914 2915 // c = board.create("curve", [[], []], attributes); 2916 // /** 2917 // * @class 2918 // * @ignore 2919 // */ 2920 // c.updateCurve = function () { 2921 // this.points = parents[0].points.concat( 2922 // [new JXG.Coords(Const.COORDS_BY_USER, [NaN, NaN], this.board)] 2923 // ).concat(parents[1].points); 2924 // this.numberPoints = this.points.length; 2925 // return this; 2926 // }; 2927 2928 // return c; 2929 // }; 2930 2931 // JXG.registerElement("curveconcat", JXG.createCurveConcat); 2932 2933 /** 2934 * @class Box plot curve. The direction of the box plot can be either vertical or horizontal which 2935 * is controlled by the attribute "dir". 2936 * @pseudo 2937 * @name Boxplot 2938 * @param {Array} quantiles Array containing at least five quantiles. The elements can be of type number, function or string. 2939 * @param {Number|Function} axis Axis position of the box plot 2940 * @param {Number|Function} width Width of the rectangle part of the box plot. The width of the first and 4th quantile 2941 * is relative to this width and can be controlled by the attribute "smallWidth". 2942 * @augments JXG.Curve 2943 * @constructor 2944 * @type JXG.Curve 2945 * 2946 * @example 2947 * var Q = [ -1, 2, 3, 3.5, 5 ]; 2948 * 2949 * var b = board.create('boxplot', [Q, 2, 4], {strokeWidth: 3}); 2950 * 2951 * </pre><div id="JXG13eb23a1-a641-41a2-be11-8e03e400a947" class="jxgbox" style="width: 300px; height: 300px;"></div> 2952 * <script type="text/javascript"> 2953 * (function() { 2954 * var board = JXG.JSXGraph.initBoard('JXG13eb23a1-a641-41a2-be11-8e03e400a947', 2955 * {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false}); 2956 * var Q = [ -1, 2, 3, 3.5, 5 ]; 2957 * var b = board.create('boxplot', [Q, 2, 4], {strokeWidth: 3}); 2958 * 2959 * })(); 2960 * 2961 * </script><pre> 2962 * 2963 * @example 2964 * var Q = [ -1, 2, 3, 3.5, 5 ]; 2965 * var b = board.create('boxplot', [Q, 3, 4], {dir: 'horizontal', smallWidth: 0.25, color:'red'}); 2966 * 2967 * </pre><div id="JXG0deb9cb2-84bc-470d-a6db-8be9a5694813" class="jxgbox" style="width: 300px; height: 300px;"></div> 2968 * <script type="text/javascript"> 2969 * (function() { 2970 * var board = JXG.JSXGraph.initBoard('JXG0deb9cb2-84bc-470d-a6db-8be9a5694813', 2971 * {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false}); 2972 * var Q = [ -1, 2, 3, 3.5, 5 ]; 2973 * var b = board.create('boxplot', [Q, 3, 4], {dir: 'horizontal', smallWidth: 0.25, color:'red'}); 2974 * 2975 * })(); 2976 * 2977 * </script><pre> 2978 * 2979 * @example 2980 * var data = [57, 57, 57, 58, 63, 66, 66, 67, 67, 68, 69, 70, 70, 70, 70, 72, 73, 75, 75, 76, 76, 78, 79, 81]; 2981 * var Q = []; 2982 * 2983 * Q[0] = JXG.Math.Statistics.min(data); 2984 * Q = Q.concat(JXG.Math.Statistics.percentile(data, [25, 50, 75])); 2985 * Q[4] = JXG.Math.Statistics.max(data); 2986 * 2987 * var b = board.create('boxplot', [Q, 0, 3]); 2988 * 2989 * </pre><div id="JXGef079e76-ae99-41e4-af29-1d07d83bf85a" class="jxgbox" style="width: 300px; height: 300px;"></div> 2990 * <script type="text/javascript"> 2991 * (function() { 2992 * var board = JXG.JSXGraph.initBoard('JXGef079e76-ae99-41e4-af29-1d07d83bf85a', 2993 * {boundingbox: [-5,90,5,30], axis: true, showcopyright: false, shownavigation: false}); 2994 * var data = [57, 57, 57, 58, 63, 66, 66, 67, 67, 68, 69, 70, 70, 70, 70, 72, 73, 75, 75, 76, 76, 78, 79, 81]; 2995 * var Q = []; 2996 * 2997 * Q[0] = JXG.Math.Statistics.min(data); 2998 * Q = Q.concat(JXG.Math.Statistics.percentile(data, [25, 50, 75])); 2999 * Q[4] = JXG.Math.Statistics.max(data); 3000 * 3001 * var b = board.create('boxplot', [Q, 0, 3]); 3002 * 3003 * })(); 3004 * 3005 * </script><pre> 3006 * 3007 * @example 3008 * var mi = board.create('glider', [0, -1, board.defaultAxes.y]); 3009 * var ma = board.create('glider', [0, 5, board.defaultAxes.y]); 3010 * var Q = [function() { return mi.Y(); }, 2, 3, 3.5, function() { return ma.Y(); }]; 3011 * 3012 * var b = board.create('boxplot', [Q, 0, 2]); 3013 * 3014 * </pre><div id="JXG3b3225da-52f0-42fe-8396-be9016bf289b" class="jxgbox" style="width: 300px; height: 300px;"></div> 3015 * <script type="text/javascript"> 3016 * (function() { 3017 * var board = JXG.JSXGraph.initBoard('JXG3b3225da-52f0-42fe-8396-be9016bf289b', 3018 * {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false}); 3019 * var mi = board.create('glider', [0, -1, board.defaultAxes.y]); 3020 * var ma = board.create('glider', [0, 5, board.defaultAxes.y]); 3021 * var Q = [function() { return mi.Y(); }, 2, 3, 3.5, function() { return ma.Y(); }]; 3022 * 3023 * var b = board.create('boxplot', [Q, 0, 2]); 3024 * 3025 * })(); 3026 * 3027 * </script><pre> 3028 * 3029 */ 3030 JXG.createBoxPlot = function (board, parents, attributes) { 3031 var box, i, len, 3032 attr = Type.copyAttributes(attributes, board.options, "boxplot"); 3033 3034 if (parents.length !== 3) { 3035 throw new Error( 3036 "JSXGraph: Can't create box plot with given parent'" + 3037 "\nPossible parent types: [array, number|function, number|function] containing quantiles, axis, width" 3038 ); 3039 } 3040 if (parents[0].length < 5) { 3041 throw new Error( 3042 "JSXGraph: Can't create box plot with given parent[0]'" + 3043 "\nparent[0] has to contain at least 5 quantiles." 3044 ); 3045 } 3046 box = board.create("curve", [[], []], attr); 3047 3048 len = parents[0].length; // Quantiles 3049 box.Q = []; 3050 for (i = 0; i < len; i++) { 3051 box.Q[i] = Type.createFunction(parents[0][i], board); 3052 } 3053 box.x = Type.createFunction(parents[1], board); 3054 box.w = Type.createFunction(parents[2], board); 3055 3056 /** 3057 * @class 3058 * @ignore 3059 */ 3060 box.updateDataArray = function () { 3061 var v1, v2, l1, l2, r1, r2, w2, dir, x; 3062 3063 w2 = this.evalVisProp('smallwidth'); 3064 dir = this.evalVisProp('dir'); 3065 x = this.x(); 3066 l1 = x - this.w() * 0.5; 3067 l2 = x - this.w() * 0.5 * w2; 3068 r1 = x + this.w() * 0.5; 3069 r2 = x + this.w() * 0.5 * w2; 3070 v1 = [x, l2, r2, x, x, l1, l1, r1, r1, x, NaN, l1, r1, NaN, x, x, l2, r2, x]; 3071 v2 = [ 3072 this.Q[0](), 3073 this.Q[0](), 3074 this.Q[0](), 3075 this.Q[0](), 3076 this.Q[1](), 3077 this.Q[1](), 3078 this.Q[3](), 3079 this.Q[3](), 3080 this.Q[1](), 3081 this.Q[1](), 3082 NaN, 3083 this.Q[2](), 3084 this.Q[2](), 3085 NaN, 3086 this.Q[3](), 3087 this.Q[4](), 3088 this.Q[4](), 3089 this.Q[4](), 3090 this.Q[4]() 3091 ]; 3092 if (dir === "vertical") { 3093 this.dataX = v1; 3094 this.dataY = v2; 3095 } else { 3096 this.dataX = v2; 3097 this.dataY = v1; 3098 } 3099 }; 3100 3101 box.addParentsFromJCFunctions([box.Q, box.x, box.w]); 3102 3103 return box; 3104 }; 3105 3106 JXG.registerElement("boxplot", JXG.createBoxPlot); 3107 3108 /** 3109 * 3110 * @class 3111 * From <a href="https://en.wikipedia.org/wiki/Implicit_curve">Wikipedia</a>: 3112 * "An implicit curve is a plane curve defined by an implicit equation 3113 * relating two coordinate variables, commonly <i>x</i> and <i>y</i>. 3114 * For example, the unit circle is defined by the implicit equation 3115 * x<sup>2</sup> + y<sup>2</sup> = 1. 3116 * In general, every implicit curve is defined by an equation of the form 3117 * <i>f(x, y) = 0</i> 3118 * for some function <i>f</i> of two variables." 3119 * <p> 3120 * The partial derivatives for <i>f</i> are optional. If not given, numerical 3121 * derivatives are used instead. This is good enough for most practical use cases. 3122 * But if supplied, both partial derivatives must be supplied. 3123 * <p> 3124 * The most effective attributes to tinker with if the implicit curve algorithm fails are 3125 * {@link ImplicitCurve#resolution_outer}, 3126 * {@link ImplicitCurve#resolution_inner}, 3127 * {@link ImplicitCurve#alpha_0}, 3128 * {@link ImplicitCurve#h_initial}, 3129 * {@link ImplicitCurve#h_max}, and 3130 * {@link ImplicitCurve#qdt_box}. 3131 * 3132 * @pseudo 3133 * @name ImplicitCurve 3134 * @param {Function|String} f Function of two variables for the left side of the equation <i>f(x,y)=0</i>. 3135 * If f is supplied as string, it has to use the variables 'x' and 'y'. 3136 * @param {Function|String} [dfx=null] Optional partial derivative in respect to the first variable 3137 * If dfx is supplied as string, it has to use the variables 'x' and 'y'. 3138 * @param {Function|String} [dfy=null] Optional partial derivative in respect to the second variable 3139 * If dfy is supplied as string, it has to use the variables 'x' and 'y'. 3140 * @param {Array|Function} [rangex=boundingbox] Optional array of length 2 3141 * of the form [x_min, x_max] setting the domain of the x coordinate of the implicit curve. 3142 * If not supplied, the board's boundingbox (+ the attribute "margin") is taken. 3143 * For algorithmic reasons, the plotted curve mighty slightly overflow the given domain. 3144 * @param {Array|Function} [rangey=boundingbox] Optional array of length 2 3145 * of the form [y_min, y_max] setting the domain of the y coordinate of the implicit curve. 3146 * If not supplied, the board's boundingbox (+ the attribute "margin") is taken. 3147 * For algorithmic reasons, the plotted curve mighty slightly overflow the given domain. 3148 * @augments JXG.Curve 3149 * @constructor 3150 * @type JXG.Curve 3151 * 3152 * @example 3153 * var f, c; 3154 * f = (x, y) => 1 / 16 * x ** 2 + y ** 2 - 1; 3155 * c = board.create('implicitcurve', [f], { 3156 * strokeWidth: 3, 3157 * strokeColor: JXG.palette.red, 3158 * strokeOpacity: 0.8 3159 * }); 3160 * 3161 * </pre><div id="JXGa6e86701-1a82-48d0-b007-3a3d32075076" class="jxgbox" style="width: 300px; height: 300px;"></div> 3162 * <script type="text/javascript"> 3163 * (function() { 3164 * var board = JXG.JSXGraph.initBoard('JXGa6e86701-1a82-48d0-b007-3a3d32075076', 3165 * {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false}); 3166 * var f, c; 3167 * f = (x, y) => 1 / 16 * x ** 2 + y ** 2 - 1; 3168 * c = board.create('implicitcurve', [f], { 3169 * strokeWidth: 3, 3170 * strokeColor: JXG.palette.red, 3171 * strokeOpacity: 0.8 3172 * }); 3173 * 3174 * })(); 3175 * 3176 * </script><pre> 3177 * 3178 * @example 3179 * var a, c, f; 3180 * a = board.create('slider', [[-3, 6], [3, 6], [-3, 1, 3]], { 3181 * name: 'a', stepWidth: 0.1 3182 * }); 3183 * f = (x, y) => x ** 2 - 2 * x * y - 2 * x + (a.Value() + 1) * y ** 2 + (4 * a.Value() + 2) * y + 4 * a.Value() - 3; 3184 * c = board.create('implicitcurve', [f], { 3185 * strokeWidth: 3, 3186 * strokeColor: JXG.palette.red, 3187 * strokeOpacity: 0.8, 3188 * resolution_outer: 20, 3189 * resolution_inner: 20 3190 * }); 3191 * 3192 * </pre><div id="JXG0b133a54-9509-4a65-9722-9c5145e23b40" class="jxgbox" style="width: 300px; height: 300px;"></div> 3193 * <script type="text/javascript"> 3194 * (function() { 3195 * var board = JXG.JSXGraph.initBoard('JXG0b133a54-9509-4a65-9722-9c5145e23b40', 3196 * {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false}); 3197 * var a, c, f; 3198 * a = board.create('slider', [[-3, 6], [3, 6], [-3, 1, 3]], { 3199 * name: 'a', stepWidth: 0.1 3200 * }); 3201 * f = (x, y) => x ** 2 - 2 * x * y - 2 * x + (a.Value() + 1) * y ** 2 + (4 * a.Value() + 2) * y + 4 * a.Value() - 3; 3202 * c = board.create('implicitcurve', [f], { 3203 * strokeWidth: 3, 3204 * strokeColor: JXG.palette.red, 3205 * strokeOpacity: 0.8, 3206 * resolution_outer: 20, 3207 * resolution_inner: 20 3208 * }); 3209 * 3210 * })(); 3211 * 3212 * </script><pre> 3213 * 3214 * @example 3215 * var c = board.create('implicitcurve', ['abs(x * y) - 3'], { 3216 * strokeWidth: 3, 3217 * strokeColor: JXG.palette.red, 3218 * strokeOpacity: 0.8 3219 * }); 3220 * 3221 * </pre><div id="JXG02802981-0abb-446b-86ea-ee588f02ed1a" class="jxgbox" style="width: 300px; height: 300px;"></div> 3222 * <script type="text/javascript"> 3223 * (function() { 3224 * var board = JXG.JSXGraph.initBoard('JXG02802981-0abb-446b-86ea-ee588f02ed1a', 3225 * {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false}); 3226 * var c = board.create('implicitcurve', ['abs(x * y) - 3'], { 3227 * strokeWidth: 3, 3228 * strokeColor: JXG.palette.red, 3229 * strokeOpacity: 0.8 3230 * }); 3231 * 3232 * })(); 3233 * 3234 * </script><pre> 3235 * 3236 * @example 3237 * var niveauline = []; 3238 * niveauline = [0.5, 1, 1.5, 2]; 3239 * for (let i = 0; i < niveauline.length; i++) { 3240 * board.create("implicitcurve", [ 3241 * (x, y) => x ** .5 * y ** .5 - niveauline[i], 3242 [0.25, 3], [0.5, 4] // Domain 3243 * ], { 3244 * strokeWidth: 2, 3245 * strokeColor: JXG.palette.red, 3246 * strokeOpacity: (1 + i) / niveauline.length, 3247 * needsRegularUpdate: false 3248 * }); 3249 * } 3250 * 3251 * </pre><div id="JXGccee9aab-6dd9-4a79-827d-3164f70cc6a1" class="jxgbox" style="width: 300px; height: 300px;"></div> 3252 * <script type="text/javascript"> 3253 * (function() { 3254 * var board = JXG.JSXGraph.initBoard('JXGccee9aab-6dd9-4a79-827d-3164f70cc6a1', 3255 * {boundingbox: [-1, 5, 5,-1], axis: true, showcopyright: false, shownavigation: false}); 3256 * var niveauline = []; 3257 * niveauline = [0.5, 1, 1.5, 2]; 3258 * for (let i = 0; i < niveauline.length; i++) { 3259 * board.create("implicitcurve", [ 3260 * (x, y) => x ** .5 * y ** .5 - niveauline[i], 3261 * [0.25, 3], [0.5, 4] 3262 * ], { 3263 * strokeWidth: 2, 3264 * strokeColor: JXG.palette.red, 3265 * strokeOpacity: (1 + i) / niveauline.length, 3266 * needsRegularUpdate: false 3267 * }); 3268 * } 3269 * 3270 * })(); 3271 * 3272 * </script><pre> 3273 * 3274 */ 3275 JXG.createImplicitCurve = function (board, parents, attributes) { 3276 var c, attr; 3277 3278 if ([1, 3, 5].indexOf(parents.length) < 0) { 3279 throw new Error( 3280 "JSXGraph: Can't create curve implicitCurve with given parent'" + 3281 "\nPossible parent types: [f], [f, rangex, rangey], [f, dfx, dfy] or [f, dfx, dfy, rangex, rangey]" + 3282 "\nwith functions f, dfx, dfy and arrays of length 2 rangex, rangey." 3283 ); 3284 } 3285 3286 // if (parents.length === 3) { 3287 // if (!Type.isArray(parents[1]) && !Type.isArray(parents[2])) { 3288 // throw new Error( 3289 // "JSXGraph: Can't create curve implicitCurve with given parent'" + 3290 // "\nPossible parent types: [f], [f, rangex, rangey], [f, dfx, dfy] or [f, dfx, dfy, rangex, rangey]" + 3291 // "\nwith functions f, dfx, dfy and arrays of length 2 rangex, rangey." 3292 // ); 3293 // } 3294 // } 3295 // if (parents.length === 5) { 3296 // if (!Type.isArray(parents[3]) && !Type.isArray(parents[4])) { 3297 // throw new Error( 3298 // "JSXGraph: Can't create curve implicitCurve with given parent'" + 3299 // "\nPossible parent types: [f], [f, rangex, rangey], [f, dfx, dfy] or [f, dfx, dfy, rangex, rangey]" + 3300 // "\nwith functions f, dfx, dfy and arrays of length 2 rangex, rangey." 3301 // ); 3302 // } 3303 // } 3304 3305 attr = Type.copyAttributes(attributes, board.options, "implicitcurve"); 3306 c = board.create("curve", [[], []], attr); 3307 3308 /** 3309 * Function of two variables for the left side of the equation <i>f(x,y)=0</i>. 3310 * 3311 * @name f 3312 * @memberOf ImplicitCurve.prototype 3313 * @function 3314 * @returns {Number} 3315 */ 3316 c.f = Type.createFunction(parents[0], board, 'x, y'); 3317 3318 /** 3319 * Partial derivative in the first variable of 3320 * the left side of the equation <i>f(x,y)=0</i>. 3321 * If null, then numerical derivative is used. 3322 * 3323 * @name dfx 3324 * @memberOf ImplicitCurve.prototype 3325 * @function 3326 * @returns {Number} 3327 */ 3328 c.dfx = Type.createFunction(parents[1], board, 'x, y'); 3329 3330 /** 3331 * Partial derivative in the second variable of 3332 * the left side of the equation <i>f(x,y)=0</i>. 3333 * If null, then numerical derivative is used. 3334 * 3335 * @name dfy 3336 * @memberOf ImplicitCurve.prototype 3337 * @function 3338 * @returns {Number} 3339 */ 3340 c.dfy = Type.createFunction(parents[2], board, 'x, y'); 3341 3342 /** 3343 * Defines a domain for searching f(x,y)=0. Default is null, meaning 3344 * the bounding box of the board is used. 3345 * Using domain, visProp.margin is ignored. 3346 * @name domain 3347 * @memberOf ImplicitCurve.prototype 3348 * @param {Array} of length 4 defining the domain used to compute the implict curve. 3349 * Syntax: [x_min, y_max, x_max, y_min] 3350 */ 3351 // c.domain = board.getBoundingBox(); 3352 c.domain = null; 3353 if (parents.length === 5) { 3354 c.domain = [parents[3], parents[4]]; 3355 // c.visProp.margin = 0; 3356 } else if (parents.length === 3) { 3357 c.domain = [parents[1], parents[2]]; 3358 // c.visProp.margin = 0; 3359 } 3360 3361 /** 3362 * @class 3363 * @ignore 3364 */ 3365 c.updateDataArray = function () { 3366 var bbox, rx, ry, 3367 ip, cfg, 3368 ret = [], 3369 mgn; 3370 3371 if (this.domain === null) { 3372 mgn = this.evalVisProp('margin'); 3373 bbox = this.board.getBoundingBox(); 3374 bbox[0] -= mgn; 3375 bbox[1] += mgn; 3376 bbox[2] += mgn; 3377 bbox[3] -= mgn; 3378 } else { 3379 rx = Type.evaluate(this.domain[0]); 3380 ry = Type.evaluate(this.domain[1]); 3381 bbox = [rx[0], ry[1], rx[1], ry[0]]; 3382 } 3383 3384 cfg = { 3385 resolution_out: Math.max(0.01, this.evalVisProp('resolution_outer')), 3386 resolution_in: Math.max(0.01, this.evalVisProp('resolution_inner')), 3387 max_steps: this.evalVisProp('max_steps'), 3388 alpha_0: this.evalVisProp('alpha_0'), 3389 tol_u0: this.evalVisProp('tol_u0'), 3390 tol_newton: this.evalVisProp('tol_newton'), 3391 tol_cusp: this.evalVisProp('tol_cusp'), 3392 tol_progress: this.evalVisProp('tol_progress'), 3393 qdt_box: this.evalVisProp('qdt_box'), 3394 kappa_0: this.evalVisProp('kappa_0'), 3395 delta_0: this.evalVisProp('delta_0'), 3396 h_initial: this.evalVisProp('h_initial'), 3397 h_critical: this.evalVisProp('h_critical'), 3398 h_max: this.evalVisProp('h_max'), 3399 loop_dist: this.evalVisProp('loop_dist'), 3400 loop_dir: this.evalVisProp('loop_dir'), 3401 loop_detection: this.evalVisProp('loop_detection'), 3402 unitX: this.board.unitX, 3403 unitY: this.board.unitY 3404 }; 3405 this.dataX = []; 3406 this.dataY = []; 3407 3408 // console.time("implicit plot"); 3409 ip = new ImplicitPlot(bbox, cfg, this.f, this.dfx, this.dfy); 3410 this.qdt = ip.qdt; 3411 3412 ret = ip.plot(); 3413 // console.timeEnd("implicit plot"); 3414 3415 this.dataX = ret[0]; 3416 this.dataY = ret[1]; 3417 }; 3418 3419 c.elType = 'implicitcurve'; 3420 3421 return c; 3422 }; 3423 3424 JXG.registerElement("implicitcurve", JXG.createImplicitCurve); 3425 3426 3427 export default JXG.Curve; 3428 3429 // export default { 3430 // Curve: JXG.Curve, 3431 // createCardinalSpline: JXG.createCardinalSpline, 3432 // createCurve: JXG.createCurve, 3433 // createCurveDifference: JXG.createCurveDifference, 3434 // createCurveIntersection: JXG.createCurveIntersection, 3435 // createCurveUnion: JXG.createCurveUnion, 3436 // createDerivative: JXG.createDerivative, 3437 // createFunctiongraph: JXG.createFunctiongraph, 3438 // createMetapostSpline: JXG.createMetapostSpline, 3439 // createPlot: JXG.createFunctiongraph, 3440 // createSpline: JXG.createSpline, 3441 // createRiemannsum: JXG.createRiemannsum, 3442 // createStepfunction: JXG.createStepfunction, 3443 // createTracecurve: JXG.createTracecurve 3444 // }; 3445 3446 // const Curve = JXG.Curve; 3447 // export { Curve as default, Curve}; 3448