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             /**
187              * @ignore
188              */
189             this.F = function(x, y) { return [f0(x, y), f1(x, y)]; };
190         } else {
191             this.F = Type.createFunction(func, el.board, varnames);
192         }
193         return this;
194     };
195 
196     el.setF(parents[0], 'x, y');
197     el.xData = parents[1];
198     el.yData = parents[2];
199 
200     el.updateDataArray = function() {
201         var x, y, i, j,
202             scale = Type.evaluate(this.visProp.scale),
203             start_x = Type.evaluate(this.xData[0]),
204             steps_x = Type.evaluate(this.xData[1]),
205             end_x = Type.evaluate(this.xData[2]),
206             delta_x = (end_x - start_x) / steps_x,
207 
208             start_y = Type.evaluate(this.yData[0]),
209             steps_y = Type.evaluate(this.yData[1]),
210             end_y = Type.evaluate(this.yData[2]),
211             delta_y = (end_y - start_y) / steps_y,
212             dx, dy, d, theta, phi,
213 
214             showArrow = Type.evaluate(this.visProp.arrowhead.enabled),
215             leg, leg_x, leg_y, alpha;
216 
217 
218         if (showArrow) {
219             // Arrow head style
220             leg = Type.evaluate(this.visProp.arrowhead.size),
221             leg_x = leg / board.unitX,
222             leg_y = leg / board.unitY,
223             alpha = Type.evaluate(this.visProp.arrowhead.angle);
224         }
225 
226         this.dataX = [];
227         this.dataY = [];
228 
229         for (i = 0, x = start_x; i <= steps_x; x += delta_x, i++) {
230             for (j = 0, y = start_y; j <= steps_y; y += delta_y, j++) {
231                 d = this.F(x, y);
232                 dx = d[0] * scale;
233                 dy = d[1] * scale;
234 
235                 this.dataX.push(x);
236                 this.dataY.push(y);
237                 this.dataX.push(x + dx);
238                 this.dataY.push(y + dy);
239 
240                 if (showArrow && Math.abs(dx) + Math.abs(dy) > 0.0) {
241                     // Arrow head
242                     theta = Math.atan2(dy, dx);
243                     phi = theta + alpha;
244                     this.dataX.push(x + dx - Math.cos(phi) * leg_x);
245                     this.dataY.push(y + dy - Math.sin(phi) * leg_y);
246                     this.dataX.push(x + dx);
247                     this.dataY.push(y + dy);
248                     phi = theta - alpha;
249                     this.dataX.push(x + dx - Math.cos(phi) * leg_x);
250                     this.dataY.push(y + dy - Math.sin(phi) * leg_y);
251                 }
252 
253                 this.dataX.push(NaN);
254                 this.dataY.push(NaN);
255             }
256         }
257     };
258 
259     el.methodMap = Type.deepCopy(el.methodMap, {
260         setF: "setF"
261     });
262 
263     return el;
264 };
265 
266 JXG.registerElement("vectorfield", JXG.createVectorField);
267 
268 /**
269  * @class Slope field.
270  * <p>
271  * Plot a slope field given by a function f(x, y) returning a number.
272  *
273  * @pseudo
274  * @name Slopefield
275  * @augments Vectorfield
276  * @constructor
277  * @type JXG.Curve
278  * @throws {Error} If the element cannot be constructed with the given parent objects an exception is thrown.
279  * Parameter options:
280  * @param {Function|String} F Function f(x, y) returning a number.
281  * @param {Array} xData Array of length 3 containing start value for x, number of steps, end value of x. The slope field will contain
282  * (number of steps) + 1 vectors in direction of x.
283  * @param {Array} yData Array of length 3 containing start value for y, number of steps, end value of y. The slope field will contain
284  * (number of steps) + 1 vectors in direction of y.
285  * @example
286  * var field = board.create('slopefield', [
287  *     (x, y) => x * x - x - 2,
288  *     [-6, 25, 6], // Horizontal mesh
289  *     [-5, 20, 5]  // Vertical mesh
290  * ]);
291  *
292  * </pre><div id="JXG8a2ee562-eea1-4ce0-91ca-46b71fc7543d" class="jxgbox" style="width: 500px; height: 500px;"></div>
293  * <script type="text/javascript">
294  *     (function() {
295  *         var board = JXG.JSXGraph.initBoard('JXG8a2ee562-eea1-4ce0-91ca-46b71fc7543d',
296  *             {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false});
297  *     var field = board.create('slopefield', [
298  *         (x, y) => x * x - x - 2,
299  *         [-6, 25, 6], [-5, 20, 5]
300  *     ]);
301  *
302  *     })();
303  *
304  * </script><pre>
305  *
306  * @example
307  * // Slider to control length of vectors
308  * var s = board.create('slider', [[-3, 7], [3, 7], [0, 0.33, 1]], {name: 'length'});
309  * // Slider to control number of steps
310  * var stepsize = board.create('slider', [[-3, 6], [3, 6], [1, 20, 100]], {name: 'steps', snapWidth: 1});
311  *
312  * var field = board.create('slopefield', [
313  *     (x, y) => x * x - y * y,
314  *     [-6, () => stepsize.Value(), 6],
315  *     [-5, () => stepsize.Value(), 5]],
316  *     {
317  *         strokeWidth: 1.5,
318  *         highlightStrokeWidth: 0.5,
319  *         highlightStrokeColor: JXG.palette.blue,
320  *
321  *         scale: () => s.Value(),
322  *
323  *         arrowHead: {
324  *             enabled: false,
325  *             size: 8,
326  *             angle: Math.PI / 16
327  *         }
328  *     });
329  *
330  * </pre><div id="JXG1ec9e4d7-6094-4d2b-b72f-4efddd514f55" class="jxgbox" style="width: 500px; height: 500px;"></div>
331  * <script type="text/javascript">
332  *     (function() {
333  *         var board = JXG.JSXGraph.initBoard('JXG1ec9e4d7-6094-4d2b-b72f-4efddd514f55',
334  *             {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false});
335  *     // Slider to control length of vectors
336  *     var s = board.create('slider', [[-3, 7], [3, 7], [0, 0.33, 1]], {name: 'length'});
337  *     // Slider to control number of steps
338  *     var stepsize = board.create('slider', [[-3, 6], [3, 6], [1, 20, 100]], {name: 'steps', snapWidth: 1});
339  *
340  *     var field = board.create('slopefield', [
341  *         (x, y) => x * x - y * y,
342  *         [-6, () => stepsize.Value(), 6],
343  *         [-5, () => stepsize.Value(), 5]],
344  *         {
345  *             strokeWidth: 1.5,
346  *             highlightStrokeWidth: 0.5,
347  *             highlightStrokeColor: JXG.palette.blue,
348  *
349  *             scale: () => s.Value(),
350  *
351  *             arrowHead: {
352  *                 enabled: false,
353  *                 size: 8,
354  *                 angle: Math.PI / 16
355  *             }
356  *         });
357  *
358  *     })();
359  *
360  * </script><pre>
361  *
362  */
363 JXG.createSlopeField = function(board, parents, attributes) {
364     var el, f, attr;
365 
366     if (!(parents.length >= 3 &&
367         (Type.isFunction(parents[0]) || Type.isString(parents[0])) &&
368         (Type.isArray(parents[1]) && parents[1].length === 3) &&
369         (Type.isArray(parents[2]) && parents[2].length === 3)
370     )) {
371         throw new Error(
372             "JSXGraph: Can't create slope field with parent types " +
373                 "'" + typeof parents[0] + "', " +
374                 "'" + typeof parents[1] + "', " +
375                 "'" + typeof parents[2] + "'."
376         );
377     }
378 
379     f = Type.createFunction(parents[0], board, 'x, y');
380     parents[0] = function(x, y) {
381         var z = f(x, y),
382             nrm = Math.sqrt(1 + z * z);
383         return [1 / nrm, z / nrm];
384     };
385     attr = Type.copyAttributes(attributes, board.options, 'slopefield');
386     el = board.create('vectorfield', parents, attr);
387     el.elType = 'slopefield';
388 
389     /**
390      * Set the defining functions of slope field.
391      * @memberOf Slopefield.prototype
392      * @name setF
393      * @function
394      * @param {Function} func Function f(x, y) returning a number.
395      * @returns {Object} Reference to the slope field object.
396      *
397      * @example
398      * field.setF((x, y) => x * x + y * y);
399      * board.update();
400      *
401      */
402     el.setF = function(func, varnames) {
403         var f = Type.createFunction(func, el.board, varnames);
404 
405         /**
406          * @ignore
407          */
408         this.F = function(x, y) {
409             var z = f(x, y),
410                 nrm = Math.sqrt(1 + z * z);
411             return [1 / nrm, z / nrm];
412         }
413     };
414 
415     el.methodMap = Type.deepCopy(el.methodMap, {
416         setF: "setF"
417     });
418 
419     return el;
420 };
421 
422 JXG.registerElement("slopefield", JXG.createSlopeField);
423