1 /* 2 Copyright 2008-2023 3 Matthias Ehmann, 4 Carsten Miller, 5 Alfred Wassermann 6 7 This file is part of JSXGraph. 8 9 JSXGraph is free software dual licensed under the GNU LGPL or MIT License. 10 11 You can redistribute it and/or modify it under the terms of the 12 13 * GNU Lesser General Public License as published by 14 the Free Software Foundation, either version 3 of the License, or 15 (at your option) any later version 16 OR 17 * MIT License: https://github.com/jsxgraph/jsxgraph/blob/master/LICENSE.MIT 18 19 JSXGraph is distributed in the hope that it will be useful, 20 but WITHOUT ANY WARRANTY; without even the implied warranty of 21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 GNU Lesser General Public License for more details. 23 24 You should have received a copy of the GNU Lesser General Public License and 25 the MIT License along with JSXGraph. If not, see <https://www.gnu.org/licenses/> 26 and <https://opensource.org/licenses/MIT/>. 27 */ 28 29 /*global JXG: true, define: true*/ 30 /*jslint nomen: true, plusplus: true*/ 31 32 /** 33 * @fileoverview Implementation of vector fields and slope fields. 34 */ 35 36 import JXG from "../jxg"; 37 import Type from "../utils/type"; 38 39 /** 40 * @class Vector field. 41 * <p> 42 * Plot a vector field either given by two functions f1(x, y) and f2(x,y) or by a function f(x, y) returning an array of size 2. 43 * 44 * @pseudo 45 * @name Vectorfield 46 * @augments JXG.Curve 47 * @constructor 48 * @type JXG.Curve 49 * @throws {Error} If the element cannot be constructed with the given parent objects an exception is thrown. 50 * Parameter options: 51 * @param {Array|Function|String} F Either an array containing two functions f1(x, y) and f2(x, y) or function f(x, y) returning an array of length 2. 52 * @param {Array} xData Array of length 3 containing start value for x, number of steps, end value of x. The vector field will contain 53 * (number of steps) + 1 vectors in direction of x. 54 * @param {Array} yData Array of length 3 containing start value for y, number of steps, end value of y. The vector field will contain 55 * (number of steps) + 1 vectors in direction of y. 56 * 57 * @example 58 * // Defining functions 59 * var fx = (x, y) => Math.sin(y); 60 * var fy = (x, y) => Math.cos(x); 61 * 62 * var field = board.create('vectorfield', [ 63 * [fx, fy], // Defining function 64 * [-6, 25, 6], // Horizontal mesh 65 * [-5, 20, 5], // Vertical mesh 66 * ]); 67 * 68 * </pre><div id="JXGa2040e30-48ea-47d4-9840-bd24cd49150b" class="jxgbox" style="width: 500px; height: 500px;"></div> 69 * <script type="text/javascript"> 70 * (function() { 71 * var board = JXG.JSXGraph.initBoard('JXGa2040e30-48ea-47d4-9840-bd24cd49150b', 72 * {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false}); 73 * // Defining functions 74 * var fx = (x, y) => Math.sin(y); 75 * var fy = (x, y) => Math.cos(x); 76 * 77 * var field = board.create('vectorfield', [ 78 * [fx, fy], // Defining function 79 * [-6, 25, 6], // Horizontal mesh 80 * [-5, 20, 5], // Vertical mesh 81 * ]); 82 * 83 * })(); 84 * 85 * </script><pre> 86 * 87 * @example 88 * // Slider to control length of vectors 89 * var s = board.create('slider', [[-3, 7], [3, 7], [0, 0.33, 1]], {name: 'length'}); 90 * // Slider to control number of steps 91 * var stepsize = board.create('slider', [[-3, 6], [3, 6], [1, 20, 100]], {name: 'steps', snapWidth: 1}); 92 * 93 * // Defining functions 94 * var fx = (x, y) => 0.2 * y; 95 * var fy = (x, y) => 0.2 * (Math.cos(x) - 2) * Math.sin(x); 96 * 97 * var field = board.create('vectorfield', [ 98 * [fx, fy], // Defining function 99 * [-6, () => stepsize.Value(), 6], // Horizontal mesh 100 * [-5, () => stepsize.Value(), 5], // Vertical mesh 101 * ], { 102 * highlightStrokeColor: JXG.palette.blue, // Make highlighting invisible 103 * 104 * scale: () => s.Value(), // Scaling of vectors 105 * 106 * arrowHead: { 107 * enabled: true, 108 * size: 8, // Pixel length of arrow head 109 * angle: Math.PI / 16 110 * } 111 * }); 112 * 113 * </pre><div id="JXG9196337e-66f0-4d09-8065-11d88c4ff140" class="jxgbox" style="width: 500px; height: 500px;"></div> 114 * <script type="text/javascript"> 115 * (function() { 116 * var board = JXG.JSXGraph.initBoard('JXG9196337e-66f0-4d09-8065-11d88c4ff140', 117 * {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false}); 118 * // Slider to control length of vectors 119 * var s = board.create('slider', [[-3, 7], [3, 7], [0, 0.33, 1]], {name: 'length'}); 120 * // Slider to control number of steps 121 * var stepsize = board.create('slider', [[-3, 6], [3, 6], [1, 20, 100]], {name: 'steps', snapWidth: 1}); 122 * 123 * // Defining functions 124 * var fx = (x, y) => 0.2 * y; 125 * var fy = (x, y) => 0.2 * (Math.cos(x) - 2) * Math.sin(x); 126 * 127 * var field = board.create('vectorfield', [ 128 * [fx, fy], // Defining function 129 * [-6, () => stepsize.Value(), 6], // Horizontal mesh 130 * [-5, () => stepsize.Value(), 5], // Vertical mesh 131 * ], { 132 * highlightStrokeColor: JXG.palette.blue, // Make highlighting invisible 133 * 134 * scale: () => s.Value(), // Scaling of vectors 135 * 136 * arrowHead: { 137 * enabled: true, 138 * size: 8, // Pixel length of arrow head 139 * angle: Math.PI / 16 140 * } 141 * }); 142 * 143 * })(); 144 * 145 * </script><pre> 146 * 147 */ 148 JXG.createVectorField = function(board, parents, attributes) { 149 var el, attr; 150 151 if (!(parents.length >= 3 && 152 (Type.isArray(parents[0]) || Type.isFunction(parents[0]) || Type.isString(parents[0])) && 153 (Type.isArray(parents[1]) && parents[1].length === 3) && 154 (Type.isArray(parents[2]) && parents[2].length === 3) 155 )) { 156 throw new Error( 157 "JSXGraph: Can't create vector field with parent types " + 158 "'" + typeof parents[0] + "', " + 159 "'" + typeof parents[1] + "', " + 160 "'" + typeof parents[2] + "'." 161 ); 162 } 163 164 attr = Type.copyAttributes(attributes, board.options, 'vectorfield'); 165 el = board.create('curve', [[], []], attr); 166 el.elType = 'vectorfield'; 167 168 /** 169 * Set the defining functions of vector field. 170 * @memberOf Vectorfield.prototype 171 * @name setF 172 * @function 173 * @param {Array|Function} func Either an array containing two functions f1(x, y) and f2(x, y) or function f(x, y) returning an array of length 2. 174 * @returns {Object} Reference to the vector field object. 175 * 176 * @example 177 * field.setF([(x, y) => Math.sin(y), (x, y) => Math.cos(x)]); 178 * board.update(); 179 * 180 */ 181 el.setF = function(func, varnames) { 182 var f0, f1; 183 if (Type.isArray(func)) { 184 f0 = Type.createFunction(func[0], this.board, varnames); 185 f1 = Type.createFunction(func[1], this.board, varnames); 186 this.F = function(x, y) { return [f0(x, y), f1(x, y)]; }; 187 } else { 188 this.F = Type.createFunction(func, el.board, varnames); 189 } 190 return this; 191 }; 192 193 el.setF(parents[0], 'x, y'); 194 el.xData = parents[1]; 195 el.yData = parents[2]; 196 197 el.updateDataArray = function() { 198 var x, y, i, j, 199 scale = Type.evaluate(this.visProp.scale), 200 start_x = Type.evaluate(this.xData[0]), 201 steps_x = Type.evaluate(this.xData[1]), 202 end_x = Type.evaluate(this.xData[2]), 203 delta_x = (end_x - start_x) / steps_x, 204 205 start_y = Type.evaluate(this.yData[0]), 206 steps_y = Type.evaluate(this.yData[1]), 207 end_y = Type.evaluate(this.yData[2]), 208 delta_y = (end_y - start_y) / steps_y, 209 dx, dy, d, theta, phi, 210 211 showArrow = Type.evaluate(this.visProp.arrowhead.enabled), 212 leg, leg_x, leg_y, alpha; 213 214 215 if (showArrow) { 216 // Arrow head style 217 leg = Type.evaluate(this.visProp.arrowhead.size), 218 leg_x = leg / board.unitX, 219 leg_y = leg / board.unitY, 220 alpha = Type.evaluate(this.visProp.arrowhead.angle); 221 } 222 223 this.dataX = []; 224 this.dataY = []; 225 226 for (i = 0, x = start_x; i <= steps_x; x += delta_x, i++) { 227 for (j = 0, y = start_y; j <= steps_y; y += delta_y, j++) { 228 d = this.F(x, y); 229 dx = d[0] * scale; 230 dy = d[1] * scale; 231 232 this.dataX.push(x); 233 this.dataY.push(y); 234 this.dataX.push(x + dx); 235 this.dataY.push(y + dy); 236 237 if (showArrow && Math.abs(dx) + Math.abs(dy) > 0.0) { 238 // Arrow head 239 theta = Math.atan2(dy, dx); 240 phi = theta + alpha; 241 this.dataX.push(x + dx - Math.cos(phi) * leg_x); 242 this.dataY.push(y + dy - Math.sin(phi) * leg_y); 243 this.dataX.push(x + dx); 244 this.dataY.push(y + dy); 245 phi = theta - alpha; 246 this.dataX.push(x + dx - Math.cos(phi) * leg_x); 247 this.dataY.push(y + dy - Math.sin(phi) * leg_y); 248 } 249 250 this.dataX.push(NaN); 251 this.dataY.push(NaN); 252 } 253 } 254 }; 255 256 el.methodMap = Type.deepCopy(el.methodMap, { 257 setF: "setF" 258 }); 259 260 return el; 261 }; 262 263 JXG.registerElement("vectorfield", JXG.createVectorField); 264 265 /** 266 * @class Slope field. 267 * <p> 268 * Plot a slope field given by a function f(x, y) returning a number. 269 * 270 * @pseudo 271 * @name Slopefield 272 * @augments Vectorfield 273 * @constructor 274 * @type JXG.Curve 275 * @throws {Error} If the element cannot be constructed with the given parent objects an exception is thrown. 276 * Parameter options: 277 * @param {Function|String} F Function f(x, y) returning a number. 278 * @param {Array} xData Array of length 3 containing start value for x, number of steps, end value of x. The slope field will contain 279 * (number of steps) + 1 vectors in direction of x. 280 * @param {Array} yData Array of length 3 containing start value for y, number of steps, end value of y. The slope field will contain 281 * (number of steps) + 1 vectors in direction of y. 282 * @example 283 * var field = board.create('slopefield', [ 284 * (x, y) => x * x - x - 2, 285 * [-6, 25, 6], // Horizontal mesh 286 * [-5, 20, 5] // Vertical mesh 287 * ]); 288 * 289 * </pre><div id="JXG8a2ee562-eea1-4ce0-91ca-46b71fc7543d" class="jxgbox" style="width: 500px; height: 500px;"></div> 290 * <script type="text/javascript"> 291 * (function() { 292 * var board = JXG.JSXGraph.initBoard('JXG8a2ee562-eea1-4ce0-91ca-46b71fc7543d', 293 * {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false}); 294 * var field = board.create('slopefield', [ 295 * (x, y) => x * x - x - 2, 296 * [-6, 25, 6], [-5, 20, 5] 297 * ]); 298 * 299 * })(); 300 * 301 * </script><pre> 302 * 303 * @example 304 * // Slider to control length of vectors 305 * var s = board.create('slider', [[-3, 7], [3, 7], [0, 0.33, 1]], {name: 'length'}); 306 * // Slider to control number of steps 307 * var stepsize = board.create('slider', [[-3, 6], [3, 6], [1, 20, 100]], {name: 'steps', snapWidth: 1}); 308 * 309 * var field = board.create('slopefield', [ 310 * (x, y) => x * x - y * y, 311 * [-6, () => stepsize.Value(), 6], 312 * [-5, () => stepsize.Value(), 5]], 313 * { 314 * strokeWidth: 1.5, 315 * highlightStrokeWidth: 0.5, 316 * highlightStrokeColor: JXG.palette.blue, 317 * 318 * scale: () => s.Value(), 319 * 320 * arrowHead: { 321 * enabled: false, 322 * size: 8, 323 * angle: Math.PI / 16 324 * } 325 * }); 326 * 327 * </pre><div id="JXG1ec9e4d7-6094-4d2b-b72f-4efddd514f55" class="jxgbox" style="width: 500px; height: 500px;"></div> 328 * <script type="text/javascript"> 329 * (function() { 330 * var board = JXG.JSXGraph.initBoard('JXG1ec9e4d7-6094-4d2b-b72f-4efddd514f55', 331 * {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false}); 332 * // Slider to control length of vectors 333 * var s = board.create('slider', [[-3, 7], [3, 7], [0, 0.33, 1]], {name: 'length'}); 334 * // Slider to control number of steps 335 * var stepsize = board.create('slider', [[-3, 6], [3, 6], [1, 20, 100]], {name: 'steps', snapWidth: 1}); 336 * 337 * var field = board.create('slopefield', [ 338 * (x, y) => x * x - y * y, 339 * [-6, () => stepsize.Value(), 6], 340 * [-5, () => stepsize.Value(), 5]], 341 * { 342 * strokeWidth: 1.5, 343 * highlightStrokeWidth: 0.5, 344 * highlightStrokeColor: JXG.palette.blue, 345 * 346 * scale: () => s.Value(), 347 * 348 * arrowHead: { 349 * enabled: false, 350 * size: 8, 351 * angle: Math.PI / 16 352 * } 353 * }); 354 * 355 * })(); 356 * 357 * </script><pre> 358 * 359 */ 360 JXG.createSlopeField = function(board, parents, attributes) { 361 var el, f, attr; 362 363 if (!(parents.length >= 3 && 364 (Type.isFunction(parents[0]) || Type.isString(parents[0])) && 365 (Type.isArray(parents[1]) && parents[1].length === 3) && 366 (Type.isArray(parents[2]) && parents[2].length === 3) 367 )) { 368 throw new Error( 369 "JSXGraph: Can't create slope field with parent types " + 370 "'" + typeof parents[0] + "', " + 371 "'" + typeof parents[1] + "', " + 372 "'" + typeof parents[2] + "'." 373 ); 374 } 375 376 f = Type.createFunction(parents[0], board, 'x, y'); 377 parents[0] = function(x, y) { 378 var z = f(x, y), 379 nrm = Math.sqrt(1 + z * z); 380 return [1 / nrm, z / nrm]; 381 }; 382 attr = Type.copyAttributes(attributes, board.options, 'slopefield'); 383 el = board.create('vectorfield', parents, attr); 384 el.elType = 'slopefield'; 385 386 /** 387 * Set the defining functions of slope field. 388 * @memberOf Slopefield.prototype 389 * @name setF 390 * @function 391 * @param {Function} func Function f(x, y) returning a number. 392 * @returns {Object} Reference to the slope field object. 393 * 394 * @example 395 * field.setF((x, y) => x * x + y * y); 396 * board.update(); 397 * 398 */ 399 el.setF = function(func, varnames) { 400 var f = Type.createFunction(func, el.board, varnames); 401 402 this.F = function(x, y) { 403 var z = f(x, y), 404 nrm = Math.sqrt(1 + z * z); 405 return [1 / nrm, z / nrm]; 406 } 407 }; 408 409 el.methodMap = Type.deepCopy(el.methodMap, { 410 setF: "setF" 411 }); 412 413 return el; 414 }; 415 416 JXG.registerElement("slopefield", JXG.createSlopeField); 417