1 /* 2 Copyright 2008-2023 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"; 40 import Clip from "../math/clip"; 41 import Const from "./constants"; 42 import Coords from "./coords"; 43 import Geometry from "../math/geometry"; 44 import GeometryElement from "./element"; 45 import GeonextParser from "../parser/geonext"; 46 import ImplicitPlot from "../math/implicitplot"; 47 import Mat from "../math/math"; 48 import Metapost from "../math/metapost"; 49 import Numerics from "../math/numerics"; 50 import Plot from "../math/plot"; 51 import QDT from "../math/qdt"; 52 import Type from "../utils/type"; 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 = Type.evaluate(this.visProp.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 (Type.evaluate(this.visProp.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 (Type.evaluate(this.visProp.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 = Type.evaluate(this.visProp.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(Type.evaluate(this.visProp.precision))) { 257 type = this.board._inputDevice; 258 prec = Type.evaluate(this.visProp.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 && Type.evaluate(this.visProp.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 += Type.evaluate(this.visProp.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 = Type.evaluate(this.visProp.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 Type.evaluate(this.visProp.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 (Type.evaluate(this.visProp.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 (Type.evaluate(this.visProp.doadvancedplot)) { 781 // console.time("plot"); 782 783 if (version === 1 || Type.evaluate(this.visProp.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 = Type.evaluate(this.visProp.numberpointshigh); 798 } else { 799 this.numberPoints = Type.evaluate(this.visProp.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 Type.evaluate(this.visProp.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 Type.evaluate(this.visProp.curvetype) !== "plot" && 833 Type.evaluate(this.visProp.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 c, 1110 x, 1111 y, 1112 ax = 0.05 * this.board.canvasWidth, 1113 ay = 0.05 * this.board.canvasHeight, 1114 bx = 0.95 * this.board.canvasWidth, 1115 by = 0.95 * this.board.canvasHeight; 1116 1117 switch (Type.evaluate(this.visProp.label.position)) { 1118 case "ulft": 1119 x = ax; 1120 y = ay; 1121 break; 1122 case "llft": 1123 x = ax; 1124 y = by; 1125 break; 1126 case "rt": 1127 x = bx; 1128 y = 0.5 * by; 1129 break; 1130 case "lrt": 1131 x = bx; 1132 y = by; 1133 break; 1134 case "urt": 1135 x = bx; 1136 y = ay; 1137 break; 1138 case "top": 1139 x = 0.5 * bx; 1140 y = ay; 1141 break; 1142 case "bot": 1143 x = 0.5 * bx; 1144 y = by; 1145 break; 1146 default: 1147 // includes case 'lft' 1148 x = ax; 1149 y = 0.5 * by; 1150 } 1151 1152 c = new Coords(Const.COORDS_BY_SCREEN, [x, y], this.board, false); 1153 return Geometry.projectCoordsToCurve( 1154 c.usrCoords[1], 1155 c.usrCoords[2], 1156 0, 1157 this, 1158 this.board 1159 )[0]; 1160 }, 1161 1162 // documented in geometry element 1163 cloneToBackground: function () { 1164 var er, 1165 copy = { 1166 id: this.id + "T" + this.numTraces, 1167 elementClass: Const.OBJECT_CLASS_CURVE, 1168 1169 points: this.points.slice(0), 1170 bezierDegree: this.bezierDegree, 1171 numberPoints: this.numberPoints, 1172 board: this.board, 1173 visProp: Type.deepCopy(this.visProp, this.visProp.traceattributes, true) 1174 }; 1175 1176 copy.visProp.layer = this.board.options.layer.trace; 1177 copy.visProp.curvetype = this.visProp.curvetype; 1178 this.numTraces++; 1179 1180 Type.clearVisPropOld(copy); 1181 copy.visPropCalc = { 1182 visible: Type.evaluate(copy.visProp.visible) 1183 }; 1184 er = this.board.renderer.enhancedRendering; 1185 this.board.renderer.enhancedRendering = true; 1186 this.board.renderer.drawCurve(copy); 1187 this.board.renderer.enhancedRendering = er; 1188 this.traces[copy.id] = copy.rendNode; 1189 1190 return this; 1191 }, 1192 1193 // Already documented in GeometryElement 1194 bounds: function () { 1195 var minX = Infinity, 1196 maxX = -Infinity, 1197 minY = Infinity, 1198 maxY = -Infinity, 1199 l = this.points.length, 1200 i, 1201 bezier, 1202 up; 1203 1204 if (this.bezierDegree === 3) { 1205 // Add methods X(), Y() 1206 for (i = 0; i < l; i++) { 1207 this.points[i].X = Type.bind(function () { 1208 return this.usrCoords[1]; 1209 }, this.points[i]); 1210 this.points[i].Y = Type.bind(function () { 1211 return this.usrCoords[2]; 1212 }, this.points[i]); 1213 } 1214 bezier = Numerics.bezier(this.points); 1215 up = bezier[3](); 1216 minX = Numerics.fminbr( 1217 function (t) { 1218 return bezier[0](t); 1219 }, 1220 [0, up] 1221 ); 1222 maxX = Numerics.fminbr( 1223 function (t) { 1224 return -bezier[0](t); 1225 }, 1226 [0, up] 1227 ); 1228 minY = Numerics.fminbr( 1229 function (t) { 1230 return bezier[1](t); 1231 }, 1232 [0, up] 1233 ); 1234 maxY = Numerics.fminbr( 1235 function (t) { 1236 return -bezier[1](t); 1237 }, 1238 [0, up] 1239 ); 1240 1241 minX = bezier[0](minX); 1242 maxX = bezier[0](maxX); 1243 minY = bezier[1](minY); 1244 maxY = bezier[1](maxY); 1245 return [minX, maxY, maxX, minY]; 1246 } 1247 1248 // Linear segments 1249 for (i = 0; i < l; i++) { 1250 if (minX > this.points[i].usrCoords[1]) { 1251 minX = this.points[i].usrCoords[1]; 1252 } 1253 1254 if (maxX < this.points[i].usrCoords[1]) { 1255 maxX = this.points[i].usrCoords[1]; 1256 } 1257 1258 if (minY > this.points[i].usrCoords[2]) { 1259 minY = this.points[i].usrCoords[2]; 1260 } 1261 1262 if (maxY < this.points[i].usrCoords[2]) { 1263 maxY = this.points[i].usrCoords[2]; 1264 } 1265 } 1266 1267 return [minX, maxY, maxX, minY]; 1268 }, 1269 1270 // documented in element.js 1271 getParents: function () { 1272 var p = [this.xterm, this.yterm, this.minX(), this.maxX()]; 1273 1274 if (this.parents.length !== 0) { 1275 p = this.parents; 1276 } 1277 1278 return p; 1279 }, 1280 1281 /** 1282 * Shift the curve by the vector 'where'. 1283 * 1284 * @param {Array} where Array containing the x and y coordinate of the target location. 1285 * @returns {JXG.Curve} Reference to itself. 1286 */ 1287 moveTo: function (where) { 1288 // TODO add animation 1289 var delta = [], 1290 p; 1291 if (this.points.length > 0 && !Type.evaluate(this.visProp.fixed)) { 1292 p = this.points[0]; 1293 if (where.length === 3) { 1294 delta = [ 1295 where[0] - p.usrCoords[0], 1296 where[1] - p.usrCoords[1], 1297 where[2] - p.usrCoords[2] 1298 ]; 1299 } else { 1300 delta = [where[0] - p.usrCoords[1], where[1] - p.usrCoords[2]]; 1301 } 1302 this.setPosition(Const.COORDS_BY_USER, delta); 1303 } 1304 return this; 1305 }, 1306 1307 /** 1308 * If the curve is the result of a transformation applied 1309 * to a continuous curve, the glider projection has to be done 1310 * on the original curve. Otherwise there will be problems 1311 * when changing between high and low precision plotting, 1312 * since there number of points changes. 1313 * 1314 * @private 1315 * @returns {Array} [Boolean, curve]: Array contining 'true' if curve is result of a transformation, 1316 * and the source curve of the transformation. 1317 */ 1318 getTransformationSource: function () { 1319 var isTransformed, curve_org; 1320 if (Type.exists(this._transformationSource)) { 1321 curve_org = this._transformationSource; 1322 if ( 1323 curve_org.elementClass === Const.OBJECT_CLASS_CURVE //&& 1324 //Type.evaluate(curve_org.visProp.curvetype) !== 'plot' 1325 ) { 1326 isTransformed = true; 1327 } 1328 } 1329 return [isTransformed, curve_org]; 1330 } 1331 1332 // See JXG.Math.Geometry.pnpoly 1333 // pnpoly: function (x_in, y_in, coord_type) { 1334 // var i, 1335 // j, 1336 // len, 1337 // x, 1338 // y, 1339 // crds, 1340 // v = this.points, 1341 // isIn = false; 1342 1343 // if (coord_type === Const.COORDS_BY_USER) { 1344 // crds = new Coords(Const.COORDS_BY_USER, [x_in, y_in], this.board); 1345 // x = crds.scrCoords[1]; 1346 // y = crds.scrCoords[2]; 1347 // } else { 1348 // x = x_in; 1349 // y = y_in; 1350 // } 1351 1352 // len = this.points.length; 1353 // for (i = 0, j = len - 2; i < len - 1; j = i++) { 1354 // if ( 1355 // v[i].scrCoords[2] > y !== v[j].scrCoords[2] > y && 1356 // x < 1357 // ((v[j].scrCoords[1] - v[i].scrCoords[1]) * (y - v[i].scrCoords[2])) / 1358 // (v[j].scrCoords[2] - v[i].scrCoords[2]) + 1359 // v[i].scrCoords[1] 1360 // ) { 1361 // isIn = !isIn; 1362 // } 1363 // } 1364 1365 // return isIn; 1366 // } 1367 } 1368 ); 1369 1370 /** 1371 * @class This element is used to provide a constructor for curve, which is just a wrapper for element {@link Curve}. 1372 * 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]. 1373 * <p> 1374 * The following types of curves can be plotted: 1375 * <ul> 1376 * <li> parametric curves: t mapsto (x(t),y(t)), where x() and y() are univariate functions. 1377 * <li> polar curves: curves commonly written with polar equations like spirals and cardioids. 1378 * <li> data plots: plot line segments through a given list of coordinates. 1379 * </ul> 1380 * @pseudo 1381 * @name Curve 1382 * @augments JXG.Curve 1383 * @constructor 1384 * @type Object 1385 * @description JXG.Curve 1386 1387 * @param {function,number_function,number_function,number_function,number} x,y,a_,b_ Parent elements for Parametric Curves. 1388 * <p> 1389 * x describes the x-coordinate of the curve. It may be a function term in one variable, e.g. x(t). 1390 * In case of x being of type number, x(t) is set to a constant function. 1391 * this function at the values of the array. 1392 * </p> 1393 * <p> 1394 * y describes the y-coordinate of the curve. In case of a number, y(t) is set to the constant function 1395 * returning this number. 1396 * </p> 1397 * <p> 1398 * Further parameters are an optional number or function for the left interval border a, 1399 * and an optional number or function for the right interval border b. 1400 * </p> 1401 * <p> 1402 * Default values are a=-10 and b=10. 1403 * </p> 1404 * 1405 * @param {array_array,function,number} 1406 * 1407 * @description x,y Parent elements for Data Plots. 1408 * <p> 1409 * x and y are arrays contining the x and y coordinates of the data points which are connected by 1410 * line segments. The individual entries of x and y may also be functions. 1411 * In case of x being an array the curve type is data plot, regardless of the second parameter and 1412 * if additionally the second parameter y is a function term the data plot evaluates. 1413 * </p> 1414 * @param {function_array,function,number_function,number_function,number} 1415 * @description r,offset_,a_,b_ Parent elements for Polar Curves. 1416 * <p> 1417 * The first parameter is a function term r(phi) describing the polar curve. 1418 * </p> 1419 * <p> 1420 * The second parameter is the offset of the curve. It has to be 1421 * an array containing numbers or functions describing the offset. Default value is the origin [0,0]. 1422 * </p> 1423 * <p> 1424 * Further parameters are an optional number or function for the left interval border a, 1425 * and an optional number or function for the right interval border b. 1426 * </p> 1427 * <p> 1428 * Default values are a=-10 and b=10. 1429 * </p> 1430 * <p> 1431 * Additionally, a curve can be created by providing a curve and a transformation (or an array of transformations). 1432 * The result is a curve which is the transformation of the supplied curve. 1433 * 1434 * @see JXG.Curve 1435 * @example 1436 * // Parametric curve 1437 * // Create a curve of the form (t-sin(t), 1-cos(t), i.e. 1438 * // the cycloid curve. 1439 * var graph = board.create('curve', 1440 * [function(t){ return t-Math.sin(t);}, 1441 * function(t){ return 1-Math.cos(t);}, 1442 * 0, 2*Math.PI] 1443 * ); 1444 * </pre><div class="jxgbox" id="JXGaf9f818b-f3b6-4c4d-8c4c-e4a4078b726d" style="width: 300px; height: 300px;"></div> 1445 * <script type="text/javascript"> 1446 * var c1_board = JXG.JSXGraph.initBoard('JXGaf9f818b-f3b6-4c4d-8c4c-e4a4078b726d', {boundingbox: [-1, 5, 7, -1], axis: true, showcopyright: false, shownavigation: false}); 1447 * var graph1 = c1_board.create('curve', [function(t){ return t-Math.sin(t);},function(t){ return 1-Math.cos(t);},0, 2*Math.PI]); 1448 * </script><pre> 1449 * @example 1450 * // Data plots 1451 * // Connect a set of points given by coordinates with dashed line segments. 1452 * // The x- and y-coordinates of the points are given in two separate 1453 * // arrays. 1454 * var x = [0,1,2,3,4,5,6,7,8,9]; 1455 * var y = [9.2,1.3,7.2,-1.2,4.0,5.3,0.2,6.5,1.1,0.0]; 1456 * var graph = board.create('curve', [x,y], {dash:2}); 1457 * </pre><div class="jxgbox" id="JXG7dcbb00e-b6ff-481d-b4a8-887f5d8c6a83" style="width: 300px; height: 300px;"></div> 1458 * <script type="text/javascript"> 1459 * var c3_board = JXG.JSXGraph.initBoard('JXG7dcbb00e-b6ff-481d-b4a8-887f5d8c6a83', {boundingbox: [-1,10,10,-1], axis: true, showcopyright: false, shownavigation: false}); 1460 * var x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; 1461 * var y = [9.2, 1.3, 7.2, -1.2, 4.0, 5.3, 0.2, 6.5, 1.1, 0.0]; 1462 * var graph3 = c3_board.create('curve', [x,y], {dash:2}); 1463 * </script><pre> 1464 * @example 1465 * // Polar plot 1466 * // Create a curve with the equation r(phi)= a*(1+phi), i.e. 1467 * // a cardioid. 1468 * var a = board.create('slider',[[0,2],[2,2],[0,1,2]]); 1469 * var graph = board.create('curve', 1470 * [function(phi){ return a.Value()*(1-Math.cos(phi));}, 1471 * [1,0], 1472 * 0, 2*Math.PI], 1473 * {curveType: 'polar'} 1474 * ); 1475 * </pre><div class="jxgbox" id="JXGd0bc7a2a-8124-45ca-a6e7-142321a8f8c2" style="width: 300px; height: 300px;"></div> 1476 * <script type="text/javascript"> 1477 * var c2_board = JXG.JSXGraph.initBoard('JXGd0bc7a2a-8124-45ca-a6e7-142321a8f8c2', {boundingbox: [-3,3,3,-3], axis: true, showcopyright: false, shownavigation: false}); 1478 * var a = c2_board.create('slider',[[0,2],[2,2],[0,1,2]]); 1479 * var graph2 = c2_board.create('curve', [function(phi){ return a.Value()*(1-Math.cos(phi));}, [1,0], 0, 2*Math.PI], {curveType: 'polar'}); 1480 * </script><pre> 1481 * 1482 * @example 1483 * // Draggable Bezier curve 1484 * var col, p, c; 1485 * col = 'blue'; 1486 * p = []; 1487 * p.push(board.create('point',[-2, -1 ], {size: 5, strokeColor:col, fillColor:col})); 1488 * p.push(board.create('point',[1, 2.5 ], {size: 5, strokeColor:col, fillColor:col})); 1489 * p.push(board.create('point',[-1, -2.5 ], {size: 5, strokeColor:col, fillColor:col})); 1490 * p.push(board.create('point',[2, -2], {size: 5, strokeColor:col, fillColor:col})); 1491 * 1492 * c = board.create('curve', JXG.Math.Numerics.bezier(p), 1493 * {strokeColor:'red', name:"curve", strokeWidth:5, fixed: false}); // Draggable curve 1494 * c.addParents(p); 1495 * </pre><div class="jxgbox" id="JXG7bcc6280-f6eb-433e-8281-c837c3387849" style="width: 300px; height: 300px;"></div> 1496 * <script type="text/javascript"> 1497 * (function(){ 1498 * var board, col, p, c; 1499 * board = JXG.JSXGraph.initBoard('JXG7bcc6280-f6eb-433e-8281-c837c3387849', {boundingbox: [-3,3,3,-3], axis: true, showcopyright: false, shownavigation: false}); 1500 * col = 'blue'; 1501 * p = []; 1502 * p.push(board.create('point',[-2, -1 ], {size: 5, strokeColor:col, fillColor:col})); 1503 * p.push(board.create('point',[1, 2.5 ], {size: 5, strokeColor:col, fillColor:col})); 1504 * p.push(board.create('point',[-1, -2.5 ], {size: 5, strokeColor:col, fillColor:col})); 1505 * p.push(board.create('point',[2, -2], {size: 5, strokeColor:col, fillColor:col})); 1506 * 1507 * c = board.create('curve', JXG.Math.Numerics.bezier(p), 1508 * {strokeColor:'red', name:"curve", strokeWidth:5, fixed: false}); // Draggable curve 1509 * c.addParents(p); 1510 * })(); 1511 * </script><pre> 1512 * 1513 * @example 1514 * // The curve cu2 is the reflection of cu1 against line li 1515 * var li = board.create('line', [1,1,1], {strokeColor: '#aaaaaa'}); 1516 * var reflect = board.create('transform', [li], {type: 'reflect'}); 1517 * var cu1 = board.create('curve', [[-1, -1, -0.5, -1, -1, -0.5], [-3, -2, -2, -2, -2.5, -2.5]]); 1518 * var cu2 = board.create('curve', [cu1, reflect], {strokeColor: 'red'}); 1519 * 1520 * </pre><div id="JXG866dc7a2-d448-11e7-93b3-901b0e1b8723" class="jxgbox" style="width: 300px; height: 300px;"></div> 1521 * <script type="text/javascript"> 1522 * (function() { 1523 * var board = JXG.JSXGraph.initBoard('JXG866dc7a2-d448-11e7-93b3-901b0e1b8723', 1524 * {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false}); 1525 * var li = board.create('line', [1,1,1], {strokeColor: '#aaaaaa'}); 1526 * var reflect = board.create('transform', [li], {type: 'reflect'}); 1527 * var cu1 = board.create('curve', [[-1, -1, -0.5, -1, -1, -0.5], [-3, -2, -2, -2, -2.5, -2.5]]); 1528 * var cu2 = board.create('curve', [cu1, reflect], {strokeColor: 'red'}); 1529 * 1530 * })(); 1531 * 1532 * </script><pre> 1533 */ 1534 JXG.createCurve = function (board, parents, attributes) { 1535 var obj, 1536 cu, 1537 attr = Type.copyAttributes(attributes, board.options, "curve"); 1538 1539 obj = board.select(parents[0], true); 1540 if ( 1541 Type.isTransformationOrArray(parents[1]) && 1542 Type.isObject(obj) && 1543 (obj.type === Const.OBJECT_TYPE_CURVE || 1544 obj.type === Const.OBJECT_TYPE_ANGLE || 1545 obj.type === Const.OBJECT_TYPE_ARC || 1546 obj.type === Const.OBJECT_TYPE_CONIC || 1547 obj.type === Const.OBJECT_TYPE_SECTOR) 1548 ) { 1549 if (obj.type === Const.OBJECT_TYPE_SECTOR) { 1550 attr = Type.copyAttributes(attributes, board.options, "sector"); 1551 } else if (obj.type === Const.OBJECT_TYPE_ARC) { 1552 attr = Type.copyAttributes(attributes, board.options, "arc"); 1553 } else if (obj.type === Const.OBJECT_TYPE_ANGLE) { 1554 if (!Type.exists(attributes.withLabel)) { 1555 attributes.withLabel = false; 1556 } 1557 attr = Type.copyAttributes(attributes, board.options, "angle"); 1558 } else { 1559 attr = Type.copyAttributes(attributes, board.options, "curve"); 1560 } 1561 attr = Type.copyAttributes(attr, board.options, "curve"); 1562 1563 cu = new JXG.Curve(board, ["x", [], []], attr); 1564 /** 1565 * @class 1566 * @ignore 1567 */ 1568 cu.updateDataArray = function () { 1569 var i, 1570 le = obj.numberPoints; 1571 this.bezierDegree = obj.bezierDegree; 1572 this.dataX = []; 1573 this.dataY = []; 1574 for (i = 0; i < le; i++) { 1575 this.dataX.push(obj.points[i].usrCoords[1]); 1576 this.dataY.push(obj.points[i].usrCoords[2]); 1577 } 1578 return this; 1579 }; 1580 cu.addTransform(parents[1]); 1581 obj.addChild(cu); 1582 cu.setParents([obj]); 1583 cu._transformationSource = obj; 1584 1585 return cu; 1586 } 1587 attr = Type.copyAttributes(attributes, board.options, "curve"); 1588 return new JXG.Curve(board, ["x"].concat(parents), attr); 1589 }; 1590 1591 JXG.registerElement("curve", JXG.createCurve); 1592 1593 /** 1594 * @class This element is used to provide a constructor for functiongraph, 1595 * which is just a wrapper for element {@link Curve} with {@link JXG.Curve#X}() 1596 * set to x. The graph is drawn for x in the interval [a,b]. 1597 * @pseudo 1598 * @name Functiongraph 1599 * @augments JXG.Curve 1600 * @constructor 1601 * @type JXG.Curve 1602 * @param {function_number,function_number,function} f,a_,b_ Parent elements are a function term f(x) describing the function graph. 1603 * <p> 1604 * Further, an optional number or function for the left interval border a, 1605 * and an optional number or function for the right interval border b. 1606 * <p> 1607 * Default values are a=-10 and b=10. 1608 * @see JXG.Curve 1609 * @example 1610 * // Create a function graph for f(x) = 0.5*x*x-2*x 1611 * var graph = board.create('functiongraph', 1612 * [function(x){ return 0.5*x*x-2*x;}, -2, 4] 1613 * ); 1614 * </pre><div class="jxgbox" id="JXGefd432b5-23a3-4846-ac5b-b471e668b437" style="width: 300px; height: 300px;"></div> 1615 * <script type="text/javascript"> 1616 * var alex1_board = JXG.JSXGraph.initBoard('JXGefd432b5-23a3-4846-ac5b-b471e668b437', {boundingbox: [-3, 7, 5, -3], axis: true, showcopyright: false, shownavigation: false}); 1617 * var graph = alex1_board.create('functiongraph', [function(x){ return 0.5*x*x-2*x;}, -2, 4]); 1618 * </script><pre> 1619 * @example 1620 * // Create a function graph for f(x) = 0.5*x*x-2*x with variable interval 1621 * var s = board.create('slider',[[0,4],[3,4],[-2,4,5]]); 1622 * var graph = board.create('functiongraph', 1623 * [function(x){ return 0.5*x*x-2*x;}, 1624 * -2, 1625 * function(){return s.Value();}] 1626 * ); 1627 * </pre><div class="jxgbox" id="JXG4a203a84-bde5-4371-ad56-44619690bb50" style="width: 300px; height: 300px;"></div> 1628 * <script type="text/javascript"> 1629 * var alex2_board = JXG.JSXGraph.initBoard('JXG4a203a84-bde5-4371-ad56-44619690bb50', {boundingbox: [-3, 7, 5, -3], axis: true, showcopyright: false, shownavigation: false}); 1630 * var s = alex2_board.create('slider',[[0,4],[3,4],[-2,4,5]]); 1631 * var graph = alex2_board.create('functiongraph', [function(x){ return 0.5*x*x-2*x;}, -2, function(){return s.Value();}]); 1632 * </script><pre> 1633 */ 1634 JXG.createFunctiongraph = function (board, parents, attributes) { 1635 var attr, 1636 par = ["x", "x"].concat(parents); // variable name and identity function for x-coordinate 1637 // par = ["x", function(x) { return x; }].concat(parents); 1638 1639 attr = Type.copyAttributes(attributes, board.options, "functiongraph"); 1640 attr = Type.copyAttributes(attr, board.options, "curve"); 1641 attr.curvetype = "functiongraph"; 1642 return new JXG.Curve(board, par, attr); 1643 }; 1644 1645 JXG.registerElement("functiongraph", JXG.createFunctiongraph); 1646 JXG.registerElement("plot", JXG.createFunctiongraph); 1647 1648 /** 1649 * @class This element is used to provide a constructor for (natural) cubic spline curves. 1650 * Create a dynamic spline interpolated curve given by sample points p_1 to p_n. 1651 * @pseudo 1652 * @name Spline 1653 * @augments JXG.Curve 1654 * @constructor 1655 * @type JXG.Curve 1656 * @param {JXG.Board} board Reference to the board the spline is drawn on. 1657 * @param {Array} parents Array of points the spline interpolates. This can be 1658 * <ul> 1659 * <li> an array of JSXGraph points</li> 1660 * <li> an array of coordinate pairs</li> 1661 * <li> an array of functions returning coordinate pairs</li> 1662 * <li> an array consisting of an array with x-coordinates and an array of y-coordinates</li> 1663 * </ul> 1664 * All individual entries of coordinates arrays may be numbers or functions returning numbers. 1665 * @param {Object} attributes Define color, width, ... of the spline 1666 * @returns {JXG.Curve} Returns reference to an object of type JXG.Curve. 1667 * @see JXG.Curve 1668 * @example 1669 * 1670 * var p = []; 1671 * p[0] = board.create('point', [-2,2], {size: 4, face: 'o'}); 1672 * p[1] = board.create('point', [0,-1], {size: 4, face: 'o'}); 1673 * p[2] = board.create('point', [2,0], {size: 4, face: 'o'}); 1674 * p[3] = board.create('point', [4,1], {size: 4, face: 'o'}); 1675 * 1676 * var c = board.create('spline', p, {strokeWidth:3}); 1677 * </pre><div id="JXG6c197afc-e482-11e5-b1bf-901b0e1b8723" style="width: 300px; height: 300px;"></div> 1678 * <script type="text/javascript"> 1679 * (function() { 1680 * var board = JXG.JSXGraph.initBoard('JXG6c197afc-e482-11e5-b1bf-901b0e1b8723', 1681 * {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false}); 1682 * 1683 * var p = []; 1684 * p[0] = board.create('point', [-2,2], {size: 4, face: 'o'}); 1685 * p[1] = board.create('point', [0,-1], {size: 4, face: 'o'}); 1686 * p[2] = board.create('point', [2,0], {size: 4, face: 'o'}); 1687 * p[3] = board.create('point', [4,1], {size: 4, face: 'o'}); 1688 * 1689 * var c = board.create('spline', p, {strokeWidth:3}); 1690 * })(); 1691 * 1692 * </script><pre> 1693 * 1694 */ 1695 JXG.createSpline = function (board, parents, attributes) { 1696 var el, funcs, ret; 1697 1698 funcs = function () { 1699 var D, 1700 x = [], 1701 y = []; 1702 1703 return [ 1704 function (t, suspended) { 1705 // Function term 1706 var i, j, c; 1707 1708 if (!suspended) { 1709 x = []; 1710 y = []; 1711 1712 // given as [x[], y[]] 1713 if ( 1714 parents.length === 2 && 1715 Type.isArray(parents[0]) && 1716 Type.isArray(parents[1]) && 1717 parents[0].length === parents[1].length 1718 ) { 1719 for (i = 0; i < parents[0].length; i++) { 1720 if (Type.isFunction(parents[0][i])) { 1721 x.push(parents[0][i]()); 1722 } else { 1723 x.push(parents[0][i]); 1724 } 1725 1726 if (Type.isFunction(parents[1][i])) { 1727 y.push(parents[1][i]()); 1728 } else { 1729 y.push(parents[1][i]); 1730 } 1731 } 1732 } else { 1733 for (i = 0; i < parents.length; i++) { 1734 if (Type.isPoint(parents[i])) { 1735 x.push(parents[i].X()); 1736 y.push(parents[i].Y()); 1737 // given as [[x1,y1], [x2, y2], ...] 1738 } else if (Type.isArray(parents[i]) && parents[i].length === 2) { 1739 for (j = 0; j < parents.length; j++) { 1740 if (Type.isFunction(parents[j][0])) { 1741 x.push(parents[j][0]()); 1742 } else { 1743 x.push(parents[j][0]); 1744 } 1745 1746 if (Type.isFunction(parents[j][1])) { 1747 y.push(parents[j][1]()); 1748 } else { 1749 y.push(parents[j][1]); 1750 } 1751 } 1752 } else if ( 1753 Type.isFunction(parents[i]) && 1754 parents[i]().length === 2 1755 ) { 1756 c = parents[i](); 1757 x.push(c[0]); 1758 y.push(c[1]); 1759 } 1760 } 1761 } 1762 1763 // The array D has only to be calculated when the position of one or more sample points 1764 // changes. Otherwise D is always the same for all points on the spline. 1765 D = Numerics.splineDef(x, y); 1766 } 1767 1768 return Numerics.splineEval(t, x, y, D); 1769 }, 1770 // minX() 1771 function () { 1772 return x[0]; 1773 }, 1774 //maxX() 1775 function () { 1776 return x[x.length - 1]; 1777 } 1778 ]; 1779 }; 1780 1781 attributes = Type.copyAttributes(attributes, board.options, "curve"); 1782 attributes.curvetype = "functiongraph"; 1783 ret = funcs(); 1784 el = new JXG.Curve(board, ["x", "x", ret[0], ret[1], ret[2]], attributes); 1785 el.setParents(parents); 1786 el.elType = "spline"; 1787 1788 return el; 1789 }; 1790 1791 /** 1792 * Register the element type spline at JSXGraph 1793 * @private 1794 */ 1795 JXG.registerElement("spline", JXG.createSpline); 1796 1797 /** 1798 * @class This element is used to provide a constructor for cardinal spline curves. 1799 * Create a dynamic cardinal spline interpolated curve given by sample points p_1 to p_n. 1800 * @pseudo 1801 * @name Cardinalspline 1802 * @augments JXG.Curve 1803 * @constructor 1804 * @type JXG.Curve 1805 * @param {JXG.Board} board Reference to the board the cardinal spline is drawn on. 1806 * @param {Array} parents Array with three entries. 1807 * <p> 1808 * First entry: Array of points the spline interpolates. This can be 1809 * <ul> 1810 * <li> an array of JSXGraph points</li> 1811 * <li> an array of coordinate pairs</li> 1812 * <li> an array of functions returning coordinate pairs</li> 1813 * <li> an array consisting of an array with x-coordinates and an array of y-coordinates</li> 1814 * </ul> 1815 * All individual entries of coordinates arrays may be numbers or functions returning numbers. 1816 * <p> 1817 * Second entry: tau number or function 1818 * <p> 1819 * Third entry: type string containing 'uniform' (default) or 'centripetal'. 1820 * @param {Object} attributes Define color, width, ... of the cardinal spline 1821 * @returns {JXG.Curve} Returns reference to an object of type JXG.Curve. 1822 * @see JXG.Curve 1823 * @example 1824 * //create a cardinal spline out of an array of JXG points with adjustable tension 1825 * //create array of points 1826 * var p1 = board.create('point',[0,0]) 1827 * var p2 = board.create('point',[1,4]) 1828 * var p3 = board.create('point',[4,5]) 1829 * var p4 = board.create('point',[2,3]) 1830 * var p5 = board.create('point',[3,0]) 1831 * var p = [p1,p2,p3,p4,p5] 1832 * 1833 * // tension 1834 * tau = board.create('slider', [[4,3],[9,3],[0.001,0.5,1]], {name:'tau'}); 1835 * c = board.create('curve', JXG.Math.Numerics.CardinalSpline(p, function(){ return tau.Value();}), {strokeWidth:3}); 1836 * </pre><div id="JXG6c197afc-e482-11e5-b2af-901b0e1b8723" style="width: 300px; height: 300px;"></div> 1837 * <script type="text/javascript"> 1838 * (function() { 1839 * var board = JXG.JSXGraph.initBoard('JXG6c197afc-e482-11e5-b2af-901b0e1b8723', 1840 * {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false}); 1841 * 1842 * var p = []; 1843 * p[0] = board.create('point', [-2,2], {size: 4, face: 'o'}); 1844 * p[1] = board.create('point', [0,-1], {size: 4, face: 'o'}); 1845 * p[2] = board.create('point', [2,0], {size: 4, face: 'o'}); 1846 * p[3] = board.create('point', [4,1], {size: 4, face: 'o'}); 1847 * 1848 * var c = board.create('spline', p, {strokeWidth:3}); 1849 * })(); 1850 * 1851 * </script><pre> 1852 */ 1853 JXG.createCardinalSpline = function (board, parents, attributes) { 1854 var el, 1855 getPointLike, 1856 points, 1857 tau, 1858 type, 1859 p, 1860 q, 1861 i, 1862 le, 1863 splineArr, 1864 errStr = "\nPossible parent types: [points:array, tau:number|function, type:string]"; 1865 1866 if (!Type.exists(parents[0]) || !Type.isArray(parents[0])) { 1867 throw new Error( 1868 "JSXGraph: JXG.createCardinalSpline: argument 1 'points' has to be array of points or coordinate pairs" + 1869 errStr 1870 ); 1871 } 1872 if ( 1873 !Type.exists(parents[1]) || 1874 (!Type.isNumber(parents[1]) && !Type.isFunction(parents[1])) 1875 ) { 1876 throw new Error( 1877 "JSXGraph: JXG.createCardinalSpline: argument 2 'tau' has to be number between [0,1] or function'" + 1878 errStr 1879 ); 1880 } 1881 if (!Type.exists(parents[2]) || !Type.isString(parents[2])) { 1882 throw new Error( 1883 "JSXGraph: JXG.createCardinalSpline: argument 3 'type' has to be string 'uniform' or 'centripetal'" + 1884 errStr 1885 ); 1886 } 1887 1888 attributes = Type.copyAttributes(attributes, board.options, "curve"); 1889 attributes = Type.copyAttributes(attributes, board.options, "cardinalspline"); 1890 attributes.curvetype = "parameter"; 1891 1892 p = parents[0]; 1893 q = []; 1894 1895 // Given as [x[], y[]] 1896 if ( 1897 !attributes.isarrayofcoordinates && 1898 p.length === 2 && 1899 Type.isArray(p[0]) && 1900 Type.isArray(p[1]) && 1901 p[0].length === p[1].length 1902 ) { 1903 for (i = 0; i < p[0].length; i++) { 1904 q[i] = []; 1905 if (Type.isFunction(p[0][i])) { 1906 q[i].push(p[0][i]()); 1907 } else { 1908 q[i].push(p[0][i]); 1909 } 1910 1911 if (Type.isFunction(p[1][i])) { 1912 q[i].push(p[1][i]()); 1913 } else { 1914 q[i].push(p[1][i]); 1915 } 1916 } 1917 } else { 1918 // given as [[x0, y0], [x1, y1], point, ...] 1919 for (i = 0; i < p.length; i++) { 1920 if (Type.isString(p[i])) { 1921 q.push(board.select(p[i])); 1922 } else if (Type.isPoint(p[i])) { 1923 q.push(p[i]); 1924 // given as [[x0,y0], [x1, y2], ...] 1925 } else if (Type.isArray(p[i]) && p[i].length === 2) { 1926 q[i] = []; 1927 if (Type.isFunction(p[i][0])) { 1928 q[i].push(p[i][0]()); 1929 } else { 1930 q[i].push(p[i][0]); 1931 } 1932 1933 if (Type.isFunction(p[i][1])) { 1934 q[i].push(p[i][1]()); 1935 } else { 1936 q[i].push(p[i][1]); 1937 } 1938 } else if (Type.isFunction(p[i]) && p[i]().length === 2) { 1939 q.push(parents[i]()); 1940 } 1941 } 1942 } 1943 1944 if (attributes.createpoints === true) { 1945 points = Type.providePoints(board, q, attributes, "cardinalspline", ["points"]); 1946 } else { 1947 points = []; 1948 1949 /** 1950 * @ignore 1951 */ 1952 getPointLike = function (ii) { 1953 return { 1954 X: function () { 1955 return q[ii][0]; 1956 }, 1957 Y: function () { 1958 return q[ii][1]; 1959 }, 1960 Dist: function (p) { 1961 var dx = this.X() - p.X(), 1962 dy = this.Y() - p.Y(); 1963 1964 return Mat.hypot(dx, dy); 1965 } 1966 }; 1967 }; 1968 1969 for (i = 0; i < q.length; i++) { 1970 if (Type.isPoint(q[i])) { 1971 points.push(q[i]); 1972 } else { 1973 points.push(getPointLike(i)); 1974 } 1975 } 1976 } 1977 1978 tau = parents[1]; 1979 type = parents[2]; 1980 1981 splineArr = ["x"].concat(Numerics.CardinalSpline(points, tau, type)); 1982 1983 el = new JXG.Curve(board, splineArr, attributes); 1984 le = points.length; 1985 el.setParents(points); 1986 for (i = 0; i < le; i++) { 1987 p = points[i]; 1988 if (Type.isPoint(p)) { 1989 if (Type.exists(p._is_new)) { 1990 el.addChild(p); 1991 delete p._is_new; 1992 } else { 1993 p.addChild(el); 1994 } 1995 } 1996 } 1997 el.elType = "cardinalspline"; 1998 1999 return el; 2000 }; 2001 2002 /** 2003 * Register the element type cardinalspline at JSXGraph 2004 * @private 2005 */ 2006 JXG.registerElement("cardinalspline", JXG.createCardinalSpline); 2007 2008 /** 2009 * @class This element is used to provide a constructor for metapost spline curves. 2010 * Create a dynamic metapost spline interpolated curve given by sample points p_1 to p_n. 2011 * @pseudo 2012 * @name Metapostspline 2013 * @augments JXG.Curve 2014 * @constructor 2015 * @type JXG.Curve 2016 * @param {JXG.Board} board Reference to the board the metapost spline is drawn on. 2017 * @param {Array} parents Array with two entries. 2018 * <p> 2019 * First entry: Array of points the spline interpolates. This can be 2020 * <ul> 2021 * <li> an array of JSXGraph points</li> 2022 * <li> an object of coordinate pairs</li> 2023 * <li> an array of functions returning coordinate pairs</li> 2024 * <li> an array consisting of an array with x-coordinates and an array of y-coordinates</li> 2025 * </ul> 2026 * All individual entries of coordinates arrays may be numbers or functions returning numbers. 2027 * <p> 2028 * Second entry: JavaScript object containing the control values like tension, direction, curl. 2029 * @param {Object} attributes Define color, width, ... of the metapost spline 2030 * @returns {JXG.Curve} Returns reference to an object of type JXG.Curve. 2031 * @see JXG.Curve 2032 * @example 2033 * var po = [], 2034 * attr = { 2035 * size: 5, 2036 * color: 'red' 2037 * }, 2038 * controls; 2039 * 2040 * var tension = board.create('slider', [[-3, 6], [3, 6], [0, 1, 20]], {name: 'tension'}); 2041 * var curl = board.create('slider', [[-3, 5], [3, 5], [0, 1, 30]], {name: 'curl A, D'}); 2042 * var dir = board.create('slider', [[-3, 4], [3, 4], [-180, 0, 180]], {name: 'direction B'}); 2043 * 2044 * po.push(board.create('point', [-3, -3])); 2045 * po.push(board.create('point', [0, -3])); 2046 * po.push(board.create('point', [4, -5])); 2047 * po.push(board.create('point', [6, -2])); 2048 * 2049 * var controls = { 2050 * tension: function() {return tension.Value(); }, 2051 * direction: { 1: function() {return dir.Value(); } }, 2052 * curl: { 0: function() {return curl.Value(); }, 2053 * 3: function() {return curl.Value(); } 2054 * }, 2055 * isClosed: false 2056 * }; 2057 * 2058 * // Plot a metapost curve 2059 * var cu = board.create('metapostspline', [po, controls], {strokeColor: 'blue', strokeWidth: 2}); 2060 * 2061 * 2062 * </pre><div id="JXGb8c6ffed-7419-41a3-9e55-3754b2327ae9" class="jxgbox" style="width: 300px; height: 300px;"></div> 2063 * <script type="text/javascript"> 2064 * (function() { 2065 * var board = JXG.JSXGraph.initBoard('JXGb8c6ffed-7419-41a3-9e55-3754b2327ae9', 2066 * {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false}); 2067 * var po = [], 2068 * attr = { 2069 * size: 5, 2070 * color: 'red' 2071 * }, 2072 * controls; 2073 * 2074 * var tension = board.create('slider', [[-3, 6], [3, 6], [0, 1, 20]], {name: 'tension'}); 2075 * var curl = board.create('slider', [[-3, 5], [3, 5], [0, 1, 30]], {name: 'curl A, D'}); 2076 * var dir = board.create('slider', [[-3, 4], [3, 4], [-180, 0, 180]], {name: 'direction B'}); 2077 * 2078 * po.push(board.create('point', [-3, -3])); 2079 * po.push(board.create('point', [0, -3])); 2080 * po.push(board.create('point', [4, -5])); 2081 * po.push(board.create('point', [6, -2])); 2082 * 2083 * var controls = { 2084 * tension: function() {return tension.Value(); }, 2085 * direction: { 1: function() {return dir.Value(); } }, 2086 * curl: { 0: function() {return curl.Value(); }, 2087 * 3: function() {return curl.Value(); } 2088 * }, 2089 * isClosed: false 2090 * }; 2091 * 2092 * // Plot a metapost curve 2093 * var cu = board.create('metapostspline', [po, controls], {strokeColor: 'blue', strokeWidth: 2}); 2094 * 2095 * 2096 * })(); 2097 * 2098 * </script><pre> 2099 * 2100 */ 2101 JXG.createMetapostSpline = function (board, parents, attributes) { 2102 var el, 2103 getPointLike, 2104 points, 2105 controls, 2106 p, 2107 q, 2108 i, 2109 le, 2110 errStr = "\nPossible parent types: [points:array, controls:object"; 2111 2112 if (!Type.exists(parents[0]) || !Type.isArray(parents[0])) { 2113 throw new Error( 2114 "JSXGraph: JXG.createMetapostSpline: argument 1 'points' has to be array of points or coordinate pairs" + 2115 errStr 2116 ); 2117 } 2118 if (!Type.exists(parents[1]) || !Type.isObject(parents[1])) { 2119 throw new Error( 2120 "JSXGraph: JXG.createMetapostSpline: argument 2 'controls' has to be a JavaScript object'" + 2121 errStr 2122 ); 2123 } 2124 2125 attributes = Type.copyAttributes(attributes, board.options, "curve"); 2126 attributes = Type.copyAttributes(attributes, board.options, "metapostspline"); 2127 attributes.curvetype = "parameter"; 2128 2129 p = parents[0]; 2130 q = []; 2131 2132 // given as [x[], y[]] 2133 if ( 2134 !attributes.isarrayofcoordinates && 2135 p.length === 2 && 2136 Type.isArray(p[0]) && 2137 Type.isArray(p[1]) && 2138 p[0].length === p[1].length 2139 ) { 2140 for (i = 0; i < p[0].length; i++) { 2141 q[i] = []; 2142 if (Type.isFunction(p[0][i])) { 2143 q[i].push(p[0][i]()); 2144 } else { 2145 q[i].push(p[0][i]); 2146 } 2147 2148 if (Type.isFunction(p[1][i])) { 2149 q[i].push(p[1][i]()); 2150 } else { 2151 q[i].push(p[1][i]); 2152 } 2153 } 2154 } else { 2155 // given as [[x0, y0], [x1, y1], point, ...] 2156 for (i = 0; i < p.length; i++) { 2157 if (Type.isString(p[i])) { 2158 q.push(board.select(p[i])); 2159 } else if (Type.isPoint(p[i])) { 2160 q.push(p[i]); 2161 // given as [[x0,y0], [x1, y2], ...] 2162 } else if (Type.isArray(p[i]) && p[i].length === 2) { 2163 q[i] = []; 2164 if (Type.isFunction(p[i][0])) { 2165 q[i].push(p[i][0]()); 2166 } else { 2167 q[i].push(p[i][0]); 2168 } 2169 2170 if (Type.isFunction(p[i][1])) { 2171 q[i].push(p[i][1]()); 2172 } else { 2173 q[i].push(p[i][1]); 2174 } 2175 } else if (Type.isFunction(p[i]) && p[i]().length === 2) { 2176 q.push(parents[i]()); 2177 } 2178 } 2179 } 2180 2181 if (attributes.createpoints === true) { 2182 points = Type.providePoints(board, q, attributes, 'metapostspline', ['points']); 2183 } else { 2184 points = []; 2185 2186 /** 2187 * @ignore 2188 */ 2189 getPointLike = function (ii) { 2190 return { 2191 X: function () { 2192 return q[ii][0]; 2193 }, 2194 Y: function () { 2195 return q[ii][1]; 2196 } 2197 }; 2198 }; 2199 2200 for (i = 0; i < q.length; i++) { 2201 if (Type.isPoint(q[i])) { 2202 points.push(q[i]); 2203 } else { 2204 points.push(getPointLike); 2205 } 2206 } 2207 } 2208 2209 controls = parents[1]; 2210 2211 el = new JXG.Curve(board, ["t", [], [], 0, p.length - 1], attributes); 2212 /** 2213 * @class 2214 * @ignore 2215 */ 2216 el.updateDataArray = function () { 2217 var res, 2218 i, 2219 len = points.length, 2220 p = []; 2221 2222 for (i = 0; i < len; i++) { 2223 p.push([points[i].X(), points[i].Y()]); 2224 } 2225 2226 res = Metapost.curve(p, controls); 2227 this.dataX = res[0]; 2228 this.dataY = res[1]; 2229 }; 2230 el.bezierDegree = 3; 2231 2232 le = points.length; 2233 el.setParents(points); 2234 for (i = 0; i < le; i++) { 2235 if (Type.isPoint(points[i])) { 2236 points[i].addChild(el); 2237 } 2238 } 2239 el.elType = "metapostspline"; 2240 2241 return el; 2242 }; 2243 2244 JXG.registerElement("metapostspline", JXG.createMetapostSpline); 2245 2246 /** 2247 * @class This element is used to provide a constructor for Riemann sums, which is realized as a special curve. 2248 * The returned element has the method Value() which returns the sum of the areas of the bars. 2249 * <p> 2250 * In case of type "simpson" and "trapezoidal", the horizontal line approximating the function value 2251 * is replaced by a parabola or a secant. IN case of "simpson", 2252 * the parabola is approximated visually by a polygonal chain of fixed step width. 2253 * 2254 * @pseudo 2255 * @name Riemannsum 2256 * @augments JXG.Curve 2257 * @constructor 2258 * @type Curve 2259 * @param {function,array_number,function_string,function_function,number_function,number} f,n,type_,a_,b_ Parent elements of Riemannsum are a 2260 * Either a function term f(x) describing the function graph which is filled by the Riemann bars, or 2261 * an array consisting of two functions and the area between is filled by the Riemann bars. 2262 * <p> 2263 * n determines the number of bars, it is either a fixed number or a function. 2264 * <p> 2265 * type is a string or function returning one of the values: 'left', 'right', 'middle', 'lower', 'upper', 'random', 'simpson', or 'trapezoidal'. 2266 * Default value is 'left'. "simpson" is Simpson's 1/3 rule. 2267 * <p> 2268 * Further parameters are an optional number or function for the left interval border a, 2269 * and an optional number or function for the right interval border b. 2270 * <p> 2271 * Default values are a=-10 and b=10. 2272 * @see JXG.Curve 2273 * @example 2274 * // Create Riemann sums for f(x) = 0.5*x*x-2*x. 2275 * var s = board.create('slider',[[0,4],[3,4],[0,4,10]],{snapWidth:1}); 2276 * var f = function(x) { return 0.5*x*x-2*x; }; 2277 * var r = board.create('riemannsum', 2278 * [f, function(){return s.Value();}, 'upper', -2, 5], 2279 * {fillOpacity:0.4} 2280 * ); 2281 * var g = board.create('functiongraph',[f, -2, 5]); 2282 * var t = board.create('text',[-2,-2, function(){ return 'Sum=' + JXG.toFixed(r.Value(), 4); }]); 2283 * </pre><div class="jxgbox" id="JXG940f40cc-2015-420d-9191-c5d83de988cf" style="width: 300px; height: 300px;"></div> 2284 * <script type="text/javascript"> 2285 * (function(){ 2286 * var board = JXG.JSXGraph.initBoard('JXG940f40cc-2015-420d-9191-c5d83de988cf', {boundingbox: [-3, 7, 5, -3], axis: true, showcopyright: false, shownavigation: false}); 2287 * var f = function(x) { return 0.5*x*x-2*x; }; 2288 * var s = board.create('slider',[[0,4],[3,4],[0,4,10]],{snapWidth:1}); 2289 * var r = board.create('riemannsum', [f, function(){return s.Value();}, 'upper', -2, 5], {fillOpacity:0.4}); 2290 * var g = board.create('functiongraph', [f, -2, 5]); 2291 * var t = board.create('text',[-2,-2, function(){ return 'Sum=' + JXG.toFixed(r.Value(), 4); }]); 2292 * })(); 2293 * </script><pre> 2294 * 2295 * @example 2296 * // Riemann sum between two functions 2297 * var s = board.create('slider',[[0,4],[3,4],[0,4,10]],{snapWidth:1}); 2298 * var g = function(x) { return 0.5*x*x-2*x; }; 2299 * var f = function(x) { return -x*(x-4); }; 2300 * var r = board.create('riemannsum', 2301 * [[g,f], function(){return s.Value();}, 'lower', 0, 4], 2302 * {fillOpacity:0.4} 2303 * ); 2304 * var f = board.create('functiongraph',[f, -2, 5]); 2305 * var g = board.create('functiongraph',[g, -2, 5]); 2306 * var t = board.create('text',[-2,-2, function(){ return 'Sum=' + JXG.toFixed(r.Value(), 4); }]); 2307 * </pre><div class="jxgbox" id="JXGf9a7ba38-b50f-4a32-a873-2f3bf9caee79" style="width: 300px; height: 300px;"></div> 2308 * <script type="text/javascript"> 2309 * (function(){ 2310 * var board = JXG.JSXGraph.initBoard('JXGf9a7ba38-b50f-4a32-a873-2f3bf9caee79', {boundingbox: [-3, 7, 5, -3], axis: true, showcopyright: false, shownavigation: false}); 2311 * var s = board.create('slider',[[0,4],[3,4],[0,4,10]],{snapWidth:1}); 2312 * var g = function(x) { return 0.5*x*x-2*x; }; 2313 * var f = function(x) { return -x*(x-4); }; 2314 * var r = board.create('riemannsum', 2315 * [[g,f], function(){return s.Value();}, 'lower', 0, 4], 2316 * {fillOpacity:0.4} 2317 * ); 2318 * var f = board.create('functiongraph',[f, -2, 5]); 2319 * var g = board.create('functiongraph',[g, -2, 5]); 2320 * var t = board.create('text',[-2,-2, function(){ return 'Sum=' + JXG.toFixed(r.Value(), 4); }]); 2321 * })(); 2322 * </script><pre> 2323 */ 2324 JXG.createRiemannsum = function (board, parents, attributes) { 2325 var n, type, f, par, c, attr; 2326 2327 attr = Type.copyAttributes(attributes, board.options, "riemannsum"); 2328 attr.curvetype = "plot"; 2329 2330 f = parents[0]; 2331 n = Type.createFunction(parents[1], board, ""); 2332 2333 if (!Type.exists(n)) { 2334 throw new Error( 2335 "JSXGraph: JXG.createRiemannsum: argument '2' n has to be number or function." + 2336 "\nPossible parent types: [function,n:number|function,type,start:number|function,end:number|function]" 2337 ); 2338 } 2339 2340 type = Type.createFunction(parents[2], board, ""); 2341 if (!Type.exists(type)) { 2342 throw new Error( 2343 "JSXGraph: JXG.createRiemannsum: argument 3 'type' has to be string or function." + 2344 "\nPossible parent types: [function,n:number|function,type,start:number|function,end:number|function]" 2345 ); 2346 } 2347 2348 par = [[0], [0]].concat(parents.slice(3)); 2349 2350 c = board.create("curve", par, attr); 2351 2352 c.sum = 0.0; 2353 /** 2354 * Returns the value of the Riemann sum, i.e. the sum of the (signed) areas of the rectangles. 2355 * @name Value 2356 * @memberOf Riemannsum.prototype 2357 * @function 2358 * @returns {Number} value of Riemann sum. 2359 */ 2360 c.Value = function () { 2361 return this.sum; 2362 }; 2363 2364 /** 2365 * @class 2366 * @ignore 2367 */ 2368 c.updateDataArray = function () { 2369 var u = Numerics.riemann(f, n(), type(), this.minX(), this.maxX()); 2370 this.dataX = u[0]; 2371 this.dataY = u[1]; 2372 2373 // Update "Riemann sum" 2374 this.sum = u[2]; 2375 }; 2376 2377 c.addParentsFromJCFunctions([n, type]); 2378 2379 return c; 2380 }; 2381 2382 JXG.registerElement("riemannsum", JXG.createRiemannsum); 2383 2384 /** 2385 * @class This element is used to provide a constructor for trace curve (simple locus curve), which is realized as a special curve. 2386 * @pseudo 2387 * @name Tracecurve 2388 * @augments JXG.Curve 2389 * @constructor 2390 * @type Object 2391 * @descript JXG.Curve 2392 * @param {Point} Parent elements of Tracecurve are a 2393 * glider point and a point whose locus is traced. 2394 * @param {point} 2395 * @see JXG.Curve 2396 * @example 2397 * // Create trace curve. 2398 * var c1 = board.create('circle',[[0, 0], [2, 0]]), 2399 * p1 = board.create('point',[-3, 1]), 2400 * g1 = board.create('glider',[2, 1, c1]), 2401 * s1 = board.create('segment',[g1, p1]), 2402 * p2 = board.create('midpoint',[s1]), 2403 * curve = board.create('tracecurve', [g1, p2]); 2404 * 2405 * </pre><div class="jxgbox" id="JXG5749fb7d-04fc-44d2-973e-45c1951e29ad" style="width: 300px; height: 300px;"></div> 2406 * <script type="text/javascript"> 2407 * var tc1_board = JXG.JSXGraph.initBoard('JXG5749fb7d-04fc-44d2-973e-45c1951e29ad', {boundingbox: [-4, 4, 4, -4], axis: false, showcopyright: false, shownavigation: false}); 2408 * var c1 = tc1_board.create('circle',[[0, 0], [2, 0]]), 2409 * p1 = tc1_board.create('point',[-3, 1]), 2410 * g1 = tc1_board.create('glider',[2, 1, c1]), 2411 * s1 = tc1_board.create('segment',[g1, p1]), 2412 * p2 = tc1_board.create('midpoint',[s1]), 2413 * curve = tc1_board.create('tracecurve', [g1, p2]); 2414 * </script><pre> 2415 */ 2416 JXG.createTracecurve = function (board, parents, attributes) { 2417 var c, glider, tracepoint, attr; 2418 2419 if (parents.length !== 2) { 2420 throw new Error( 2421 "JSXGraph: Can't create trace curve with given parent'" + 2422 "\nPossible parent types: [glider, point]" 2423 ); 2424 } 2425 2426 glider = board.select(parents[0]); 2427 tracepoint = board.select(parents[1]); 2428 2429 if (glider.type !== Const.OBJECT_TYPE_GLIDER || !Type.isPoint(tracepoint)) { 2430 throw new Error( 2431 "JSXGraph: Can't create trace curve with parent types '" + 2432 typeof parents[0] + 2433 "' and '" + 2434 typeof parents[1] + 2435 "'." + 2436 "\nPossible parent types: [glider, point]" 2437 ); 2438 } 2439 2440 attr = Type.copyAttributes(attributes, board.options, "tracecurve"); 2441 attr.curvetype = "plot"; 2442 c = board.create("curve", [[0], [0]], attr); 2443 2444 /** 2445 * @class 2446 * @ignore 2447 */ 2448 c.updateDataArray = function () { 2449 var i, step, t, el, pEl, x, y, from, 2450 savetrace, 2451 le = attr.numberpoints, 2452 savePos = glider.position, 2453 slideObj = glider.slideObject, 2454 mi = slideObj.minX(), 2455 ma = slideObj.maxX(); 2456 2457 // set step width 2458 step = (ma - mi) / le; 2459 this.dataX = []; 2460 this.dataY = []; 2461 2462 /* 2463 * For gliders on circles and lines a closed curve is computed. 2464 * For gliders on curves the curve is not closed. 2465 */ 2466 if (slideObj.elementClass !== Const.OBJECT_CLASS_CURVE) { 2467 le++; 2468 } 2469 2470 // Loop over all steps 2471 for (i = 0; i < le; i++) { 2472 t = mi + i * step; 2473 x = slideObj.X(t) / slideObj.Z(t); 2474 y = slideObj.Y(t) / slideObj.Z(t); 2475 2476 // Position the glider 2477 glider.setPositionDirectly(Const.COORDS_BY_USER, [x, y]); 2478 from = false; 2479 2480 // Update all elements from the glider up to the trace element 2481 for (el in this.board.objects) { 2482 if (this.board.objects.hasOwnProperty(el)) { 2483 pEl = this.board.objects[el]; 2484 2485 if (pEl === glider) { 2486 from = true; 2487 } 2488 2489 if (from && pEl.needsRegularUpdate) { 2490 // Save the trace mode of the element 2491 savetrace = pEl.visProp.trace; 2492 pEl.visProp.trace = false; 2493 pEl.needsUpdate = true; 2494 pEl.update(true); 2495 2496 // Restore the trace mode 2497 pEl.visProp.trace = savetrace; 2498 if (pEl === tracepoint) { 2499 break; 2500 } 2501 } 2502 } 2503 } 2504 2505 // Store the position of the trace point 2506 this.dataX[i] = tracepoint.X(); 2507 this.dataY[i] = tracepoint.Y(); 2508 } 2509 2510 // Restore the original position of the glider 2511 glider.position = savePos; 2512 from = false; 2513 2514 // Update all elements from the glider to the trace point 2515 for (el in this.board.objects) { 2516 if (this.board.objects.hasOwnProperty(el)) { 2517 pEl = this.board.objects[el]; 2518 if (pEl === glider) { 2519 from = true; 2520 } 2521 2522 if (from && pEl.needsRegularUpdate) { 2523 savetrace = pEl.visProp.trace; 2524 pEl.visProp.trace = false; 2525 pEl.needsUpdate = true; 2526 pEl.update(true); 2527 pEl.visProp.trace = savetrace; 2528 2529 if (pEl === tracepoint) { 2530 break; 2531 } 2532 } 2533 } 2534 } 2535 }; 2536 2537 return c; 2538 }; 2539 2540 JXG.registerElement("tracecurve", JXG.createTracecurve); 2541 2542 /** 2543 * @class This element is used to provide a constructor for step function, which is realized as a special curve. 2544 * 2545 * In case the data points should be updated after creation time, they can be accessed by curve.xterm and curve.yterm. 2546 * @pseudo 2547 * @name Stepfunction 2548 * @augments JXG.Curve 2549 * @constructor 2550 * @type Curve 2551 * @description JXG.Curve 2552 * @param {Array|Function} Parent1 elements of Stepfunction are two arrays containing the coordinates. 2553 * @param {Array|Function} Parent2 2554 * @see JXG.Curve 2555 * @example 2556 * // Create step function. 2557 var curve = board.create('stepfunction', [[0,1,2,3,4,5], [1,3,0,2,2,1]]); 2558 2559 * </pre><div class="jxgbox" id="JXG32342ec9-ad17-4339-8a97-ff23dc34f51a" style="width: 300px; height: 300px;"></div> 2560 * <script type="text/javascript"> 2561 * var sf1_board = JXG.JSXGraph.initBoard('JXG32342ec9-ad17-4339-8a97-ff23dc34f51a', {boundingbox: [-1, 5, 6, -2], axis: true, showcopyright: false, shownavigation: false}); 2562 * var curve = sf1_board.create('stepfunction', [[0,1,2,3,4,5], [1,3,0,2,2,1]]); 2563 * </script><pre> 2564 */ 2565 JXG.createStepfunction = function (board, parents, attributes) { 2566 var c, attr; 2567 if (parents.length !== 2) { 2568 throw new Error( 2569 "JSXGraph: Can't create step function with given parent'" + 2570 "\nPossible parent types: [array, array|function]" 2571 ); 2572 } 2573 2574 attr = Type.copyAttributes(attributes, board.options, "stepfunction"); 2575 c = board.create("curve", parents, attr); 2576 /** 2577 * @class 2578 * @ignore 2579 */ 2580 c.updateDataArray = function () { 2581 var i, 2582 j = 0, 2583 len = this.xterm.length; 2584 2585 this.dataX = []; 2586 this.dataY = []; 2587 2588 if (len === 0) { 2589 return; 2590 } 2591 2592 this.dataX[j] = this.xterm[0]; 2593 this.dataY[j] = this.yterm[0]; 2594 ++j; 2595 2596 for (i = 1; i < len; ++i) { 2597 this.dataX[j] = this.xterm[i]; 2598 this.dataY[j] = this.dataY[j - 1]; 2599 ++j; 2600 this.dataX[j] = this.xterm[i]; 2601 this.dataY[j] = this.yterm[i]; 2602 ++j; 2603 } 2604 }; 2605 2606 return c; 2607 }; 2608 2609 JXG.registerElement("stepfunction", JXG.createStepfunction); 2610 2611 /** 2612 * @class This element is used to provide a constructor for the graph showing 2613 * the (numerical) derivative of a given curve. 2614 * 2615 * @pseudo 2616 * @name Derivative 2617 * @augments JXG.Curve 2618 * @constructor 2619 * @type JXG.Curve 2620 * @param {JXG.Curve} Parent Curve for which the derivative is generated. 2621 * @see JXG.Curve 2622 * @example 2623 * var cu = board.create('cardinalspline', [[[-3,0], [-1,2], [0,1], [2,0], [3,1]], 0.5, 'centripetal'], {createPoints: false}); 2624 * var d = board.create('derivative', [cu], {dash: 2}); 2625 * 2626 * </pre><div id="JXGb9600738-1656-11e8-8184-901b0e1b8723" class="jxgbox" style="width: 300px; height: 300px;"></div> 2627 * <script type="text/javascript"> 2628 * (function() { 2629 * var board = JXG.JSXGraph.initBoard('JXGb9600738-1656-11e8-8184-901b0e1b8723', 2630 * {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false}); 2631 * var cu = board.create('cardinalspline', [[[-3,0], [-1,2], [0,1], [2,0], [3,1]], 0.5, 'centripetal'], {createPoints: false}); 2632 * var d = board.create('derivative', [cu], {dash: 2}); 2633 * 2634 * })(); 2635 * 2636 * </script><pre> 2637 * 2638 */ 2639 JXG.createDerivative = function (board, parents, attributes) { 2640 var c, curve, dx, dy, attr; 2641 2642 if (parents.length !== 1 && parents[0].class !== Const.OBJECT_CLASS_CURVE) { 2643 throw new Error( 2644 "JSXGraph: Can't create derivative curve with given parent'" + 2645 "\nPossible parent types: [curve]" 2646 ); 2647 } 2648 2649 attr = Type.copyAttributes(attributes, board.options, "curve"); 2650 2651 curve = parents[0]; 2652 dx = Numerics.D(curve.X); 2653 dy = Numerics.D(curve.Y); 2654 2655 c = board.create( 2656 "curve", 2657 [ 2658 function (t) { 2659 return curve.X(t); 2660 }, 2661 function (t) { 2662 return dy(t) / dx(t); 2663 }, 2664 curve.minX(), 2665 curve.maxX() 2666 ], 2667 attr 2668 ); 2669 2670 c.setParents(curve); 2671 2672 return c; 2673 }; 2674 2675 JXG.registerElement("derivative", JXG.createDerivative); 2676 2677 /** 2678 * @class Intersection of two closed path elements. The elements may be of type curve, circle, polygon, inequality. 2679 * If one element is a curve, it has to be closed. 2680 * The resulting element is of type curve. 2681 * @pseudo 2682 * @name CurveIntersection 2683 * @param {JXG.Curve|JXG.Polygon|JXG.Circle} curve1 First element which is intersected 2684 * @param {JXG.Curve|JXG.Polygon|JXG.Circle} curve2 Second element which is intersected 2685 * @augments JXG.Curve 2686 * @constructor 2687 * @type JXG.Curve 2688 * 2689 * @example 2690 * var f = board.create('functiongraph', ['cos(x)']); 2691 * var ineq = board.create('inequality', [f], {inverse: true, fillOpacity: 0.1}); 2692 * var circ = board.create('circle', [[0,0], 4]); 2693 * var clip = board.create('curveintersection', [ineq, circ], {fillColor: 'yellow', fillOpacity: 0.6}); 2694 * 2695 * </pre><div id="JXGe2948257-8835-4276-9164-8acccb48e8d4" class="jxgbox" style="width: 300px; height: 300px;"></div> 2696 * <script type="text/javascript"> 2697 * (function() { 2698 * var board = JXG.JSXGraph.initBoard('JXGe2948257-8835-4276-9164-8acccb48e8d4', 2699 * {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false}); 2700 * var f = board.create('functiongraph', ['cos(x)']); 2701 * var ineq = board.create('inequality', [f], {inverse: true, fillOpacity: 0.1}); 2702 * var circ = board.create('circle', [[0,0], 4]); 2703 * var clip = board.create('curveintersection', [ineq, circ], {fillColor: 'yellow', fillOpacity: 0.6}); 2704 * 2705 * })(); 2706 * 2707 * </script><pre> 2708 * 2709 */ 2710 JXG.createCurveIntersection = function (board, parents, attributes) { 2711 var c; 2712 2713 if (parents.length !== 2) { 2714 throw new Error( 2715 "JSXGraph: Can't create curve intersection with given parent'" + 2716 "\nPossible parent types: [array, array|function]" 2717 ); 2718 } 2719 2720 c = board.create("curve", [[], []], attributes); 2721 /** 2722 * @class 2723 * @ignore 2724 */ 2725 c.updateDataArray = function () { 2726 var a = Clip.intersection(parents[0], parents[1], this.board); 2727 this.dataX = a[0]; 2728 this.dataY = a[1]; 2729 }; 2730 return c; 2731 }; 2732 2733 /** 2734 * @class Union of two closed path elements. The elements may be of type curve, circle, polygon, inequality. 2735 * If one element is a curve, it has to be closed. 2736 * The resulting element is of type curve. 2737 * @pseudo 2738 * @name CurveUnion 2739 * @param {JXG.Curve|JXG.Polygon|JXG.Circle} curve1 First element defining the union 2740 * @param {JXG.Curve|JXG.Polygon|JXG.Circle} curve2 Second element defining the union 2741 * @augments JXG.Curve 2742 * @constructor 2743 * @type JXG.Curve 2744 * 2745 * @example 2746 * var f = board.create('functiongraph', ['cos(x)']); 2747 * var ineq = board.create('inequality', [f], {inverse: true, fillOpacity: 0.1}); 2748 * var circ = board.create('circle', [[0,0], 4]); 2749 * var clip = board.create('curveunion', [ineq, circ], {fillColor: 'yellow', fillOpacity: 0.6}); 2750 * 2751 * </pre><div id="JXGe2948257-8835-4276-9164-8acccb48e8d4" class="jxgbox" style="width: 300px; height: 300px;"></div> 2752 * <script type="text/javascript"> 2753 * (function() { 2754 * var board = JXG.JSXGraph.initBoard('JXGe2948257-8835-4276-9164-8acccb48e8d4', 2755 * {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false}); 2756 * var f = board.create('functiongraph', ['cos(x)']); 2757 * var ineq = board.create('inequality', [f], {inverse: true, fillOpacity: 0.1}); 2758 * var circ = board.create('circle', [[0,0], 4]); 2759 * var clip = board.create('curveunion', [ineq, circ], {fillColor: 'yellow', fillOpacity: 0.6}); 2760 * 2761 * })(); 2762 * 2763 * </script><pre> 2764 * 2765 */ 2766 JXG.createCurveUnion = function (board, parents, attributes) { 2767 var c; 2768 2769 if (parents.length !== 2) { 2770 throw new Error( 2771 "JSXGraph: Can't create curve union with given parent'" + 2772 "\nPossible parent types: [array, array|function]" 2773 ); 2774 } 2775 2776 c = board.create("curve", [[], []], attributes); 2777 /** 2778 * @class 2779 * @ignore 2780 */ 2781 c.updateDataArray = function () { 2782 var a = Clip.union(parents[0], parents[1], this.board); 2783 this.dataX = a[0]; 2784 this.dataY = a[1]; 2785 }; 2786 return c; 2787 }; 2788 2789 /** 2790 * @class Difference of two closed path elements. The elements may be of type curve, circle, polygon, inequality. 2791 * If one element is a curve, it has to be closed. 2792 * The resulting element is of type curve. 2793 * @pseudo 2794 * @name CurveDifference 2795 * @param {JXG.Curve|JXG.Polygon|JXG.Circle} curve1 First element from which the second element is "subtracted" 2796 * @param {JXG.Curve|JXG.Polygon|JXG.Circle} curve2 Second element which is subtracted from the first element 2797 * @augments JXG.Curve 2798 * @constructor 2799 * @type JXG.Curve 2800 * 2801 * @example 2802 * var f = board.create('functiongraph', ['cos(x)']); 2803 * var ineq = board.create('inequality', [f], {inverse: true, fillOpacity: 0.1}); 2804 * var circ = board.create('circle', [[0,0], 4]); 2805 * var clip = board.create('curvedifference', [ineq, circ], {fillColor: 'yellow', fillOpacity: 0.6}); 2806 * 2807 * </pre><div id="JXGe2948257-8835-4276-9164-8acccb48e8d4" class="jxgbox" style="width: 300px; height: 300px;"></div> 2808 * <script type="text/javascript"> 2809 * (function() { 2810 * var board = JXG.JSXGraph.initBoard('JXGe2948257-8835-4276-9164-8acccb48e8d4', 2811 * {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false}); 2812 * var f = board.create('functiongraph', ['cos(x)']); 2813 * var ineq = board.create('inequality', [f], {inverse: true, fillOpacity: 0.1}); 2814 * var circ = board.create('circle', [[0,0], 4]); 2815 * var clip = board.create('curvedifference', [ineq, circ], {fillColor: 'yellow', fillOpacity: 0.6}); 2816 * 2817 * })(); 2818 * 2819 * </script><pre> 2820 * 2821 */ 2822 JXG.createCurveDifference = function (board, parents, attributes) { 2823 var c; 2824 2825 if (parents.length !== 2) { 2826 throw new Error( 2827 "JSXGraph: Can't create curve difference with given parent'" + 2828 "\nPossible parent types: [array, array|function]" 2829 ); 2830 } 2831 2832 c = board.create("curve", [[], []], attributes); 2833 /** 2834 * @class 2835 * @ignore 2836 */ 2837 c.updateDataArray = function () { 2838 var a = Clip.difference(parents[0], parents[1], this.board); 2839 this.dataX = a[0]; 2840 this.dataY = a[1]; 2841 }; 2842 return c; 2843 }; 2844 2845 JXG.registerElement("curvedifference", JXG.createCurveDifference); 2846 JXG.registerElement("curveintersection", JXG.createCurveIntersection); 2847 JXG.registerElement("curveunion", JXG.createCurveUnion); 2848 2849 // /** 2850 // * @class Concat of two path elements, in general neither is a closed path. The parent elements have to be curves, too. 2851 // * The resulting element is of type curve. The curve points are simply concatenated. 2852 // * @pseudo 2853 // * @name CurveConcat 2854 // * @param {JXG.Curve} curve1 First curve element. 2855 // * @param {JXG.Curve} curve2 Second curve element. 2856 // * @augments JXG.Curve 2857 // * @constructor 2858 // * @type JXG.Curve 2859 // */ 2860 // JXG.createCurveConcat = function (board, parents, attributes) { 2861 // var c; 2862 2863 // if (parents.length !== 2) { 2864 // throw new Error( 2865 // "JSXGraph: Can't create curve difference with given parent'" + 2866 // "\nPossible parent types: [array, array|function]" 2867 // ); 2868 // } 2869 2870 // c = board.create("curve", [[], []], attributes); 2871 // /** 2872 // * @class 2873 // * @ignore 2874 // */ 2875 // c.updateCurve = function () { 2876 // this.points = parents[0].points.concat( 2877 // [new JXG.Coords(Const.COORDS_BY_USER, [NaN, NaN], this.board)] 2878 // ).concat(parents[1].points); 2879 // this.numberPoints = this.points.length; 2880 // return this; 2881 // }; 2882 2883 // return c; 2884 // }; 2885 2886 // JXG.registerElement("curveconcat", JXG.createCurveConcat); 2887 2888 /** 2889 * @class Box plot curve. The direction of the box plot can be either vertical or horizontal which 2890 * is controlled by the attribute "dir". 2891 * @pseudo 2892 * @name Boxplot 2893 * @param {Array} quantiles Array containing at least five quantiles. The elements can be of type number, function or string. 2894 * @param {Number|Function} axis Axis position of the box plot 2895 * @param {Number|Function} width Width of the rectangle part of the box plot. The width of the first and 4th quantile 2896 * is relative to this width and can be controlled by the attribute "smallWidth". 2897 * @augments JXG.Curve 2898 * @constructor 2899 * @type JXG.Curve 2900 * 2901 * @example 2902 * var Q = [ -1, 2, 3, 3.5, 5 ]; 2903 * 2904 * var b = board.create('boxplot', [Q, 2, 4], {strokeWidth: 3}); 2905 * 2906 * </pre><div id="JXG13eb23a1-a641-41a2-be11-8e03e400a947" class="jxgbox" style="width: 300px; height: 300px;"></div> 2907 * <script type="text/javascript"> 2908 * (function() { 2909 * var board = JXG.JSXGraph.initBoard('JXG13eb23a1-a641-41a2-be11-8e03e400a947', 2910 * {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false}); 2911 * var Q = [ -1, 2, 3, 3.5, 5 ]; 2912 * var b = board.create('boxplot', [Q, 2, 4], {strokeWidth: 3}); 2913 * 2914 * })(); 2915 * 2916 * </script><pre> 2917 * 2918 * @example 2919 * var Q = [ -1, 2, 3, 3.5, 5 ]; 2920 * var b = board.create('boxplot', [Q, 3, 4], {dir: 'horizontal', smallWidth: 0.25, color:'red'}); 2921 * 2922 * </pre><div id="JXG0deb9cb2-84bc-470d-a6db-8be9a5694813" class="jxgbox" style="width: 300px; height: 300px;"></div> 2923 * <script type="text/javascript"> 2924 * (function() { 2925 * var board = JXG.JSXGraph.initBoard('JXG0deb9cb2-84bc-470d-a6db-8be9a5694813', 2926 * {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false}); 2927 * var Q = [ -1, 2, 3, 3.5, 5 ]; 2928 * var b = board.create('boxplot', [Q, 3, 4], {dir: 'horizontal', smallWidth: 0.25, color:'red'}); 2929 * 2930 * })(); 2931 * 2932 * </script><pre> 2933 * 2934 * @example 2935 * 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]; 2936 * var Q = []; 2937 * 2938 * Q[0] = JXG.Math.Statistics.min(data); 2939 * Q = Q.concat(JXG.Math.Statistics.percentile(data, [25, 50, 75])); 2940 * Q[4] = JXG.Math.Statistics.max(data); 2941 * 2942 * var b = board.create('boxplot', [Q, 0, 3]); 2943 * 2944 * </pre><div id="JXGef079e76-ae99-41e4-af29-1d07d83bf85a" class="jxgbox" style="width: 300px; height: 300px;"></div> 2945 * <script type="text/javascript"> 2946 * (function() { 2947 * var board = JXG.JSXGraph.initBoard('JXGef079e76-ae99-41e4-af29-1d07d83bf85a', 2948 * {boundingbox: [-5,90,5,30], axis: true, showcopyright: false, shownavigation: false}); 2949 * 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]; 2950 * var Q = []; 2951 * 2952 * Q[0] = JXG.Math.Statistics.min(data); 2953 * Q = Q.concat(JXG.Math.Statistics.percentile(data, [25, 50, 75])); 2954 * Q[4] = JXG.Math.Statistics.max(data); 2955 * 2956 * var b = board.create('boxplot', [Q, 0, 3]); 2957 * 2958 * })(); 2959 * 2960 * </script><pre> 2961 * 2962 * @example 2963 * var mi = board.create('glider', [0, -1, board.defaultAxes.y]); 2964 * var ma = board.create('glider', [0, 5, board.defaultAxes.y]); 2965 * var Q = [function() { return mi.Y(); }, 2, 3, 3.5, function() { return ma.Y(); }]; 2966 * 2967 * var b = board.create('boxplot', [Q, 0, 2]); 2968 * 2969 * </pre><div id="JXG3b3225da-52f0-42fe-8396-be9016bf289b" class="jxgbox" style="width: 300px; height: 300px;"></div> 2970 * <script type="text/javascript"> 2971 * (function() { 2972 * var board = JXG.JSXGraph.initBoard('JXG3b3225da-52f0-42fe-8396-be9016bf289b', 2973 * {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false}); 2974 * var mi = board.create('glider', [0, -1, board.defaultAxes.y]); 2975 * var ma = board.create('glider', [0, 5, board.defaultAxes.y]); 2976 * var Q = [function() { return mi.Y(); }, 2, 3, 3.5, function() { return ma.Y(); }]; 2977 * 2978 * var b = board.create('boxplot', [Q, 0, 2]); 2979 * 2980 * })(); 2981 * 2982 * </script><pre> 2983 * 2984 */ 2985 JXG.createBoxPlot = function (board, parents, attributes) { 2986 var box, i, len, 2987 attr = Type.copyAttributes(attributes, board.options, "boxplot"); 2988 2989 if (parents.length !== 3) { 2990 throw new Error( 2991 "JSXGraph: Can't create box plot with given parent'" + 2992 "\nPossible parent types: [array, number|function, number|function] containing quantiles, axis, width" 2993 ); 2994 } 2995 if (parents[0].length < 5) { 2996 throw new Error( 2997 "JSXGraph: Can't create box plot with given parent[0]'" + 2998 "\nparent[0] has to contain at least 5 quantiles." 2999 ); 3000 } 3001 box = board.create("curve", [[], []], attr); 3002 3003 len = parents[0].length; // Quantiles 3004 box.Q = []; 3005 for (i = 0; i < len; i++) { 3006 box.Q[i] = Type.createFunction(parents[0][i], board); 3007 } 3008 box.x = Type.createFunction(parents[1], board); 3009 box.w = Type.createFunction(parents[2], board); 3010 3011 /** 3012 * @class 3013 * @ignore 3014 */ 3015 box.updateDataArray = function () { 3016 var v1, v2, l1, l2, r1, r2, w2, dir, x; 3017 3018 w2 = Type.evaluate(this.visProp.smallwidth); 3019 dir = Type.evaluate(this.visProp.dir); 3020 x = this.x(); 3021 l1 = x - this.w() * 0.5; 3022 l2 = x - this.w() * 0.5 * w2; 3023 r1 = x + this.w() * 0.5; 3024 r2 = x + this.w() * 0.5 * w2; 3025 v1 = [x, l2, r2, x, x, l1, l1, r1, r1, x, NaN, l1, r1, NaN, x, x, l2, r2, x]; 3026 v2 = [ 3027 this.Q[0](), 3028 this.Q[0](), 3029 this.Q[0](), 3030 this.Q[0](), 3031 this.Q[1](), 3032 this.Q[1](), 3033 this.Q[3](), 3034 this.Q[3](), 3035 this.Q[1](), 3036 this.Q[1](), 3037 NaN, 3038 this.Q[2](), 3039 this.Q[2](), 3040 NaN, 3041 this.Q[3](), 3042 this.Q[4](), 3043 this.Q[4](), 3044 this.Q[4](), 3045 this.Q[4]() 3046 ]; 3047 if (dir === "vertical") { 3048 this.dataX = v1; 3049 this.dataY = v2; 3050 } else { 3051 this.dataX = v2; 3052 this.dataY = v1; 3053 } 3054 }; 3055 3056 box.addParentsFromJCFunctions([box.Q, box.x, box.w]); 3057 3058 return box; 3059 }; 3060 3061 JXG.registerElement("boxplot", JXG.createBoxPlot); 3062 3063 /** 3064 * 3065 * @class 3066 * From <a href="https://en.wikipedia.org/wiki/Implicit_curve">Wikipedia</a>: 3067 * "An implicit curve is a plane curve defined by an implicit equation 3068 * relating two coordinate variables, commonly <i>x</i> and <i>y</i>. 3069 * For example, the unit circle is defined by the implicit equation 3070 * x<sup>2</sup> + y<sup>2</sup> = 1. 3071 * In general, every implicit curve is defined by an equation of the form 3072 * <i>f(x, y) = 0</i> 3073 * for some function <i>f</i> of two variables." 3074 * <p> 3075 * The partial derivatives for <i>f</i> are optional. If not given, numerical 3076 * derivatives are used instead. This is good enough for most practical use cases. 3077 * But if supplied, both partial derivatives must be supplied. 3078 * <p> 3079 * The most effective attributes to tinker with if the implicit curve algorithm fails are 3080 * {@link ImplicitCurve#resolution_outer}, 3081 * {@link ImplicitCurve#resolution_inner}, 3082 * {@link ImplicitCurve#alpha_0}, 3083 * {@link ImplicitCurve#h_initial}, 3084 * {@link ImplicitCurve#h_max}, and 3085 * {@link ImplicitCurve#qdt_box}. 3086 * 3087 * @pseudo 3088 * @name ImplicitCurve 3089 * @param {Function|String} f Function of two variables for the left side of the equation <i>f(x,y)=0</i>. 3090 * If f is supplied as string, it has to use the variables 'x' and 'y'. 3091 * @param {Function|String} [dfx=null] Optional partial derivative in respect to the first variable 3092 * If dfx is supplied as string, it has to use the variables 'x' and 'y'. 3093 * @param {Function|String} [dfy=null] Optional partial derivative in respect to the second variable 3094 * If dfy is supplied as string, it has to use the variables 'x' and 'y'. 3095 * @augments JXG.Curve 3096 * @constructor 3097 * @type JXG.Curve 3098 * 3099 * @example 3100 * var f, c; 3101 * f = (x, y) => 1 / 16 * x ** 2 + y ** 2 - 1; 3102 * c = board.create('implicitcurve', [f], { 3103 * strokeWidth: 3, 3104 * strokeColor: JXG.palette.red, 3105 * strokeOpacity: 0.8 3106 * }); 3107 * 3108 * </pre><div id="JXGa6e86701-1a82-48d0-b007-3a3d32075076" class="jxgbox" style="width: 300px; height: 300px;"></div> 3109 * <script type="text/javascript"> 3110 * (function() { 3111 * var board = JXG.JSXGraph.initBoard('JXGa6e86701-1a82-48d0-b007-3a3d32075076', 3112 * {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false}); 3113 * var f, c; 3114 * f = (x, y) => 1 / 16 * x ** 2 + y ** 2 - 1; 3115 * c = board.create('implicitcurve', [f], { 3116 * strokeWidth: 3, 3117 * strokeColor: JXG.palette.red, 3118 * strokeOpacity: 0.8 3119 * }); 3120 * 3121 * })(); 3122 * 3123 * </script><pre> 3124 * 3125 * @example 3126 * var a, c, f; 3127 * a = board.create('slider', [[-3, 6], [3, 6], [-3, 1, 3]], { 3128 * name: 'a', stepWidth: 0.1 3129 * }); 3130 * f = (x, y) => x ** 2 - 2 * x * y - 2 * x + (a.Value() + 1) * y ** 2 + (4 * a.Value() + 2) * y + 4 * a.Value() - 3; 3131 * c = board.create('implicitcurve', [f], { 3132 * strokeWidth: 3, 3133 * strokeColor: JXG.palette.red, 3134 * strokeOpacity: 0.8, 3135 * resolution_outer: 20, 3136 * resolution_inner: 20 3137 * }); 3138 * 3139 * </pre><div id="JXG0b133a54-9509-4a65-9722-9c5145e23b40" class="jxgbox" style="width: 300px; height: 300px;"></div> 3140 * <script type="text/javascript"> 3141 * (function() { 3142 * var board = JXG.JSXGraph.initBoard('JXG0b133a54-9509-4a65-9722-9c5145e23b40', 3143 * {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false}); 3144 * var a, c, f; 3145 * a = board.create('slider', [[-3, 6], [3, 6], [-3, 1, 3]], { 3146 * name: 'a', stepWidth: 0.1 3147 * }); 3148 * f = (x, y) => x ** 2 - 2 * x * y - 2 * x + (a.Value() + 1) * y ** 2 + (4 * a.Value() + 2) * y + 4 * a.Value() - 3; 3149 * c = board.create('implicitcurve', [f], { 3150 * strokeWidth: 3, 3151 * strokeColor: JXG.palette.red, 3152 * strokeOpacity: 0.8, 3153 * resolution_outer: 20, 3154 * resolution_inner: 20 3155 * }); 3156 * 3157 * })(); 3158 * 3159 * </script><pre> 3160 * 3161 * @example 3162 * var c = board.create('implicitcurve', ['abs(x * y) - 3'], { 3163 * strokeWidth: 3, 3164 * strokeColor: JXG.palette.red, 3165 * strokeOpacity: 0.8 3166 * }); 3167 * 3168 * </pre><div id="JXG02802981-0abb-446b-86ea-ee588f02ed1a" class="jxgbox" style="width: 300px; height: 300px;"></div> 3169 * <script type="text/javascript"> 3170 * (function() { 3171 * var board = JXG.JSXGraph.initBoard('JXG02802981-0abb-446b-86ea-ee588f02ed1a', 3172 * {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false}); 3173 * var c = board.create('implicitcurve', ['abs(x * y) - 3'], { 3174 * strokeWidth: 3, 3175 * strokeColor: JXG.palette.red, 3176 * strokeOpacity: 0.8 3177 * }); 3178 * 3179 * })(); 3180 * 3181 * </script><pre> 3182 * 3183 */ 3184 JXG.createImplicitCurve = function(board, parents, attributes) { 3185 var c, attr; 3186 if (parents.length !== 1 && parents.length !== 3) { 3187 throw new Error( 3188 "JSXGraph: Can't create curve implicitCurve with given parent'" + 3189 "\nPossible parent types: [f] or [f, dfx, dfy]" + 3190 "\nwith functions f, dfx, dfy" 3191 ); 3192 } 3193 3194 attr = Type.copyAttributes(attributes, board.options, "implicitcurve"); 3195 c = board.create("curve", [[], []], attr); 3196 3197 /** 3198 * Function of two variables for the left side of the equation <i>f(x,y)=0</i>. 3199 * 3200 * @name f 3201 * @memberOf ImplicitCurve.prototype 3202 * @function 3203 * @returns {Number} 3204 */ 3205 c.f = Type.createFunction(parents[0], board, 'x, y'); 3206 3207 /** 3208 * Partial derivative in the first variable of 3209 * the left side of the equation <i>f(x,y)=0</i>. 3210 * If null, then numerical derivative is used. 3211 * 3212 * @name dfx 3213 * @memberOf ImplicitCurve.prototype 3214 * @function 3215 * @returns {Number} 3216 */ 3217 c.dfx = Type.createFunction(parents[1], board, 'x, y'); 3218 3219 /** 3220 * Partial derivative in the second variable of 3221 * the left side of the equation <i>f(x,y)=0</i>. 3222 * If null, then numerical derivative is used. 3223 * 3224 * @name dfy 3225 * @memberOf ImplicitCurve.prototype 3226 * @function 3227 * @returns {Number} 3228 */ 3229 c.dfy = Type.createFunction(parents[2], board, 'x, y'); 3230 3231 /** 3232 * @class 3233 * @ignore 3234 */ 3235 c.updateDataArray = function () { 3236 var bbox = this.board.getBoundingBox(), 3237 ip, cfg, 3238 ret = [], 3239 mgn = Type.evaluate(this.visProp.margin); 3240 3241 bbox[0] -= mgn; 3242 bbox[1] += mgn; 3243 bbox[2] += mgn; 3244 bbox[3] -= mgn; 3245 3246 cfg = { 3247 resolution_out: Math.max(0.01, Type.evaluate(this.visProp.resolution_outer)), 3248 resolution_in: Math.max(0.01, Type.evaluate(this.visProp.resolution_inner)), 3249 max_steps: Type.evaluate(this.visProp.max_steps), 3250 alpha_0: Type.evaluate(this.visProp.alpha_0), 3251 tol_u0: Type.evaluate(this.visProp.tol_u0), 3252 tol_newton: Type.evaluate(this.visProp.tol_newton), 3253 tol_cusp: Type.evaluate(this.visProp.tol_cusp), 3254 tol_progress: Type.evaluate(this.visProp.tol_progress), 3255 qdt_box: Type.evaluate(this.visProp.qdt_box), 3256 kappa_0: Type.evaluate(this.visProp.kappa_0), 3257 delta_0: Type.evaluate(this.visProp.delta_0), 3258 h_initial: Type.evaluate(this.visProp.h_initial), 3259 h_critical: Type.evaluate(this.visProp.h_critical), 3260 h_max: Type.evaluate(this.visProp.h_max), 3261 loop_dist: Type.evaluate(this.visProp.loop_dist), 3262 loop_dir: Type.evaluate(this.visProp.loop_dir), 3263 loop_detection: Type.evaluate(this.visProp.loop_detection), 3264 unitX: this.board.unitX, 3265 unitY: this.board.unitY 3266 }; 3267 this.dataX = []; 3268 this.dataY = []; 3269 3270 // console.time("implicit plot"); 3271 ip = new ImplicitPlot(bbox, cfg, this.f, this.dfx, this.dfy); 3272 this.qdt = ip.qdt; 3273 3274 ret = ip.plot(); 3275 // console.timeEnd("implicit plot"); 3276 3277 this.dataX = ret[0]; 3278 this.dataY = ret[1]; 3279 }; 3280 3281 c.elType = 'implicitcurve'; 3282 3283 return c; 3284 }; 3285 3286 JXG.registerElement("implicitcurve", JXG.createImplicitCurve); 3287 3288 3289 export default JXG.Curve; 3290 3291 // export default { 3292 // Curve: JXG.Curve, 3293 // createCardinalSpline: JXG.createCardinalSpline, 3294 // createCurve: JXG.createCurve, 3295 // createCurveDifference: JXG.createCurveDifference, 3296 // createCurveIntersection: JXG.createCurveIntersection, 3297 // createCurveUnion: JXG.createCurveUnion, 3298 // createDerivative: JXG.createDerivative, 3299 // createFunctiongraph: JXG.createFunctiongraph, 3300 // createMetapostSpline: JXG.createMetapostSpline, 3301 // createPlot: JXG.createFunctiongraph, 3302 // createSpline: JXG.createSpline, 3303 // createRiemannsum: JXG.createRiemannsum, 3304 // createStepfunction: JXG.createStepfunction, 3305 // createTracecurve: JXG.createTracecurve 3306 // }; 3307 3308 // const Curve = JXG.Curve; 3309 // export { Curve as default, Curve}; 3310