1 /*
  2     Copyright 2008-2024
  3         Matthias Ehmann,
  4         Michael Gerhaeuser,
  5         Carsten Miller,
  6         Bianca Valentin,
  7         Alfred Wassermann,
  8         Peter Wilfahrt
  9 
 10     This file is part of JSXGraph.
 11 
 12     JSXGraph is free software dual licensed under the GNU LGPL or MIT License.
 13 
 14     You can redistribute it and/or modify it under the terms of the
 15 
 16       * GNU Lesser General Public License as published by
 17         the Free Software Foundation, either version 3 of the License, or
 18         (at your option) any later version
 19       OR
 20       * MIT License: https://github.com/jsxgraph/jsxgraph/blob/master/LICENSE.MIT
 21 
 22     JSXGraph is distributed in the hope that it will be useful,
 23     but WITHOUT ANY WARRANTY; without even the implied warranty of
 24     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 25     GNU Lesser General Public License for more details.
 26 
 27     You should have received a copy of the GNU Lesser General Public License and
 28     the MIT License along with JSXGraph. If not, see <https://www.gnu.org/licenses/>
 29     and <https://opensource.org/licenses/MIT/>.
 30  */
 31 /*
 32     Some functionalities in this file were developed as part of a software project
 33     with students. We would like to thank all contributors for their help:
 34 
 35     Winter semester 2023/2024:
 36         Matti Kirchbach
 37  */
 38 
 39 /*global JXG: true, define: true*/
 40 /*jslint nomen: true, plusplus: true*/
 41 
 42 /**
 43  * @fileoverview The geometry object Line is defined in this file. Line stores all
 44  * style and functional properties that are required to draw and move a line on
 45  * a board.
 46  */
 47 
 48 import JXG from "../jxg.js";
 49 import Mat from "../math/math.js";
 50 import Geometry from "../math/geometry.js";
 51 import Numerics from "../math/numerics.js";
 52 import Statistics from "../math/statistics.js";
 53 import Const from "./constants.js";
 54 import Coords from "./coords.js";
 55 import GeometryElement from "./element.js";
 56 import Type from "../utils/type.js";
 57 
 58 /**
 59  * The Line class is a basic class for all kind of line objects, e.g. line, arrow, and axis. It is usually defined by two points and can
 60  * be intersected with some other geometry elements.
 61  * @class Creates a new basic line object. Do not use this constructor to create a line.
 62  * Use {@link JXG.Board#create} with
 63  * type {@link Line}, {@link Arrow}, or {@link Axis} instead.
 64  * @constructor
 65  * @augments JXG.GeometryElement
 66  * @param {String|JXG.Board} board The board the new line is drawn on.
 67  * @param {Point} p1 Startpoint of the line.
 68  * @param {Point} p2 Endpoint of the line.
 69  * @param {Object} attributes Javascript object containing attributes like name, id and colors.
 70  */
 71 JXG.Line = function (board, p1, p2, attributes) {
 72     this.constructor(board, attributes, Const.OBJECT_TYPE_LINE, Const.OBJECT_CLASS_LINE);
 73 
 74     /**
 75      * Startpoint of the line. You really should not set this field directly as it may break JSXGraph's
 76      * update system so your construction won't be updated properly.
 77      * @type JXG.Point
 78      */
 79     this.point1 = this.board.select(p1);
 80 
 81     /**
 82      * Endpoint of the line. Just like {@link JXG.Line.point1} you shouldn't write this field directly.
 83      * @type JXG.Point
 84      */
 85     this.point2 = this.board.select(p2);
 86 
 87     /**
 88      * Array of ticks storing all the ticks on this line. Do not set this field directly and use
 89      * {@link JXG.Line#addTicks} and {@link JXG.Line#removeTicks} to add and remove ticks to and from the line.
 90      * @type Array
 91      * @see JXG.Ticks
 92      */
 93     this.ticks = [];
 94 
 95     /**
 96      * Reference of the ticks created automatically when constructing an axis.
 97      * @type JXG.Ticks
 98      * @see JXG.Ticks
 99      */
100     this.defaultTicks = null;
101 
102     /**
103      * If the line is the border of a polygon, the polygon object is stored, otherwise null.
104      * @type JXG.Polygon
105      * @default null
106      * @private
107      */
108     this.parentPolygon = null;
109 
110     /* Register line at board */
111     this.id = this.board.setId(this, "L");
112     this.board.renderer.drawLine(this);
113     this.board.finalizeAdding(this);
114 
115     this.elType = "line";
116 
117     /* Add line as child to defining points */
118     if (this.point1._is_new) {
119         this.addChild(this.point1);
120         delete this.point1._is_new;
121     } else {
122         this.point1.addChild(this);
123     }
124     if (this.point2._is_new) {
125         this.addChild(this.point2);
126         delete this.point2._is_new;
127     } else {
128         this.point2.addChild(this);
129     }
130 
131     this.inherits.push(this.point1, this.point2);
132 
133     this.updateStdform(); // This is needed in the following situation:
134     // * the line is defined by three coordinates
135     // * and it will have a glider
136     // * and board.suspendUpdate() has been called.
137 
138     // create Label
139     this.createLabel();
140 
141     this.methodMap = JXG.deepCopy(this.methodMap, {
142         point1: "point1",
143         point2: "point2",
144         getSlope: "Slope",
145         Slope: "Slope",
146         Direction: "Direction",
147         getRise: "getRise",
148         Rise: "getRise",
149         getYIntersect: "getRise",
150         YIntersect: "getRise",
151         getAngle: "getAngle",
152         Angle: "getAngle",
153         L: "L",
154         length: "L",
155         setFixedLength: "setFixedLength",
156         setStraight: "setStraight"
157     });
158 };
159 
160 JXG.Line.prototype = new GeometryElement();
161 
162 JXG.extend(
163     JXG.Line.prototype,
164     /** @lends JXG.Line.prototype */ {
165         /**
166          * Checks whether (x,y) is near the line.
167          * @param {Number} x Coordinate in x direction, screen coordinates.
168          * @param {Number} y Coordinate in y direction, screen coordinates.
169          * @returns {Boolean} True if (x,y) is near the line, False otherwise.
170          */
171         hasPoint: function (x, y) {
172             // Compute the stdform of the line in screen coordinates.
173             var c = [],
174                 v = [1, x, y],
175                 s, vnew, p1c, p2c, d, pos, i, prec, type,
176                 sw = this.evalVisProp('strokewidth');
177 
178             if (Type.isObject(this.evalVisProp('precision'))) {
179                 type = this.board._inputDevice;
180                 prec = this.evalVisProp('precision.' + type);
181             } else {
182                 // 'inherit'
183                 prec = this.board.options.precision.hasPoint;
184             }
185             prec += sw * 0.5;
186 
187             c[0] =
188                 this.stdform[0] -
189                 (this.stdform[1] * this.board.origin.scrCoords[1]) / this.board.unitX +
190                 (this.stdform[2] * this.board.origin.scrCoords[2]) / this.board.unitY;
191             c[1] = this.stdform[1] / this.board.unitX;
192             c[2] = this.stdform[2] / -this.board.unitY;
193 
194             s = Geometry.distPointLine(v, c);
195             if (isNaN(s) || s > prec) {
196                 return false;
197             }
198 
199             if (
200                 this.evalVisProp('straightfirst') &&
201                 this.evalVisProp('straightlast')
202             ) {
203                 return true;
204             }
205 
206             // If the line is a ray or segment we have to check if the projected point is between P1 and P2.
207             p1c = this.point1.coords;
208             p2c = this.point2.coords;
209 
210             // Project the point orthogonally onto the line
211             vnew = [0, c[1], c[2]];
212             // Orthogonal line to c through v
213             vnew = Mat.crossProduct(vnew, v);
214             // Intersect orthogonal line with line
215             vnew = Mat.crossProduct(vnew, c);
216 
217             // Normalize the projected point
218             vnew[1] /= vnew[0];
219             vnew[2] /= vnew[0];
220             vnew[0] = 1;
221 
222             vnew = new Coords(Const.COORDS_BY_SCREEN, vnew.slice(1), this.board).usrCoords;
223             d = p1c.distance(Const.COORDS_BY_USER, p2c);
224             p1c = p1c.usrCoords.slice(0);
225             p2c = p2c.usrCoords.slice(0);
226 
227             // The defining points are identical
228             if (d < Mat.eps) {
229                 pos = 0;
230             } else {
231                 /*
232                  * Handle the cases, where one of the defining points is an ideal point.
233                  * d is set to something close to infinity, namely 1/eps.
234                  * The ideal point is (temporarily) replaced by a finite point which has
235                  * distance d from the other point.
236                  * This is accomplished by extracting the x- and y-coordinates (x,y)=:v of the ideal point.
237                  * v determines the direction of the line. v is normalized, i.e. set to length 1 by dividing through its length.
238                  * Finally, the new point is the sum of the other point and v*d.
239                  *
240                  */
241 
242                 // At least one point is an ideal point
243                 if (d === Number.POSITIVE_INFINITY) {
244                     d = 1 / Mat.eps;
245 
246                     // The second point is an ideal point
247                     if (Math.abs(p2c[0]) < Mat.eps) {
248                         d /= Geometry.distance([0, 0, 0], p2c);
249                         p2c = [1, p1c[1] + p2c[1] * d, p1c[2] + p2c[2] * d];
250                         // The first point is an ideal point
251                     } else {
252                         d /= Geometry.distance([0, 0, 0], p1c);
253                         p1c = [1, p2c[1] + p1c[1] * d, p2c[2] + p1c[2] * d];
254                     }
255                 }
256                 i = 1;
257                 d = p2c[i] - p1c[i];
258 
259                 if (Math.abs(d) < Mat.eps) {
260                     i = 2;
261                     d = p2c[i] - p1c[i];
262                 }
263                 pos = (vnew[i] - p1c[i]) / d;
264             }
265 
266             if (!this.evalVisProp('straightfirst') && pos < 0) {
267                 return false;
268             }
269 
270             return !(!this.evalVisProp('straightlast') && pos > 1);
271         },
272 
273         // documented in base/element
274         update: function () {
275             var funps;
276 
277             if (!this.needsUpdate) {
278                 return this;
279             }
280 
281             if (this.constrained) {
282                 if (Type.isFunction(this.funps)) {
283                     funps = this.funps();
284                     if (funps && funps.length && funps.length === 2) {
285                         this.point1 = funps[0];
286                         this.point2 = funps[1];
287                     }
288                 } else {
289                     if (Type.isFunction(this.funp1)) {
290                         funps = this.funp1();
291                         if (Type.isPoint(funps)) {
292                             this.point1 = funps;
293                         } else if (funps && funps.length && funps.length === 2) {
294                             this.point1.setPositionDirectly(Const.COORDS_BY_USER, funps);
295                         }
296                     }
297 
298                     if (Type.isFunction(this.funp2)) {
299                         funps = this.funp2();
300                         if (Type.isPoint(funps)) {
301                             this.point2 = funps;
302                         } else if (funps && funps.length && funps.length === 2) {
303                             this.point2.setPositionDirectly(Const.COORDS_BY_USER, funps);
304                         }
305                     }
306                 }
307             }
308 
309             this.updateSegmentFixedLength();
310             this.updateStdform();
311 
312             if (this.evalVisProp('trace')) {
313                 this.cloneToBackground(true);
314             }
315 
316             return this;
317         },
318 
319         /**
320          * Update segments with fixed length and at least one movable point.
321          * @private
322          */
323         updateSegmentFixedLength: function () {
324             var d, d_new, d1, d2, drag1, drag2, x, y;
325 
326             if (!this.hasFixedLength) {
327                 return this;
328             }
329 
330             // Compute the actual length of the segment
331             d = this.point1.Dist(this.point2);
332             // Determine the length the segment ought to have
333             d_new = (this.evalVisProp('nonnegativeonly')) ?
334                 Math.max(0.0, this.fixedLength()) :
335                 Math.abs(this.fixedLength());
336 
337             // Distances between the two points and their respective
338             // position before the update
339             d1 = this.fixedLengthOldCoords[0].distance(
340                 Const.COORDS_BY_USER,
341                 this.point1.coords
342             );
343             d2 = this.fixedLengthOldCoords[1].distance(
344                 Const.COORDS_BY_USER,
345                 this.point2.coords
346             );
347 
348             // If the position of the points or the fixed length function has been changed we have to work.
349             if (d1 > Mat.eps || d2 > Mat.eps || d !== d_new) {
350                 drag1 =
351                     this.point1.isDraggable &&
352                     this.point1.type !== Const.OBJECT_TYPE_GLIDER &&
353                     !this.point1.evalVisProp('fixed');
354                 drag2 =
355                     this.point2.isDraggable &&
356                     this.point2.type !== Const.OBJECT_TYPE_GLIDER &&
357                     !this.point2.evalVisProp('fixed');
358 
359                 // First case: the two points are different
360                 // Then we try to adapt the point that was not dragged
361                 // If this point can not be moved (e.g. because it is a glider)
362                 // we try move the other point
363                 if (d > Mat.eps) {
364                     if ((d1 > d2 && drag2) || (d1 <= d2 && drag2 && !drag1)) {
365                         this.point2.setPositionDirectly(Const.COORDS_BY_USER, [
366                             this.point1.X() + ((this.point2.X() - this.point1.X()) * d_new) / d,
367                             this.point1.Y() + ((this.point2.Y() - this.point1.Y()) * d_new) / d
368                         ]);
369                         this.point2.fullUpdate();
370                     } else if ((d1 <= d2 && drag1) || (d1 > d2 && drag1 && !drag2)) {
371                         this.point1.setPositionDirectly(Const.COORDS_BY_USER, [
372                             this.point2.X() + ((this.point1.X() - this.point2.X()) * d_new) / d,
373                             this.point2.Y() + ((this.point1.Y() - this.point2.Y()) * d_new) / d
374                         ]);
375                         this.point1.fullUpdate();
376                     }
377                     // Second case: the two points are identical. In this situation
378                     // we choose a random direction.
379                 } else {
380                     x = Math.random() - 0.5;
381                     y = Math.random() - 0.5;
382                     d = Mat.hypot(x, y);
383 
384                     if (drag2) {
385                         this.point2.setPositionDirectly(Const.COORDS_BY_USER, [
386                             this.point1.X() + (x * d_new) / d,
387                             this.point1.Y() + (y * d_new) / d
388                         ]);
389                         this.point2.fullUpdate();
390                     } else if (drag1) {
391                         this.point1.setPositionDirectly(Const.COORDS_BY_USER, [
392                             this.point2.X() + (x * d_new) / d,
393                             this.point2.Y() + (y * d_new) / d
394                         ]);
395                         this.point1.fullUpdate();
396                     }
397                 }
398                 // Finally, we save the position of the two points.
399                 this.fixedLengthOldCoords[0].setCoordinates(
400                     Const.COORDS_BY_USER,
401                     this.point1.coords.usrCoords
402                 );
403                 this.fixedLengthOldCoords[1].setCoordinates(
404                     Const.COORDS_BY_USER,
405                     this.point2.coords.usrCoords
406                 );
407             }
408 
409             return this;
410         },
411 
412         /**
413          * Updates the stdform derived from the parent point positions.
414          * @private
415          */
416         updateStdform: function () {
417             var v = Mat.crossProduct(
418                 this.point1.coords.usrCoords,
419                 this.point2.coords.usrCoords
420             );
421 
422             this.stdform[0] = v[0];
423             this.stdform[1] = v[1];
424             this.stdform[2] = v[2];
425             this.stdform[3] = 0;
426 
427             this.normalize();
428         },
429 
430         /**
431          * Uses the boards renderer to update the line.
432          * @private
433          */
434         updateRenderer: function () {
435             //var wasReal;
436 
437             if (!this.needsUpdate) {
438                 return this;
439             }
440 
441             if (this.visPropCalc.visible) {
442                 // wasReal = this.isReal;
443                 this.isReal =
444                     !isNaN(
445                         this.point1.coords.usrCoords[1] +
446                         this.point1.coords.usrCoords[2] +
447                         this.point2.coords.usrCoords[1] +
448                         this.point2.coords.usrCoords[2]
449                     ) && Mat.innerProduct(this.stdform, this.stdform, 3) >= Mat.eps * Mat.eps;
450 
451                 if (
452                     //wasReal &&
453                     !this.isReal
454                 ) {
455                     this.updateVisibility(false);
456                 }
457             }
458 
459             if (this.visPropCalc.visible) {
460                 this.board.renderer.updateLine(this);
461             }
462 
463             /* Update the label if visible. */
464             if (
465                 this.hasLabel &&
466                 this.visPropCalc.visible &&
467                 this.label &&
468                 this.label.visPropCalc.visible &&
469                 this.isReal
470             ) {
471                 this.label.update();
472                 this.board.renderer.updateText(this.label);
473             }
474 
475             // Update rendNode display
476             this.setDisplayRendNode();
477 
478             this.needsUpdate = false;
479             return this;
480         },
481 
482         // /**
483         //  * Used to generate a polynomial for a point p that lies on this line, i.e. p is collinear to
484         //  * {@link JXG.Line#point1} and {@link JXG.Line#point2}.
485         //  *
486         //  * @param {JXG.Point} p The point for that the polynomial is generated.
487         //  * @returns {Array} An array containing the generated polynomial.
488         //  * @private
489         //  */
490         generatePolynomial: function (p) {
491             var u1 = this.point1.symbolic.x,
492                 u2 = this.point1.symbolic.y,
493                 v1 = this.point2.symbolic.x,
494                 v2 = this.point2.symbolic.y,
495                 w1 = p.symbolic.x,
496                 w2 = p.symbolic.y;
497 
498             /*
499              * The polynomial in this case is determined by three points being collinear:
500              *
501              *      U (u1,u2)      W (w1,w2)                V (v1,v2)
502              *  ----x--------------x------------------------x----------------
503              *
504              *  The collinearity condition is
505              *
506              *      u2-w2       w2-v2
507              *     -------  =  -------           (1)
508              *      u1-w1       w1-v1
509              *
510              * Multiplying (1) with denominators and simplifying is
511              *
512              *    u2w1 - u2v1 + w2v1 - u1w2 + u1v2 - w1v2 = 0
513              */
514 
515             return [
516                 [
517                     "(", u2, ")*(", w1, ")-(", u2, ")*(", v1, ")+(", w2, ")*(", v1, ")-(", u1, ")*(", w2, ")+(", u1, ")*(", v2, ")-(", w1, ")*(", v2, ")"
518                 ].join("")
519             ];
520         },
521 
522         /**
523          * Calculates the y intersect of the line.
524          * @returns {Number} The y intersect.
525          */
526         getRise: function () {
527             if (Math.abs(this.stdform[2]) >= Mat.eps) {
528                 return -this.stdform[0] / this.stdform[2];
529             }
530 
531             return Infinity;
532         },
533 
534         /**
535          * Calculates the slope of the line.
536          * @returns {Number} The slope of the line or Infinity if the line is parallel to the y-axis.
537          */
538         Slope: function () {
539             if (Math.abs(this.stdform[2]) >= Mat.eps) {
540                 return -this.stdform[1] / this.stdform[2];
541             }
542 
543             return Infinity;
544         },
545 
546         /**
547          * Alias for line.Slope
548          * @returns {Number} The slope of the line or Infinity if the line is parallel to the y-axis.
549          * @deprecated
550          * @see Line#Slope
551          */
552         getSlope: function () {
553             return this.Slope();
554         },
555 
556         /**
557          * Determines the angle between the positive x axis and the line.
558          * @returns {Number}
559          */
560         getAngle: function () {
561             return Math.atan2(-this.stdform[1], this.stdform[2]);
562         },
563 
564         /**
565          * Returns the direction vector of the line. This is an array of length two
566          * containing the direction vector as [x, y]. It is defined as
567          *  <li> the difference of the x- and y-coordinate of the second and first point, in case both points are finite or both points are infinite.
568          *  <li> [x, y] coordinates of point2, in case only point2 is infinite.
569          *  <li> [-x, -y] coordinates of point1, in case only point1 is infinite.
570          * @function
571          * @returns {Array} of length 2.
572          */
573         Direction: function () {
574             var coords1 = this.point1.coords.usrCoords,
575                 coords2 = this.point2.coords.usrCoords;
576 
577             if (coords2[0] === 0 && coords1[0] !== 0) {
578                 return coords2.slice(1);
579             }
580 
581             if (coords1[0] === 0 && coords2[0] !== 0) {
582                 return [-coords1[1], -coords1[2]];
583             }
584 
585             return [
586                 coords2[1] - coords1[1],
587                 coords2[2] - coords1[2]
588             ];
589         },
590 
591         /**
592          * Returns true, if the line is vertical (if the x coordinate of the direction vector is 0).
593          * @function
594          * @returns {Boolean}
595          */
596         isVertical: function () {
597             var dir = this.Direction();
598             return dir[0] === 0 && dir[1] !== 0;
599         },
600 
601         /**
602          * Returns true, if the line is horizontal (if the y coordinate of the direction vector is 0).
603          * @function
604          * @returns {Boolean}
605          */
606         isHorizontal: function () {
607             var dir = this.Direction();
608             return dir[1] === 0 && dir[0] !== 0;
609         },
610 
611         /**
612          * Determines whether the line is drawn beyond {@link JXG.Line#point1} and
613          * {@link JXG.Line#point2} and updates the line.
614          * @param {Boolean} straightFirst True if the Line shall be drawn beyond
615          * {@link JXG.Line#point1}, false otherwise.
616          * @param {Boolean} straightLast True if the Line shall be drawn beyond
617          * {@link JXG.Line#point2}, false otherwise.
618          * @see Line#straightFirst
619          * @see Line#straightLast
620          * @private
621          */
622         setStraight: function (straightFirst, straightLast) {
623             this.visProp.straightfirst = straightFirst;
624             this.visProp.straightlast = straightLast;
625 
626             this.board.renderer.updateLine(this);
627             return this;
628         },
629 
630         // documented in geometry element
631         getTextAnchor: function () {
632             return new Coords(
633                 Const.COORDS_BY_USER,
634                 [
635                     0.5 * (this.point2.X() + this.point1.X()),
636                     0.5 * (this.point2.Y() + this.point1.Y())
637                 ],
638                 this.board
639             );
640         },
641 
642         /**
643          * Adjusts Label coords relative to Anchor. DESCRIPTION
644          * @private
645          */
646         setLabelRelativeCoords: function (relCoords) {
647             if (Type.exists(this.label)) {
648                 this.label.relativeCoords = new Coords(
649                     Const.COORDS_BY_SCREEN,
650                     [relCoords[0], -relCoords[1]],
651                     this.board
652                 );
653             }
654         },
655 
656         // documented in geometry element
657         getLabelAnchor: function () {
658             var x, y, pos,
659                 xy, lbda, dx, dy, d,
660                 dist = 1.5,
661                 fs = 0,
662                 c1 = new Coords(Const.COORDS_BY_USER, this.point1.coords.usrCoords, this.board),
663                 c2 = new Coords(Const.COORDS_BY_USER, this.point2.coords.usrCoords, this.board),
664                 ev_sf = this.evalVisProp('straightfirst'),
665                 ev_sl = this.evalVisProp('straightlast');
666 
667             if (ev_sf || ev_sl) {
668                 Geometry.calcStraight(this, c1, c2, 0);
669             }
670 
671             c1 = c1.scrCoords;
672             c2 = c2.scrCoords;
673 
674             if (!Type.exists(this.label)) {
675                 return new Coords(Const.COORDS_BY_SCREEN, [NaN, NaN], this.board);
676             }
677 
678             pos = this.label.evalVisProp('position');
679             if (!Type.isString(pos)) {
680                 return new Coords(Const.COORDS_BY_SCREEN, [NaN, NaN], this.board);
681             }
682 
683             if (pos.indexOf('right') < 0 && pos.indexOf('left') < 0) {
684                 // Old positioning commands
685                 switch (pos) {
686                     case 'last':
687                         x = c2[1];
688                         y = c2[2];
689                         break;
690                     case 'first':
691                         x = c1[1];
692                         y = c1[2];
693                         break;
694                     case "lft":
695                     case "llft":
696                     case "ulft":
697                         if (c1[1] < c2[1] + Mat.eps) {
698                             x = c1[1];
699                             y = c1[2];
700                         } else {
701                             x = c2[1];
702                             y = c2[2];
703                         }
704                         break;
705                     case "rt":
706                     case "lrt":
707                     case "urt":
708                         if (c1[1] > c2[1] + Mat.eps) {
709                             x = c1[1];
710                             y = c1[2];
711                         } else {
712                             x = c2[1];
713                             y = c2[2];
714                         }
715                         break;
716                     default:
717                         x = 0.5 * (c1[1] + c2[1]);
718                         y = 0.5 * (c1[2] + c2[2]);
719                 }
720             } else {
721                 // New positioning
722                 xy = Type.parsePosition(pos);
723                 lbda = Type.parseNumber(xy.pos, 1, 1);
724 
725                 dx = c2[1] - c1[1];
726                 dy = c2[2] - c1[2];
727                 d = Mat.hypot(dx, dy);
728 
729                 if (xy.pos.indexOf('px') >= 0 ||
730                     xy.pos.indexOf('fr') >= 0 ||
731                     xy.pos.indexOf('%') >= 0) {
732                     // lbda is interpreted in screen coords
733 
734                     if (xy.pos.indexOf('px') >= 0) {
735                         // Pixel values are supported
736                         lbda /= d;
737                     }
738 
739                     // Position along the line
740                     x = c1[1] + lbda * dx;
741                     y = c1[2] + lbda * dy;
742                 } else {
743                     // lbda is given as number or as a number string
744                     // Then, lbda is interpreted in user coords
745                     x = c1[1] + lbda * this.board.unitX * dx / d;
746                     y = c1[2] + lbda * this.board.unitY * dy / d;
747                 }
748 
749                 // Position left or right
750                 if (xy.side === 'left') {
751                     dx *= -1;
752                 } else {
753                     dy *= -1;
754                 }
755                 if (Type.exists(this.label)) {
756                     dist = 0.5 * this.label.evalVisProp('distance') / d;
757                 }
758                 x += dy * this.label.size[0] * dist;
759                 y += dx * this.label.size[1] * dist;
760             }
761 
762             // Correct coordinates if the label seems to be outside of canvas.
763             if (ev_sf || ev_sl) {
764                 if (Type.exists(this.label)) {
765                     // Does not exist during createLabel
766                     fs = this.label.evalVisProp('fontsize');
767                 }
768 
769                 if (Math.abs(x) < Mat.eps) {
770                     x = fs;
771                 } else if (
772                     this.board.canvasWidth + Mat.eps > x &&
773                     x > this.board.canvasWidth - fs - Mat.eps
774                 ) {
775                     x = this.board.canvasWidth - fs;
776                 }
777 
778                 if (Mat.eps + fs > y && y > -Mat.eps) {
779                     y = fs;
780                 } else if (
781                     this.board.canvasHeight + Mat.eps > y &&
782                     y > this.board.canvasHeight - fs - Mat.eps
783                 ) {
784                     y = this.board.canvasHeight - fs;
785                 }
786             }
787 
788             return new Coords(Const.COORDS_BY_SCREEN, [x, y], this.board);
789         },
790 
791         // documented in geometry element
792         cloneToBackground: function () {
793             var copy = Type.getCloneObject(this),
794                 r, s,
795                 er;
796 
797             copy.point1 = this.point1;
798             copy.point2 = this.point2;
799             copy.stdform = this.stdform;
800 
801             s = this.getSlope();
802             r = this.getRise();
803             copy.getSlope = function () {
804                 return s;
805             };
806             copy.getRise = function () {
807                 return r;
808             };
809 
810             er = this.board.renderer.enhancedRendering;
811             this.board.renderer.enhancedRendering = true;
812             this.board.renderer.drawLine(copy);
813             this.board.renderer.enhancedRendering = er;
814             this.traces[copy.id] = copy.rendNode;
815 
816             return this;
817         },
818 
819         /**
820          * Add transformations to this line.
821          * @param {JXG.Transformation|Array} transform Either one {@link JXG.Transformation} or an array of
822          * {@link JXG.Transformation}s.
823          * @returns {JXG.Line} Reference to this line object.
824          */
825         addTransform: function (transform) {
826             var i,
827                 list = Type.isArray(transform) ? transform : [transform],
828                 len = list.length;
829 
830             for (i = 0; i < len; i++) {
831                 this.point1.transformations.push(list[i]);
832                 this.point2.transformations.push(list[i]);
833             }
834 
835             // Why not like this?
836             // The difference is in setting baseElement
837             // var list = Type.isArray(transform) ? transform : [transform];
838             // this.point1.addTransform(this, list);
839             // this.point2.addTransform(this, list);
840 
841             return this;
842         },
843 
844         // see GeometryElement.js
845         snapToGrid: function (pos) {
846             var c1, c2, dc, t, ticks, x, y, sX, sY;
847 
848             if (this.evalVisProp('snaptogrid')) {
849                 if (this.parents.length < 3) {
850                     // Line through two points
851                     this.point1.handleSnapToGrid(true, true);
852                     this.point2.handleSnapToGrid(true, true);
853                 } else if (Type.exists(pos)) {
854                     // Free line
855                     sX = this.evalVisProp('snapsizex');
856                     sY = this.evalVisProp('snapsizey');
857 
858                     c1 = new Coords(Const.COORDS_BY_SCREEN, [pos.Xprev, pos.Yprev], this.board);
859 
860                     x = c1.usrCoords[1];
861                     y = c1.usrCoords[2];
862 
863                     if (
864                         sX <= 0 &&
865                         this.board.defaultAxes &&
866                         this.board.defaultAxes.x.defaultTicks
867                     ) {
868                         ticks = this.board.defaultAxes.x.defaultTicks;
869                         sX = ticks.ticksDelta * (ticks.evalVisProp('minorticks') + 1);
870                     }
871                     if (
872                         sY <= 0 &&
873                         this.board.defaultAxes &&
874                         this.board.defaultAxes.y.defaultTicks
875                     ) {
876                         ticks = this.board.defaultAxes.y.defaultTicks;
877                         sY = ticks.ticksDelta * (ticks.evalVisProp('minorticks') + 1);
878                     }
879 
880                     // if no valid snap sizes are available, don't change the coords.
881                     if (sX > 0 && sY > 0) {
882                         // projectCoordsToLine
883                         /*
884                         v = [0, this.stdform[1], this.stdform[2]];
885                         v = Mat.crossProduct(v, c1.usrCoords);
886                         c2 = Geometry.meetLineLine(v, this.stdform, 0, this.board);
887                         */
888                         c2 = Geometry.projectPointToLine({ coords: c1 }, this, this.board);
889 
890                         dc = Statistics.subtract(
891                             [1, Math.round(x / sX) * sX, Math.round(y / sY) * sY],
892                             c2.usrCoords
893                         );
894                         t = this.board.create("transform", dc.slice(1), {
895                             type: "translate"
896                         });
897                         t.applyOnce([this.point1, this.point2]);
898                     }
899                 }
900             } else {
901                 this.point1.handleSnapToGrid(false, true);
902                 this.point2.handleSnapToGrid(false, true);
903             }
904 
905             return this;
906         },
907 
908         // see element.js
909         snapToPoints: function () {
910             var forceIt = this.evalVisProp('snaptopoints');
911 
912             if (this.parents.length < 3) {
913                 // Line through two points
914                 this.point1.handleSnapToPoints(forceIt);
915                 this.point2.handleSnapToPoints(forceIt);
916             }
917 
918             return this;
919         },
920 
921         /**
922          * Treat the line as parametric curve in homogeneous coordinates, where the parameter t runs from 0 to 1.
923          * First we transform the interval [0,1] to [-1,1].
924          * If the line has homogeneous coordinates [c, a, b] = stdform[] then the direction of the line is [b, -a].
925          * Now, we take one finite point that defines the line, i.e. we take either point1 or point2
926          * (in case the line is not the ideal line).
927          * Let the coordinates of that point be [z, x, y].
928          * Then, the curve runs linearly from
929          * [0, b, -a] (t=-1) to [z, x, y] (t=0)
930          * and
931          * [z, x, y] (t=0) to [0, -b, a] (t=1)
932          *
933          * @param {Number} t Parameter running from 0 to 1.
934          * @returns {Number} X(t) x-coordinate of the line treated as parametric curve.
935          * */
936         X: function (t) {
937             var x,
938                 b = this.stdform[2];
939 
940             x =
941                 Math.abs(this.point1.coords.usrCoords[0]) > Mat.eps
942                     ? this.point1.coords.usrCoords[1]
943                     : this.point2.coords.usrCoords[1];
944 
945             t = (t - 0.5) * 2;
946 
947             return (1 - Math.abs(t)) * x - t * b;
948         },
949 
950         /**
951          * Treat the line as parametric curve in homogeneous coordinates.
952          * See {@link JXG.Line#X} for a detailed description.
953          * @param {Number} t Parameter running from 0 to 1.
954          * @returns {Number} Y(t) y-coordinate of the line treated as parametric curve.
955          */
956         Y: function (t) {
957             var y,
958                 a = this.stdform[1];
959 
960             y =
961                 Math.abs(this.point1.coords.usrCoords[0]) > Mat.eps
962                     ? this.point1.coords.usrCoords[2]
963                     : this.point2.coords.usrCoords[2];
964 
965             t = (t - 0.5) * 2;
966 
967             return (1 - Math.abs(t)) * y + t * a;
968         },
969 
970         /**
971          * Treat the line as parametric curve in homogeneous coordinates.
972          * See {@link JXG.Line#X} for a detailed description.
973          *
974          * @param {Number} t Parameter running from 0 to 1.
975          * @returns {Number} Z(t) z-coordinate of the line treated as parametric curve.
976          */
977         Z: function (t) {
978             var z =
979                 Math.abs(this.point1.coords.usrCoords[0]) > Mat.eps
980                     ? this.point1.coords.usrCoords[0]
981                     : this.point2.coords.usrCoords[0];
982 
983             t = (t - 0.5) * 2;
984 
985             return (1 - Math.abs(t)) * z;
986         },
987 
988         /**
989          * The distance between the two points defining the line.
990          * @returns {Number}
991          */
992         L: function () {
993             return this.point1.Dist(this.point2);
994         },
995 
996         /**
997          * Set a new fixed length, then update the board.
998          * @param {String|Number|function} l A string, function or number describing the new length.
999          * @returns {JXG.Line} Reference to this line
1000          */
1001         setFixedLength: function (l) {
1002             if (!this.hasFixedLength) {
1003                 return this;
1004             }
1005 
1006             this.fixedLength = Type.createFunction(l, this.board);
1007             this.hasFixedLength = true;
1008             this.addParentsFromJCFunctions([this.fixedLength]);
1009             this.board.update();
1010 
1011             return this;
1012         },
1013 
1014         /**
1015          * Treat the element  as a parametric curve
1016          * @private
1017          */
1018         minX: function () {
1019             return 0.0;
1020         },
1021 
1022         /**
1023          * Treat the element as parametric curve
1024          * @private
1025          */
1026         maxX: function () {
1027             return 1.0;
1028         },
1029 
1030         // documented in geometry element
1031         bounds: function () {
1032             var p1c = this.point1.coords.usrCoords,
1033                 p2c = this.point2.coords.usrCoords;
1034 
1035             return [
1036                 Math.min(p1c[1], p2c[1]),
1037                 Math.max(p1c[2], p2c[2]),
1038                 Math.max(p1c[1], p2c[1]),
1039                 Math.min(p1c[2], p2c[2])
1040             ];
1041         },
1042 
1043         // documented in GeometryElement.js
1044         remove: function () {
1045             this.removeAllTicks();
1046             GeometryElement.prototype.remove.call(this);
1047         }
1048 
1049         // hideElement: function () {
1050         //     var i;
1051         //
1052         //     GeometryElement.prototype.hideElement.call(this);
1053         //
1054         //     for (i = 0; i < this.ticks.length; i++) {
1055         //         this.ticks[i].hideElement();
1056         //     }
1057         // },
1058         //
1059         // showElement: function () {
1060         //     var i;
1061         //     GeometryElement.prototype.showElement.call(this);
1062         //
1063         //     for (i = 0; i < this.ticks.length; i++) {
1064         //         this.ticks[i].showElement();
1065         //     }
1066         // }
1067 
1068     }
1069 );
1070 
1071 /**
1072  * @class This element is used to provide a constructor for a general line. A general line is given by two points. By setting additional properties
1073  * a line can be used as an arrow and/or axis.
1074  * @pseudo
1075  * @name Line
1076  * @augments JXG.Line
1077  * @constructor
1078  * @type JXG.Line
1079  * @throws {Exception} If the element cannot be constructed with the given parent objects an exception is thrown.
1080  * @param {JXG.Point,array,function_JXG.Point,array,function} point1,point2 Parent elements can be two elements either of type {@link JXG.Point} or array of
1081  * numbers describing the coordinates of a point. In the latter case the point will be constructed automatically as a fixed invisible point.
1082  * It is possible to provide a function returning an array or a point, instead of providing an array or a point.
1083  * @param {Number,function_Number,function_Number,function} a,b,c A line can also be created providing three numbers. The line is then described by
1084  * the set of solutions of the equation <tt>a*z+b*x+c*y = 0</tt>. For all finite points, z is normalized to the value 1.
1085  * It is possible to provide three functions returning numbers, too.
1086  * @param {function} f This function must return an array containing three numbers forming the line's homogeneous coordinates.
1087  * <p>
1088  * Additionally, a line can be created by providing a line and a transformation (or an array of transformations).
1089  * Then, the result is a line which is the transformation of the supplied line.
1090  * @example
1091  * // Create a line using point and coordinates/
1092  * // The second point will be fixed and invisible.
1093  * var p1 = board.create('point', [4.5, 2.0]);
1094  * var l1 = board.create('line', [p1, [1.0, 1.0]]);
1095  * </pre><div class="jxgbox" id="JXGc0ae3461-10c4-4d39-b9be-81d74759d122" style="width: 300px; height: 300px;"></div>
1096  * <script type="text/javascript">
1097  *   var glex1_board = JXG.JSXGraph.initBoard('JXGc0ae3461-10c4-4d39-b9be-81d74759d122', {boundingbox: [-1, 7, 7, -1], axis: true, showcopyright: false, shownavigation: false});
1098  *   var glex1_p1 = glex1_board.create('point', [4.5, 2.0]);
1099  *   var glex1_l1 = glex1_board.create('line', [glex1_p1, [1.0, 1.0]]);
1100  * </script><pre>
1101  * @example
1102  * // Create a line using three coordinates
1103  * var l1 = board.create('line', [1.0, -2.0, 3.0]);
1104  * </pre><div class="jxgbox" id="JXGcf45e462-f964-4ba4-be3a-c9db94e2593f" style="width: 300px; height: 300px;"></div>
1105  * <script type="text/javascript">
1106  *   var glex2_board = JXG.JSXGraph.initBoard('JXGcf45e462-f964-4ba4-be3a-c9db94e2593f', {boundingbox: [-1, 7, 7, -1], axis: true, showcopyright: false, shownavigation: false});
1107  *   var glex2_l1 = glex2_board.create('line', [1.0, -2.0, 3.0]);
1108  * </script><pre>
1109  * @example
1110  *         // Create a line (l2) as reflection of another line (l1)
1111  *         // reflection line
1112  *         var li = board.create('line', [1,1,1], {strokeColor: '#aaaaaa'});
1113  *         var reflect = board.create('transform', [li], {type: 'reflect'});
1114  *
1115  *         var l1 = board.create('line', [1,-5,1]);
1116  *         var l2 = board.create('line', [l1, reflect]);
1117  *
1118  * </pre><div id="JXGJXGa00d7dd6-d38c-11e7-93b3-901b0e1b8723" class="jxgbox" style="width: 300px; height: 300px;"></div>
1119  * <script type="text/javascript">
1120  *     (function() {
1121  *         var board = JXG.JSXGraph.initBoard('JXGJXGa00d7dd6-d38c-11e7-93b3-901b0e1b8723',
1122  *             {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false});
1123  *             // reflection line
1124  *             var li = board.create('line', [1,1,1], {strokeColor: '#aaaaaa'});
1125  *             var reflect = board.create('transform', [li], {type: 'reflect'});
1126  *
1127  *             var l1 = board.create('line', [1,-5,1]);
1128  *             var l2 = board.create('line', [l1, reflect]);
1129  *     })();
1130  *
1131  * </script><pre>
1132  *
1133  * @example
1134  * var t = board.create('transform', [2, 1.5], {type: 'scale'});
1135  * var l1 = board.create('line', [1, -5, 1]);
1136  * var l2 = board.create('line', [l1, t]);
1137  *
1138  * </pre><div id="d16d5b58-6338-11e8-9fb9-901b0e1b8723" class="jxgbox" style="width: 300px; height: 300px;"></div>
1139  * <script type="text/javascript">
1140  *     (function() {
1141  *         var board = JXG.JSXGraph.initBoard('d16d5b58-6338-11e8-9fb9-901b0e1b8723',
1142  *             {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false});
1143  *     var t = board.create('transform', [2, 1.5], {type: 'scale'});
1144  *     var l1 = board.create('line', [1, -5, 1]);
1145  *     var l2 = board.create('line', [l1, t]);
1146  *
1147  *     })();
1148  *
1149  * </script><pre>
1150  *
1151  * @example
1152  * //create line between two points
1153  * var p1 = board.create('point', [0,0]);
1154  * var p2 = board.create('point', [2,2]);
1155  * var l1 = board.create('line', [p1,p2], {straightFirst:false, straightLast:false});
1156  * </pre><div id="d21d5b58-6338-11e8-9fb9-901b0e1b8723" class="jxgbox" style="width: 300px; height: 300px;"></div>
1157  * <script type="text/javascript">
1158  *     (function() {
1159  *         var board = JXG.JSXGraph.initBoard('d21d5b58-6338-11e8-9fb9-901b0e1b8723',
1160  *             {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false});
1161  *             var ex5p1 = board.create('point', [0,0]);
1162  *             var ex5p2 = board.create('point', [2,2]);
1163  *             var ex5l1 = board.create('line', [ex5p1,ex5p2], {straightFirst:false, straightLast:false});
1164  *     })();
1165  *
1166  * </script><pre>
1167  */
1168 JXG.createLine = function (board, parents, attributes) {
1169     var ps, el, p1, p2, i, attr,
1170         c = [],
1171         doTransform = false,
1172         constrained = false,
1173         isDraggable;
1174 
1175     if (parents.length === 2) {
1176         // The line is defined by two points or coordinates of two points.
1177         // In the latter case, the points are created.
1178         attr = Type.copyAttributes(attributes, board.options, "line", "point1");
1179         if (Type.isArray(parents[0]) && parents[0].length > 1) {
1180             p1 = board.create("point", parents[0], attr);
1181         } else if (Type.isString(parents[0]) || Type.isPoint(parents[0])) {
1182             p1 = board.select(parents[0]);
1183         } else if (Type.isFunction(parents[0]) && Type.isPoint(parents[0]())) {
1184             p1 = parents[0]();
1185             constrained = true;
1186         } else if (
1187             Type.isFunction(parents[0]) &&
1188             parents[0]().length &&
1189             parents[0]().length >= 2
1190         ) {
1191             p1 = JXG.createPoint(board, parents[0](), attr);
1192             constrained = true;
1193         } else if (Type.isObject(parents[0]) && Type.isTransformationOrArray(parents[1])) {
1194             doTransform = true;
1195             p1 = board.create("point", [parents[0].point1, parents[1]], attr);
1196         } else {
1197             throw new Error(
1198                 "JSXGraph: Can't create line with parent types '" +
1199                 typeof parents[0] +
1200                 "' and '" +
1201                 typeof parents[1] +
1202                 "'." +
1203                 "\nPossible parent types: [point,point], [[x1,y1],[x2,y2]], [a,b,c]"
1204             );
1205         }
1206 
1207         // point 2 given by coordinates
1208         attr = Type.copyAttributes(attributes, board.options, "line", "point2");
1209         if (doTransform) {
1210             p2 = board.create("point", [parents[0].point2, parents[1]], attr);
1211         } else if (Type.isArray(parents[1]) && parents[1].length > 1) {
1212             p2 = board.create("point", parents[1], attr);
1213         } else if (Type.isString(parents[1]) || Type.isPoint(parents[1])) {
1214             p2 = board.select(parents[1]);
1215         } else if (Type.isFunction(parents[1]) && Type.isPoint(parents[1]())) {
1216             p2 = parents[1]();
1217             constrained = true;
1218         } else if (
1219             Type.isFunction(parents[1]) &&
1220             parents[1]().length &&
1221             parents[1]().length >= 2
1222         ) {
1223             p2 = JXG.createPoint(board, parents[1](), attr);
1224             constrained = true;
1225         } else {
1226             throw new Error(
1227                 "JSXGraph: Can't create line with parent types '" +
1228                 typeof parents[0] +
1229                 "' and '" +
1230                 typeof parents[1] +
1231                 "'." +
1232                 "\nPossible parent types: [point,point], [[x1,y1],[x2,y2]], [a,b,c]"
1233             );
1234         }
1235 
1236         attr = Type.copyAttributes(attributes, board.options, "line");
1237         el = new JXG.Line(board, p1, p2, attr);
1238 
1239         if (constrained) {
1240             el.constrained = true;
1241             el.funp1 = parents[0];
1242             el.funp2 = parents[1];
1243         } else if (!doTransform) {
1244             el.isDraggable = true;
1245         }
1246 
1247         //if (!el.constrained) {
1248         el.setParents([p1.id, p2.id]);
1249         //}
1250 
1251     } else if (parents.length === 3) {
1252         // Free line:
1253         // Line is defined by three homogeneous coordinates.
1254         // Also in this case points are created.
1255         isDraggable = true;
1256         for (i = 0; i < 3; i++) {
1257             if (Type.isNumber(parents[i])) {
1258                 // createFunction will just wrap a function around our constant number
1259                 // that does nothing else but to return that number.
1260                 c[i] = Type.createFunction(parents[i]);
1261             } else if (Type.isFunction(parents[i])) {
1262                 c[i] = parents[i];
1263                 isDraggable = false;
1264             } else {
1265                 throw new Error(
1266                     "JSXGraph: Can't create line with parent types '" +
1267                     typeof parents[0] +
1268                     "' and '" +
1269                     typeof parents[1] +
1270                     "' and '" +
1271                     typeof parents[2] +
1272                     "'." +
1273                     "\nPossible parent types: [point,point], [[x1,y1],[x2,y2]], [a,b,c]"
1274                 );
1275             }
1276         }
1277 
1278         // point 1 is the midpoint between (0, c, -b) and point 2. => point1 is finite.
1279         attr = Type.copyAttributes(attributes, board.options, "line", "point1");
1280         if (isDraggable) {
1281             p1 = board.create("point", [
1282                 c[2]() * c[2]() + c[1]() * c[1](),
1283                 c[2]() - c[1]() * c[0]() + c[2](),
1284                 -c[1]() - c[2]() * c[0]() - c[1]()
1285             ], attr);
1286         } else {
1287             p1 = board.create("point", [
1288                 function () {
1289                     return (c[2]() * c[2]() + c[1]() * c[1]()) * 0.5;
1290                 },
1291                 function () {
1292                     return (c[2]() - c[1]() * c[0]() + c[2]()) * 0.5;
1293                 },
1294                 function () {
1295                     return (-c[1]() - c[2]() * c[0]() - c[1]()) * 0.5;
1296                 }
1297             ], attr);
1298         }
1299 
1300         // point 2: (b^2+c^2,-ba+c,-ca-b)
1301         attr = Type.copyAttributes(attributes, board.options, "line", "point2");
1302         if (isDraggable) {
1303             p2 = board.create("point", [
1304                 c[2]() * c[2]() + c[1]() * c[1](),
1305                 -c[1]() * c[0]() + c[2](),
1306                 -c[2]() * c[0]() - c[1]()
1307             ], attr);
1308         } else {
1309             p2 = board.create("point", [
1310                 function () {
1311                     return c[2]() * c[2]() + c[1]() * c[1]();
1312                 },
1313                 function () {
1314                     return -c[1]() * c[0]() + c[2]();
1315                 },
1316                 function () {
1317                     return -c[2]() * c[0]() - c[1]();
1318                 }
1319             ], attr);
1320         }
1321 
1322         // If the line will have a glider and board.suspendUpdate() has been called, we
1323         // need to compute the initial position of the two points p1 and p2.
1324         p1.prepareUpdate().update();
1325         p2.prepareUpdate().update();
1326         attr = Type.copyAttributes(attributes, board.options, "line");
1327         el = new JXG.Line(board, p1, p2, attr);
1328         // Not yet working, because the points are not draggable.
1329         el.isDraggable = isDraggable;
1330         el.setParents([p1, p2]);
1331 
1332     } else if (
1333         // The parent array contains a function which returns two points.
1334         parents.length === 1 &&
1335         Type.isFunction(parents[0]) &&
1336         parents[0]().length === 2 &&
1337         Type.isPoint(parents[0]()[0]) &&
1338         Type.isPoint(parents[0]()[1])
1339     ) {
1340         ps = parents[0]();
1341         attr = Type.copyAttributes(attributes, board.options, "line");
1342         el = new JXG.Line(board, ps[0], ps[1], attr);
1343         el.constrained = true;
1344         el.funps = parents[0];
1345         el.setParents(ps);
1346     } else if (
1347         parents.length === 1 &&
1348         Type.isFunction(parents[0]) &&
1349         parents[0]().length === 3 &&
1350         Type.isNumber(parents[0]()[0]) &&
1351         Type.isNumber(parents[0]()[1]) &&
1352         Type.isNumber(parents[0]()[2])
1353     ) {
1354         ps = parents[0];
1355 
1356         attr = Type.copyAttributes(attributes, board.options, "line", "point1");
1357         p1 = board.create("point", [
1358             function () {
1359                 var c = ps();
1360 
1361                 return [
1362                     (c[2] * c[2] + c[1] * c[1]) * 0.5,
1363                     (c[2] - c[1] * c[0] + c[2]) * 0.5,
1364                     (-c[1] - c[2] * c[0] - c[1]) * 0.5
1365                 ];
1366             }
1367         ], attr);
1368 
1369         attr = Type.copyAttributes(attributes, board.options, "line", "point2");
1370         p2 = board.create("point", [
1371             function () {
1372                 var c = ps();
1373 
1374                 return [
1375                     c[2] * c[2] + c[1] * c[1],
1376                     -c[1] * c[0] + c[2],
1377                     -c[2] * c[0] - c[1]
1378                 ];
1379             }
1380         ], attr);
1381 
1382         attr = Type.copyAttributes(attributes, board.options, "line");
1383         el = new JXG.Line(board, p1, p2, attr);
1384 
1385         el.constrained = true;
1386         el.funps = parents[0];
1387         el.setParents([p1, p2]);
1388     } else {
1389         throw new Error(
1390             "JSXGraph: Can't create line with parent types '" +
1391             typeof parents[0] +
1392             "' and '" +
1393             typeof parents[1] +
1394             "'." +
1395             "\nPossible parent types: [point,point], [[x1,y1],[x2,y2]], [a,b,c]"
1396         );
1397     }
1398 
1399     return el;
1400 };
1401 
1402 JXG.registerElement("line", JXG.createLine);
1403 
1404 /**
1405  * @class This element is used to provide a constructor for a segment.
1406  * It's strictly spoken just a wrapper for element {@link Line} with {@link Line#straightFirst}
1407  * and {@link Line#straightLast} properties set to false. If there is a third variable then the
1408  * segment has a fixed length (which may be a function, too) determined by the absolute value of
1409  * that number.
1410  * @pseudo
1411  * @name Segment
1412  * @augments JXG.Line
1413  * @constructor
1414  * @type JXG.Line
1415  * @throws {Exception} If the element cannot be constructed with the given parent objects an exception is thrown.
1416  * @param {JXG.Point,array_JXG.Point,array} point1,point2 Parent elements can be two elements either of type {@link JXG.Point}
1417  * or array of numbers describing the
1418  * coordinates of a point. In the latter case the point will be constructed automatically as a fixed invisible point.
1419  * @param {number,function} [length] The points are adapted - if possible - such that their distance
1420  * is equal to the absolute value of this number.
1421  * @see Line
1422  * @example
1423  * // Create a segment providing two points.
1424  *   var p1 = board.create('point', [4.5, 2.0]);
1425  *   var p2 = board.create('point', [1.0, 1.0]);
1426  *   var l1 = board.create('segment', [p1, p2]);
1427  * </pre><div class="jxgbox" id="JXGd70e6aac-7c93-4525-a94c-a1820fa38e2f" style="width: 300px; height: 300px;"></div>
1428  * <script type="text/javascript">
1429  *   var slex1_board = JXG.JSXGraph.initBoard('JXGd70e6aac-7c93-4525-a94c-a1820fa38e2f', {boundingbox: [-1, 7, 7, -1], axis: true, showcopyright: false, shownavigation: false});
1430  *   var slex1_p1 = slex1_board.create('point', [4.5, 2.0]);
1431  *   var slex1_p2 = slex1_board.create('point', [1.0, 1.0]);
1432  *   var slex1_l1 = slex1_board.create('segment', [slex1_p1, slex1_p2]);
1433  * </script><pre>
1434  *
1435  * @example
1436  * // Create a segment providing two points.
1437  *   var p1 = board.create('point', [4.0, 1.0]);
1438  *   var p2 = board.create('point', [1.0, 1.0]);
1439  *   // AB
1440  *   var l1 = board.create('segment', [p1, p2]);
1441  *   var p3 = board.create('point', [4.0, 2.0]);
1442  *   var p4 = board.create('point', [1.0, 2.0]);
1443  *   // CD
1444  *   var l2 = board.create('segment', [p3, p4, 3]); // Fixed length
1445  *   var p5 = board.create('point', [4.0, 3.0]);
1446  *   var p6 = board.create('point', [1.0, 4.0]);
1447  *   // EF
1448  *   var l3 = board.create('segment', [p5, p6, function(){ return l1.L();} ]); // Fixed, but dependent length
1449  * </pre><div class="jxgbox" id="JXG617336ba-0705-4b2b-a236-c87c28ef25be" style="width: 300px; height: 300px;"></div>
1450  * <script type="text/javascript">
1451  *   var slex2_board = JXG.JSXGraph.initBoard('JXG617336ba-0705-4b2b-a236-c87c28ef25be', {boundingbox: [-1, 7, 7, -1], axis: true, showcopyright: false, shownavigation: false});
1452  *   var slex2_p1 = slex2_board.create('point', [4.0, 1.0]);
1453  *   var slex2_p2 = slex2_board.create('point', [1.0, 1.0]);
1454  *   var slex2_l1 = slex2_board.create('segment', [slex2_p1, slex2_p2]);
1455  *   var slex2_p3 = slex2_board.create('point', [4.0, 2.0]);
1456  *   var slex2_p4 = slex2_board.create('point', [1.0, 2.0]);
1457  *   var slex2_l2 = slex2_board.create('segment', [slex2_p3, slex2_p4, 3]);
1458  *   var slex2_p5 = slex2_board.create('point', [4.0, 2.0]);
1459  *   var slex2_p6 = slex2_board.create('point', [1.0, 2.0]);
1460  *   var slex2_l3 = slex2_board.create('segment', [slex2_p5, slex2_p6, function(){ return slex2_l1.L();}]);
1461  * </script><pre>
1462  *
1463  */
1464 JXG.createSegment = function (board, parents, attributes) {
1465     var el, attr;
1466 
1467     attributes.straightFirst = false;
1468     attributes.straightLast = false;
1469     attr = Type.copyAttributes(attributes, board.options, "segment");
1470 
1471     el = board.create("line", parents.slice(0, 2), attr);
1472 
1473     if (parents.length === 3) {
1474         try {
1475             el.hasFixedLength = true;
1476             el.fixedLengthOldCoords = [];
1477             el.fixedLengthOldCoords[0] = new Coords(
1478                 Const.COORDS_BY_USER,
1479                 el.point1.coords.usrCoords.slice(1, 3),
1480                 board
1481             );
1482             el.fixedLengthOldCoords[1] = new Coords(
1483                 Const.COORDS_BY_USER,
1484                 el.point2.coords.usrCoords.slice(1, 3),
1485                 board
1486             );
1487 
1488             el.setFixedLength(parents[2]);
1489         } catch (err) {
1490             throw new Error(
1491                 "JSXGraph: Can't create segment with third parent type '" +
1492                 typeof parents[2] +
1493                 "'." +
1494                 "\nPossible third parent types: number or function"
1495             );
1496         }
1497         // if (Type.isNumber(parents[2])) {
1498         //     el.fixedLength = function () {
1499         //         return parents[2];
1500         //     };
1501         // } else if (Type.isFunction(parents[2])) {
1502         //     el.fixedLength = Type.createFunction(parents[2], this.board);
1503         // } else {
1504         //     throw new Error(
1505         //         "JSXGraph: Can't create segment with third parent type '" +
1506         //             typeof parents[2] +
1507         //             "'." +
1508         //             "\nPossible third parent types: number or function"
1509         //     );
1510         // }
1511 
1512         el.getParents = function () {
1513             return this.parents.concat(this.fixedLength());
1514         };
1515 
1516     }
1517 
1518     el.elType = "segment";
1519 
1520     return el;
1521 };
1522 
1523 JXG.registerElement("segment", JXG.createSegment);
1524 
1525 /**
1526  * @class This element is used to provide a constructor for arrow, which is just a wrapper for element
1527  * {@link Line} with {@link Line#straightFirst}
1528  * and {@link Line#straightLast} properties set to false and {@link Line#lastArrow} set to true.
1529  * @pseudo
1530  * @name Arrow
1531  * @augments JXG.Line
1532  * @constructor
1533  * @type JXG.Line
1534  * @throws {Exception} If the element cannot be constructed with the given parent objects an exception is thrown.
1535  * @param {JXG.Point,array_JXG.Point,array} point1,point2 Parent elements can be two elements either of type {@link JXG.Point} or array of numbers describing the
1536  * coordinates of a point. In the latter case the point will be constructed automatically as a fixed invisible point.
1537  * @param {Number_Number_Number} a,b,c A line can also be created providing three numbers. The line is then described by the set of solutions
1538  * of the equation <tt>a*x+b*y+c*z = 0</tt>.
1539  * @see Line
1540  * @example
1541  * // Create an arrow providing two points.
1542  *   var p1 = board.create('point', [4.5, 2.0]);
1543  *   var p2 = board.create('point', [1.0, 1.0]);
1544  *   var l1 = board.create('arrow', [p1, p2]);
1545  * </pre><div class="jxgbox" id="JXG1d26bd22-7d6d-4018-b164-4c8bc8d22ccf" style="width: 300px; height: 300px;"></div>
1546  * <script type="text/javascript">
1547  *   var alex1_board = JXG.JSXGraph.initBoard('JXG1d26bd22-7d6d-4018-b164-4c8bc8d22ccf', {boundingbox: [-1, 7, 7, -1], axis: true, showcopyright: false, shownavigation: false});
1548  *   var alex1_p1 = alex1_board.create('point', [4.5, 2.0]);
1549  *   var alex1_p2 = alex1_board.create('point', [1.0, 1.0]);
1550  *   var alex1_l1 = alex1_board.create('arrow', [alex1_p1, alex1_p2]);
1551  * </script><pre>
1552  */
1553 JXG.createArrow = function (board, parents, attributes) {
1554     var el, attr;
1555 
1556     attributes.straightFirst = false;
1557     attributes.straightLast = false;
1558     attr = Type.copyAttributes(attributes, board.options, "arrow");
1559     el = board.create("line", parents, attr);
1560     //el.setArrow(false, true);
1561     el.type = Const.OBJECT_TYPE_VECTOR;
1562     el.elType = "arrow";
1563 
1564     return el;
1565 };
1566 
1567 JXG.registerElement("arrow", JXG.createArrow);
1568 
1569 /**
1570  * @class This element is used to provide a constructor for an axis. It's strictly spoken just a wrapper for element {@link Line} with {@link Line#straightFirst}
1571  * and {@link Line#straightLast} properties set to true. Additionally {@link Line#lastArrow} is set to true and default {@link Ticks} will be created.
1572  * @pseudo
1573  * @name Axis
1574  * @augments JXG.Line
1575  * @constructor
1576  * @type JXG.Line
1577  * @throws {Exception} If the element cannot be constructed with the given parent objects an exception is thrown.
1578  * @param {JXG.Point,array_JXG.Point,array} point1,point2 Parent elements can be two elements either of type {@link JXG.Point} or array of numbers describing the
1579  * coordinates of a point. In the latter case, the point will be constructed automatically as a fixed invisible point.
1580  * @param {Number_Number_Number} a,b,c A line can also be created providing three numbers. The line is then described by the set of solutions
1581  * of the equation <tt>a*x+b*y+c*z = 0</tt>.
1582  * @example
1583  * // Create an axis providing two coords pairs.
1584  *   var l1 = board.create('axis', [[0.0, 1.0], [1.0, 1.3]]);
1585  * </pre><div class="jxgbox" id="JXG4f414733-624c-42e4-855c-11f5530383ae" style="width: 300px; height: 300px;"></div>
1586  * <script type="text/javascript">
1587  *   var axex1_board = JXG.JSXGraph.initBoard('JXG4f414733-624c-42e4-855c-11f5530383ae', {boundingbox: [-1, 7, 7, -1], axis: true, showcopyright: false, shownavigation: false});
1588  *   var axex1_l1 = axex1_board.create('axis', [[0.0, 1.0], [1.0, 1.3]]);
1589  * </script><pre>
1590  * @example
1591  *  // Create ticks labels as fractions
1592  *  board.create('axis', [[0,1], [1,1]], {
1593  *      ticks: {
1594  *          label: {
1595  *              toFraction: true,
1596  *              useMathjax: false,
1597  *              anchorX: 'middle',
1598  *              offset: [0, -10]
1599  *          }
1600  *      }
1601  *  });
1602  *
1603  *
1604  * </pre><div id="JXG34174cc4-0050-4ab4-af69-e91365d0666f" class="jxgbox" style="width: 300px; height: 300px;"></div>
1605  * <script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js" id="MathJax-script"></script>
1606  * <script type="text/javascript">
1607  *     (function() {
1608  *         var board = JXG.JSXGraph.initBoard('JXG34174cc4-0050-4ab4-af69-e91365d0666f',
1609  *             {boundingbox: [-1.2, 2.3, 1.2, -2.3], axis: true, showcopyright: false, shownavigation: false});
1610  *             board.create('axis', [[0,1], [1,1]], {
1611  *                 ticks: {
1612  *                     label: {
1613  *                         toFraction: true,
1614  *                         useMathjax: false,
1615  *                         anchorX: 'middle',
1616  *                         offset: [0, -10]
1617  *                     }
1618  *                 }
1619  *             });
1620  *
1621  *
1622  *     })();
1623  *
1624  * </script><pre>
1625  *
1626  */
1627 JXG.createAxis = function (board, parents, attributes) {
1628     var axis, attr,
1629         ancestor, ticksDist;
1630 
1631     // Create line
1632     attr = Type.copyAttributes(attributes, board.options, "axis");
1633     try {
1634         axis = board.create("line", parents, attr);
1635     } catch (err) {
1636         throw new Error(
1637             "JSXGraph: Can't create axis with parent types '" +
1638             typeof parents[0] +
1639             "' and '" +
1640             typeof parents[1] +
1641             "'." +
1642             "\nPossible parent types: [point,point], [[x1,y1],[x2,y2]]"
1643         );
1644     }
1645 
1646     axis.type = Const.OBJECT_TYPE_AXIS;
1647     axis.isDraggable = false;
1648     axis.point1.isDraggable = false;
1649     axis.point2.isDraggable = false;
1650 
1651     // Save usrCoords of points
1652     axis._point1UsrCoordsOrg = axis.point1.coords.usrCoords.slice();
1653     axis._point2UsrCoordsOrg = axis.point2.coords.usrCoords.slice();
1654 
1655     for (ancestor in axis.ancestors) {
1656         if (axis.ancestors.hasOwnProperty(ancestor)) {
1657             axis.ancestors[ancestor].type = Const.OBJECT_TYPE_AXISPOINT;
1658         }
1659     }
1660 
1661     // Create ticks
1662     // attrTicks = attr.ticks;
1663     if (Type.exists(attr.ticks.ticksdistance)) {
1664         ticksDist = attr.ticks.ticksdistance;
1665     } else if (Type.isArray(attr.ticks.ticks)) {
1666         ticksDist = attr.ticks.ticks;
1667     } else {
1668         ticksDist = 1.0;
1669     }
1670 
1671     /**
1672      * The ticks attached to the axis.
1673      * @memberOf Axis.prototype
1674      * @name defaultTicks
1675      * @type JXG.Ticks
1676      */
1677     axis.defaultTicks = board.create("ticks", [axis, ticksDist], attr.ticks);
1678     axis.defaultTicks.dump = false;
1679     axis.elType = "axis";
1680     axis.subs = {
1681         ticks: axis.defaultTicks
1682     };
1683     axis.inherits.push(axis.defaultTicks);
1684 
1685     axis.update = function () {
1686         var bbox,
1687             position, i,
1688             direction, horizontal, vertical,
1689             ticksAutoPos, ticksAutoPosThres, dist,
1690             anchor, left, right,
1691             distUsr,
1692             newPosP1, newPosP2,
1693             locationOrg,
1694             visLabel, anchr, off;
1695 
1696         if (!this.needsUpdate) {
1697             return this;
1698         }
1699 
1700         bbox = this.board.getBoundingBox();
1701         position = this.evalVisProp('position');
1702         direction = this.Direction();
1703         horizontal = this.isHorizontal();
1704         vertical = this.isVertical();
1705         ticksAutoPos = this.evalVisProp('ticksautopos');
1706         ticksAutoPosThres = this.evalVisProp('ticksautoposthreshold');
1707 
1708         if (horizontal) {
1709             ticksAutoPosThres = Type.parseNumber(ticksAutoPosThres, Math.abs(bbox[1] - bbox[3]), 1 / this.board.unitX) * this.board.unitX;
1710         } else if (vertical) {
1711             ticksAutoPosThres = Type.parseNumber(ticksAutoPosThres, Math.abs(bbox[1] - bbox[3]), 1 / this.board.unitY) * this.board.unitY;
1712         } else {
1713             ticksAutoPosThres = Type.parseNumber(ticksAutoPosThres, 1, 1);
1714         }
1715 
1716         anchor = this.evalVisProp('anchor');
1717         left = anchor.indexOf('left') > -1;
1718         right = anchor.indexOf('right') > -1;
1719 
1720         distUsr = this.evalVisProp('anchordist');
1721         if (horizontal) {
1722             distUsr = Type.parseNumber(distUsr, Math.abs(bbox[1] - bbox[3]), 1 / this.board.unitX);
1723         } else if (vertical) {
1724             distUsr = Type.parseNumber(distUsr, Math.abs(bbox[0] - bbox[2]), 1 / this.board.unitY);
1725         } else {
1726             distUsr = 0;
1727         }
1728 
1729         locationOrg = this.board.getPointLoc(this._point1UsrCoordsOrg, distUsr);
1730 
1731         // Set position of axis
1732         newPosP1 = this.point1.coords.usrCoords.slice();
1733         newPosP2 = this.point2.coords.usrCoords.slice();
1734 
1735         if (position === 'static' || (!vertical && !horizontal)) {
1736             // Do nothing
1737 
1738         } else if (position === 'fixed') {
1739             if (horizontal) { // direction[1] === 0
1740                 if ((direction[0] > 0 && right) || (direction[0] < 0 && left)) {
1741                     newPosP1[2] = bbox[3] + distUsr;
1742                     newPosP2[2] = bbox[3] + distUsr;
1743                 } else if ((direction[0] > 0 && left) || (direction[0] < 0 && right)) {
1744                     newPosP1[2] = bbox[1] - distUsr;
1745                     newPosP2[2] = bbox[1] - distUsr;
1746 
1747                 } else {
1748                     newPosP1 = this._point1UsrCoordsOrg.slice();
1749                     newPosP2 = this._point2UsrCoordsOrg.slice();
1750                 }
1751             }
1752             if (vertical) { // direction[0] === 0
1753                 if ((direction[1] > 0 && left) || (direction[1] < 0 && right)) {
1754                     newPosP1[1] = bbox[0] + distUsr;
1755                     newPosP2[1] = bbox[0] + distUsr;
1756 
1757                 } else if ((direction[1] > 0 && right) || (direction[1] < 0 && left)) {
1758                     newPosP1[1] = bbox[2] - distUsr;
1759                     newPosP2[1] = bbox[2] - distUsr;
1760 
1761                 } else {
1762                     newPosP1 = this._point1UsrCoordsOrg.slice();
1763                     newPosP2 = this._point2UsrCoordsOrg.slice();
1764                 }
1765             }
1766 
1767         } else if (position === 'sticky') {
1768             if (horizontal) { // direction[1] === 0
1769                 if (locationOrg[1] < 0 && ((direction[0] > 0 && right) || (direction[0] < 0 && left))) {
1770                     newPosP1[2] = bbox[3] + distUsr;
1771                     newPosP2[2] = bbox[3] + distUsr;
1772 
1773                 } else if (locationOrg[1] > 0 && ((direction[0] > 0 && left) || (direction[0] < 0 && right))) {
1774                     newPosP1[2] = bbox[1] - distUsr;
1775                     newPosP2[2] = bbox[1] - distUsr;
1776 
1777                 } else {
1778                     newPosP1 = this._point1UsrCoordsOrg.slice();
1779                     newPosP2 = this._point2UsrCoordsOrg.slice();
1780                 }
1781             }
1782             if (vertical) { // direction[0] === 0
1783                 if (locationOrg[0] < 0 && ((direction[1] > 0 && left) || (direction[1] < 0 && right))) {
1784                     newPosP1[1] = bbox[0] + distUsr;
1785                     newPosP2[1] = bbox[0] + distUsr;
1786 
1787                 } else if (locationOrg[0] > 0 && ((direction[1] > 0 && right) || (direction[1] < 0 && left))) {
1788                     newPosP1[1] = bbox[2] - distUsr;
1789                     newPosP2[1] = bbox[2] - distUsr;
1790 
1791                 } else {
1792                     newPosP1 = this._point1UsrCoordsOrg.slice();
1793                     newPosP2 = this._point2UsrCoordsOrg.slice();
1794                 }
1795             }
1796         }
1797 
1798         this.point1.setPositionDirectly(JXG.COORDS_BY_USER, newPosP1);
1799         this.point2.setPositionDirectly(JXG.COORDS_BY_USER, newPosP2);
1800 
1801         // Set position of tick labels
1802         if (Type.exists(this.defaultTicks)) {
1803             visLabel = this.defaultTicks.visProp.label;
1804             if (ticksAutoPos && (horizontal || vertical)) {
1805 
1806                 if (!Type.exists(visLabel._anchorx_org)) {
1807                     visLabel._anchorx_org = Type.def(visLabel.anchorx, this.board.options.text.anchorX);
1808                 }
1809                 if (!Type.exists(visLabel._anchory_org)) {
1810                     visLabel._anchory_org = Type.def(visLabel.anchory, this.board.options.text.anchorY);
1811                 }
1812                 if (!Type.exists(visLabel._offset_org)) {
1813                     visLabel._offset_org = visLabel.offset.slice();
1814                 }
1815 
1816                 off = visLabel.offset;
1817                 if (horizontal) {
1818                     dist = axis.point1.coords.scrCoords[2] - (this.board.canvasHeight * 0.5);
1819 
1820                     anchr = visLabel.anchory;
1821 
1822                     // The last position of the labels is stored in visLabel._side
1823                     if (dist < 0 && Math.abs(dist) > ticksAutoPosThres) {
1824                         // Put labels on top of the line
1825                         if (visLabel._side === 'bottom') {
1826                             // Switch position
1827                             if (visLabel.anchory === 'top') {
1828                                 anchr = 'bottom';
1829                             }
1830                             off[1] *= -1;
1831                             visLabel._side = 'top';
1832                         }
1833 
1834                     } else if (dist > 0 && Math.abs(dist) > ticksAutoPosThres) {
1835                         // Put labels below the line
1836                         if (visLabel._side === 'top') {
1837                             // Switch position
1838                             if (visLabel.anchory === 'bottom') {
1839                                 anchr = 'top';
1840                             }
1841                             off[1] *= -1;
1842                             visLabel._side = 'bottom';
1843                         }
1844 
1845                     } else {
1846                         // Put to original position
1847                         anchr = visLabel._anchory_org;
1848                         off = visLabel._offset_org.slice();
1849 
1850                         if (anchr === 'top') {
1851                             visLabel._side = 'bottom';
1852                         } else if (anchr === 'bottom') {
1853                             visLabel._side = 'top';
1854                         } else if (off[1] < 0) {
1855                             visLabel._side = 'bottom';
1856                         } else {
1857                             visLabel._side = 'top';
1858                         }
1859                     }
1860 
1861                     for (i = 0; i < axis.defaultTicks.labels.length; i++) {
1862                         this.defaultTicks.labels[i].visProp.anchory = anchr;
1863                     }
1864                     visLabel.anchory = anchr;
1865 
1866                 } else if (vertical) {
1867                     dist = axis.point1.coords.scrCoords[1] - (this.board.canvasWidth * 0.5);
1868 
1869                     if (dist < 0 && Math.abs(dist) > ticksAutoPosThres) {
1870                         // Put labels to the left of the line
1871                         if (visLabel._side === 'right') {
1872                             // Switch position
1873                             if (visLabel.anchorx === 'left') {
1874                                 anchr = 'right';
1875                             }
1876                             off[0] *= -1;
1877                             visLabel._side = 'left';
1878                         }
1879 
1880                     } else if (dist > 0 && Math.abs(dist) > ticksAutoPosThres) {
1881                         // Put labels to the right of the line
1882                         if (visLabel._side === 'left') {
1883                             // Switch position
1884                             if (visLabel.anchorx === 'right') {
1885                                 anchr = 'left';
1886                             }
1887                             off[0] *= -1;
1888                             visLabel._side = 'right';
1889                         }
1890 
1891                     } else {
1892                         // Put to original position
1893                         anchr = visLabel._anchorx_org;
1894                         off = visLabel._offset_org.slice();
1895 
1896                         if (anchr === 'left') {
1897                             visLabel._side = 'right';
1898                         } else if (anchr === 'right') {
1899                             visLabel._side = 'left';
1900                         } else if (off[0] < 0) {
1901                             visLabel._side = 'left';
1902                         } else {
1903                             visLabel._side = 'right';
1904                         }
1905                     }
1906 
1907                     for (i = 0; i < axis.defaultTicks.labels.length; i++) {
1908                         this.defaultTicks.labels[i].visProp.anchorx = anchr;
1909                     }
1910                     visLabel.anchorx = anchr;
1911                 }
1912                 visLabel.offset = off;
1913 
1914             } else {
1915                 delete visLabel._anchorx_org;
1916                 delete visLabel._anchory_org;
1917                 delete visLabel._offset_org;
1918             }
1919             this.defaultTicks.needsUpdate = true;
1920         }
1921 
1922         JXG.Line.prototype.update.call(this);
1923 
1924         return this;
1925     };
1926 
1927     return axis;
1928 };
1929 
1930 JXG.registerElement("axis", JXG.createAxis);
1931 
1932 /**
1933  * @class With the element tangent the slope of a line, circle, conic, turtle, or curve in a certain point can be visualized. A tangent is always constructed
1934  * by a point on a line, circle, or curve and describes the tangent in the point on that line, circle, or curve.
1935  * <p>
1936  * If the point is not on the object (line, circle, conic, curve, turtle) the output depends on the type of the object.
1937  * For conics and circles, the polar line will be constructed. For function graphs,
1938  * the tangent of the vertical projection of the point to the function graph is constructed. For all other objects, the tangent
1939  * in the orthogonal projection of the point to the object will be constructed.
1940  * @pseudo
1941  * @name Tangent
1942  * @augments JXG.Line
1943  * @constructor
1944  * @type JXG.Line
1945  * @throws {Exception} If the element cannot be constructed with the given parent objects an exception is thrown.
1946  * @param {Glider} g A glider on a line, circle, or curve.
1947  * @param {JXG.GeometryElement} [c] Optional element for which the tangent is constructed
1948  * @example
1949  * // Create a tangent providing a glider on a function graph
1950  *   var c1 = board.create('curve', [function(t){return t},function(t){return t*t*t;}]);
1951  *   var g1 = board.create('glider', [0.6, 1.2, c1]);
1952  *   var t1 = board.create('tangent', [g1]);
1953  * </pre><div class="jxgbox" id="JXG7b7233a0-f363-47dd-9df5-4018d0d17a98" style="width: 400px; height: 400px;"></div>
1954  * <script type="text/javascript">
1955  *   var tlex1_board = JXG.JSXGraph.initBoard('JXG7b7233a0-f363-47dd-9df5-4018d0d17a98', {boundingbox: [-6, 6, 6, -6], axis: true, showcopyright: false, shownavigation: false});
1956  *   var tlex1_c1 = tlex1_board.create('curve', [function(t){return t},function(t){return t*t*t;}]);
1957  *   var tlex1_g1 = tlex1_board.create('glider', [0.6, 1.2, tlex1_c1]);
1958  *   var tlex1_t1 = tlex1_board.create('tangent', [tlex1_g1]);
1959  * </script><pre>
1960  */
1961 JXG.createTangent = function (board, parents, attributes) {
1962     var p, c, j, el, tangent, attr,
1963         getCurveTangentDir,
1964         res, isTransformed,
1965         slides = [];
1966 
1967     if (parents.length === 1) {
1968         // One argument: glider on line, circle or curve
1969         p = parents[0];
1970         c = p.slideObject;
1971 
1972     } else if (parents.length === 2) {
1973         // Two arguments: (point,line|curve|circle|conic) or (line|curve|circle|conic,point).
1974         // In fact, for circles and conics it is the polar
1975         if (Type.isPoint(parents[0])) {
1976             p = parents[0];
1977             c = parents[1];
1978         } else if (Type.isPoint(parents[1])) {
1979             c = parents[0];
1980             p = parents[1];
1981         } else {
1982             throw new Error(
1983                 "JSXGraph: Can't create tangent with parent types '" +
1984                 typeof parents[0] +
1985                 "' and '" +
1986                 typeof parents[1] +
1987                 "'." +
1988                 "\nPossible parent types: [glider|point], [point,line|curve|circle|conic]"
1989             );
1990         }
1991     } else {
1992         throw new Error(
1993             "JSXGraph: Can't create tangent with parent types '" +
1994             typeof parents[0] +
1995             "' and '" +
1996             typeof parents[1] +
1997             "'." +
1998             "\nPossible parent types: [glider|point], [point,line|curve|circle|conic]"
1999         );
2000     }
2001 
2002     attr = Type.copyAttributes(attributes, board.options, 'tangent');
2003     if (c.elementClass === Const.OBJECT_CLASS_LINE) {
2004         tangent = board.create("line", [c.point1, c.point2], attr);
2005         tangent.glider = p;
2006     } else if (
2007         c.elementClass === Const.OBJECT_CLASS_CURVE &&
2008         c.type !== Const.OBJECT_TYPE_CONIC
2009     ) {
2010         res = c.getTransformationSource();
2011         isTransformed = res[0];
2012         if (isTransformed) {
2013             // Curve is result of a transformation
2014             // We recursively collect all curves from which
2015             // the curve is transformed.
2016             slides.push(c);
2017             while (res[0] && Type.exists(res[1]._transformationSource)) {
2018                 slides.push(res[1]);
2019                 res = res[1].getTransformationSource();
2020             }
2021         }
2022 
2023         if (c.evalVisProp('curvetype') !== "plot" || isTransformed) {
2024             // Functiongraph or parametric curve or
2025             // transformed curve thereof.
2026             tangent = board.create(
2027                 "line",
2028                 [
2029                     function () {
2030                         var g = c.X,
2031                             f = c.Y,
2032                             df, dg,
2033                             li, i, c_org, invMat, po,
2034                             t;
2035 
2036                         if (p.type === Const.OBJECT_TYPE_GLIDER) {
2037                             t = p.position;
2038                         } else if (c.evalVisProp('curvetype') === 'functiongraph') {
2039                             t = p.X();
2040                         } else {
2041                             t = Geometry.projectPointToCurve(p, c, board)[1];
2042                         }
2043 
2044                         // po are the coordinates of the point
2045                         // on the "original" curve. That is the curve or
2046                         // the original curve which is transformed (maybe multiple times)
2047                         // to this curve.
2048                         // t is the position of the point on the "original" curve
2049                         po = p.Coords(true);
2050                         if (isTransformed) {
2051                             c_org = slides[slides.length - 1]._transformationSource;
2052                             g = c_org.X;
2053                             f = c_org.Y;
2054                             for (i = 0; i < slides.length; i++) {
2055                                 slides[i].updateTransformMatrix();
2056                                 invMat = Mat.inverse(slides[i].transformMat);
2057                                 po = Mat.matVecMult(invMat, po);
2058                             }
2059 
2060                             if (p.type !== Const.OBJECT_TYPE_GLIDER) {
2061                                 po[1] /= po[0];
2062                                 po[2] /= po[0];
2063                                 po[0] /= po[0];
2064                                 t = Geometry.projectCoordsToCurve(po[1], po[2], 0, c_org, board)[1];
2065                             }
2066                         }
2067 
2068                         // li are the coordinates of the line on the "original" curve
2069                         df = Numerics.D(f)(t);
2070                         dg = Numerics.D(g)(t);
2071                         li = [
2072                             -po[1] * df + po[2] * dg,
2073                             po[0] * df,
2074                             -po[0] * dg
2075                         ];
2076 
2077                         if (isTransformed) {
2078                             // Transform the line to the transformed curve
2079                             for (i = slides.length - 1; i >= 0; i--) {
2080                                 invMat = Mat.transpose(Mat.inverse(slides[i].transformMat));
2081                                 li = Mat.matVecMult(invMat, li);
2082                             }
2083                         }
2084 
2085                         return li;
2086                     }
2087                 ],
2088                 attr
2089             );
2090 
2091             p.addChild(tangent);
2092             // this is required for the geogebra reader to display a slope
2093             tangent.glider = p;
2094         } else {
2095             // curveType 'plot': discrete data
2096             /**
2097              * @ignore
2098              *
2099              * In case of bezierDegree == 1:
2100              * Find two points p1, p2 enclosing the glider.
2101              * Then the equation of the line segment is: 0 = y*(x1-x2) + x*(y2-y1) + y1*x2-x1*y2,
2102              * which is the cross product of p1 and p2.
2103              *
2104              * In case of bezierDegree === 3:
2105              * The slope dy / dx of the tangent is determined. Then the
2106              * tangent is computed as cross product between
2107              * the glider p and [1, p.X() + dx, p.Y() + dy]
2108              *
2109              */
2110             getCurveTangentDir = function (position, c, num) {
2111                 var i = Math.floor(position),
2112                     p1, p2, t, A, B, C, D, dx, dy, d,
2113                     points, le;
2114 
2115                 if (c.bezierDegree === 1) {
2116                     if (i === c.numberPoints - 1) {
2117                         i--;
2118                     }
2119                 } else if (c.bezierDegree === 3) {
2120                     // i is start of the Bezier segment
2121                     // t is the position in the Bezier segment
2122                     if (c.elType === 'sector') {
2123                         points = c.points.slice(3, c.numberPoints - 3);
2124                         le = points.length;
2125                     } else {
2126                         points = c.points;
2127                         le = points.length;
2128                     }
2129                     i = Math.floor((position * (le - 1)) / 3) * 3;
2130                     t = (position * (le - 1) - i) / 3;
2131                     if (i >= le - 1) {
2132                         i = le - 4;
2133                         t = 1;
2134                     }
2135                 } else {
2136                     return 0;
2137                 }
2138 
2139                 if (i < 0) {
2140                     return 1;
2141                 }
2142 
2143                 // The curve points are transformed (if there is a transformation)
2144                 // c.X(i) is not transformed.
2145                 if (c.bezierDegree === 1) {
2146                     p1 = c.points[i].usrCoords;
2147                     p2 = c.points[i + 1].usrCoords;
2148                 } else {
2149                     A = points[i].usrCoords;
2150                     B = points[i + 1].usrCoords;
2151                     C = points[i + 2].usrCoords;
2152                     D = points[i + 3].usrCoords;
2153                     dx = (1 - t) * (1 - t) * (B[1] - A[1]) +
2154                         2 * (1 - t) * t * (C[1] - B[1]) +
2155                         t * t * (D[1] - C[1]);
2156                     dy = (1 - t) * (1 - t) * (B[2] - A[2]) +
2157                         2 * (1 - t) * t * (C[2] - B[2]) +
2158                         t * t * (D[2] - C[2]);
2159                     d = Mat.hypot(dx, dy);
2160                     dx /= d;
2161                     dy /= d;
2162                     p1 = p.coords.usrCoords;
2163                     p2 = [1, p1[1] + dx, p1[2] + dy];
2164                 }
2165 
2166                 switch (num) {
2167                     case 0:
2168                         return p1[2] * p2[1] - p1[1] * p2[2];
2169                     case 1:
2170                         return p2[2] - p1[2];
2171                     case 2:
2172                         return p1[1] - p2[1];
2173                     default:
2174                         return [
2175                             p1[2] * p2[1] - p1[1] * p2[2],
2176                             p2[2] - p1[2],
2177                             p1[1] - p2[1]
2178                         ];
2179                 }
2180             };
2181 
2182             tangent = board.create(
2183                 "line",
2184                 [
2185                     function () {
2186                         var t;
2187 
2188                         if (p.type === Const.OBJECT_TYPE_GLIDER) {
2189                             t = p.position;
2190                         } else {
2191                             t = Geometry.projectPointToCurve(p, c, board)[1];
2192                         }
2193 
2194                         return getCurveTangentDir(t, c);
2195                     }
2196                 ],
2197                 attr
2198             );
2199 
2200             p.addChild(tangent);
2201             // this is required for the geogebra reader to display a slope
2202             tangent.glider = p;
2203         }
2204     } else if (c.type === Const.OBJECT_TYPE_TURTLE) {
2205         tangent = board.create(
2206             "line",
2207             [
2208                 function () {
2209                     var i, t;
2210                     if (p.type === Const.OBJECT_TYPE_GLIDER) {
2211                         t = p.position;
2212                     } else {
2213                         t = Geometry.projectPointToTurtle(p, c, board)[1];
2214                     }
2215 
2216                     i = Math.floor(t);
2217 
2218                     // run through all curves of this turtle
2219                     for (j = 0; j < c.objects.length; j++) {
2220                         el = c.objects[j];
2221 
2222                         if (el.type === Const.OBJECT_TYPE_CURVE) {
2223                             if (i < el.numberPoints) {
2224                                 break;
2225                             }
2226 
2227                             i -= el.numberPoints;
2228                         }
2229                     }
2230 
2231                     if (i === el.numberPoints - 1) {
2232                         i--;
2233                     }
2234 
2235                     if (i < 0) {
2236                         return [1, 0, 0];
2237                     }
2238 
2239                     return [
2240                         el.Y(i) * el.X(i + 1) - el.X(i) * el.Y(i + 1),
2241                         el.Y(i + 1) - el.Y(i),
2242                         el.X(i) - el.X(i + 1)
2243                     ];
2244                 }
2245             ],
2246             attr
2247         );
2248         p.addChild(tangent);
2249 
2250         // this is required for the geogebra reader to display a slope
2251         tangent.glider = p;
2252     } else if (
2253         c.elementClass === Const.OBJECT_CLASS_CIRCLE ||
2254         c.type === Const.OBJECT_TYPE_CONIC
2255     ) {
2256         // If p is not on c, the tangent is the polar.
2257         // This construction should work on conics, too. p has to lie on c.
2258         tangent = board.create(
2259             "line",
2260             [
2261                 function () {
2262                     return Mat.matVecMult(c.quadraticform, p.coords.usrCoords);
2263                 }
2264             ],
2265             attr
2266         );
2267 
2268         p.addChild(tangent);
2269         // this is required for the geogebra reader to display a slope
2270         tangent.glider = p;
2271     }
2272 
2273     if (!Type.exists(tangent)) {
2274         throw new Error("JSXGraph: Couldn't create tangent with the given parents.");
2275     }
2276 
2277     tangent.elType = "tangent";
2278     tangent.type = Const.OBJECT_TYPE_TANGENT;
2279     tangent.setParents(parents);
2280 
2281     return tangent;
2282 };
2283 
2284 /**
2285  * @class Constructs a normal.
2286  * @pseudo
2287  * @description A normal is a line through a given point on an element of type line, circle, curve, or turtle and orthogonal to that object.
2288  * @constructor
2289  * @name Normal
2290  * @type JXG.Line
2291  * @augments JXG.Line
2292  * @throws {Error} If the element cannot be constructed with the given parent objects an exception is thrown.
2293  * @param {JXG.Line,JXG.Circle,JXG.Curve,JXG.Turtle_JXG.Point} o,p The constructed line contains p which lies on the object and is orthogonal
2294  * to the tangent to the object in the given point.
2295  * @param {Glider} p Works like above, however the object is given by {@link JXG.CoordsElement#slideObject}.
2296  * @example
2297  * // Create a normal to a circle.
2298  * var p1 = board.create('point', [2.0, 2.0]);
2299  * var p2 = board.create('point', [3.0, 2.0]);
2300  * var c1 = board.create('circle', [p1, p2]);
2301  *
2302  * var norm1 = board.create('normal', [c1, p2]);
2303  * </pre><div class="jxgbox" id="JXG4154753d-3d29-40fb-a860-0b08aa4f3743" style="width: 400px; height: 400px;"></div>
2304  * <script type="text/javascript">
2305  *   var nlex1_board = JXG.JSXGraph.initBoard('JXG4154753d-3d29-40fb-a860-0b08aa4f3743', {boundingbox: [-1, 9, 9, -1], axis: true, showcopyright: false, shownavigation: false});
2306  *   var nlex1_p1 = nlex1_board.create('point', [2.0, 2.0]);
2307  *   var nlex1_p2 = nlex1_board.create('point', [3.0, 2.0]);
2308  *   var nlex1_c1 = nlex1_board.create('circle', [nlex1_p1, nlex1_p2]);
2309  *
2310  *   // var nlex1_p3 = nlex1_board.create('point', [1.0, 2.0]);
2311  *   var nlex1_norm1 = nlex1_board.create('normal', [nlex1_c1, nlex1_p2]);
2312  * </script><pre>
2313  */
2314 JXG.createNormal = function (board, parents, attributes) {
2315     var p, c, l, i, attr, pp, attrp,
2316         getCurveNormalDir,
2317         res, isTransformed,
2318         slides = [];
2319 
2320     for (i = 0; i < parents.length; ++i) {
2321         parents[i] = board.select(parents[i]);
2322     }
2323     // One arguments: glider on line, circle or curve
2324     if (parents.length === 1) {
2325         p = parents[0];
2326         c = p.slideObject;
2327         // Two arguments: (point,line), (point,circle), (line,point) or (circle,point)
2328     } else if (parents.length === 2) {
2329         if (Type.isPointType(board, parents[0])) {
2330             p = Type.providePoints(board, [parents[0]], attributes, "point")[0];
2331             c = parents[1];
2332         } else if (Type.isPointType(board, parents[1])) {
2333             c = parents[0];
2334             p = Type.providePoints(board, [parents[1]], attributes, "point")[0];
2335         } else {
2336             throw new Error(
2337                 "JSXGraph: Can't create normal with parent types '" +
2338                 typeof parents[0] +
2339                 "' and '" +
2340                 typeof parents[1] +
2341                 "'." +
2342                 "\nPossible parent types: [point,line], [point,circle], [glider]"
2343             );
2344         }
2345     } else {
2346         throw new Error(
2347             "JSXGraph: Can't create normal with parent types '" +
2348             typeof parents[0] +
2349             "' and '" +
2350             typeof parents[1] +
2351             "'." +
2352             "\nPossible parent types: [point,line], [point,circle], [glider]"
2353         );
2354     }
2355 
2356     attr = Type.copyAttributes(attributes, board.options, "normal");
2357     if (c.elementClass === Const.OBJECT_CLASS_LINE) {
2358         // Private point
2359         attrp = Type.copyAttributes(attributes, board.options, "normal", "point");
2360         pp = board.create(
2361             "point",
2362             [
2363                 function () {
2364                     var p = Mat.crossProduct([1, 0, 0], c.stdform);
2365                     return [p[0], -p[2], p[1]];
2366                 }
2367             ],
2368             attrp
2369         );
2370         pp.isDraggable = true;
2371 
2372         l = board.create("line", [p, pp], attr);
2373 
2374         /**
2375          * A helper point used to create a normal to a {@link JXG.Line} object. For normals to circles or curves this
2376          * element is <tt>undefined</tt>.
2377          * @type JXG.Point
2378          * @name point
2379          * @memberOf Normal.prototype
2380          */
2381         l.point = pp;
2382         l.subs = {
2383             point: pp
2384         };
2385         l.inherits.push(pp);
2386     } else if (c.elementClass === Const.OBJECT_CLASS_CIRCLE) {
2387         l = board.create("line", [c.midpoint, p], attr);
2388     } else if (c.elementClass === Const.OBJECT_CLASS_CURVE) {
2389         res = c.getTransformationSource();
2390         isTransformed = res[0];
2391         if (isTransformed) {
2392             // Curve is result of a transformation
2393             // We recursively collect all curves from which
2394             // the curve is transformed.
2395             slides.push(c);
2396             while (res[0] && Type.exists(res[1]._transformationSource)) {
2397                 slides.push(res[1]);
2398                 res = res[1].getTransformationSource();
2399             }
2400         }
2401 
2402         if (c.evalVisProp('curvetype') !== "plot" || isTransformed) {
2403             // Functiongraph or parametric curve or
2404             // transformed curve thereof.
2405             l = board.create(
2406                 "line",
2407                 [
2408                     function () {
2409                         var g = c.X,
2410                             f = c.Y,
2411                             df, dg,
2412                             li, i, c_org, invMat, po,
2413                             t;
2414 
2415                         if (p.type === Const.OBJECT_TYPE_GLIDER) {
2416                             t = p.position;
2417                         } else if (c.evalVisProp('curvetype') === 'functiongraph') {
2418                             t = p.X();
2419                         } else {
2420                             t = Geometry.projectPointToCurve(p, c, board)[1];
2421                         }
2422 
2423                         // po are the coordinates of the point
2424                         // on the "original" curve. That is the curve or
2425                         // the original curve which is transformed (maybe multiple times)
2426                         // to this curve.
2427                         // t is the position of the point on the "original" curve
2428                         po = p.Coords(true);
2429                         if (isTransformed) {
2430                             c_org = slides[slides.length - 1]._transformationSource;
2431                             g = c_org.X;
2432                             f = c_org.Y;
2433                             for (i = 0; i < slides.length; i++) {
2434                                 slides[i].updateTransformMatrix();
2435                                 invMat = Mat.inverse(slides[i].transformMat);
2436                                 po = Mat.matVecMult(invMat, po);
2437                             }
2438 
2439                             if (p.type !== Const.OBJECT_TYPE_GLIDER) {
2440                                 po[1] /= po[0];
2441                                 po[2] /= po[0];
2442                                 po[0] /= po[0];
2443                                 t = Geometry.projectCoordsToCurve(po[1], po[2], 0, c_org, board)[1];
2444                             }
2445                         }
2446 
2447                         df = Numerics.D(f)(t);
2448                         dg = Numerics.D(g)(t);
2449                         li = [
2450                             -po[1] * dg - po[2] * df,
2451                             po[0] * dg,
2452                             po[0] * df
2453                         ];
2454 
2455                         if (isTransformed) {
2456                             // Transform the line to the transformed curve
2457                             for (i = slides.length - 1; i >= 0; i--) {
2458                                 invMat = Mat.transpose(Mat.inverse(slides[i].transformMat));
2459                                 li = Mat.matVecMult(invMat, li);
2460                             }
2461                         }
2462 
2463                         return li;
2464                     }
2465                 ],
2466                 attr
2467             );
2468         } else {
2469             // curveType 'plot': discrete data
2470             getCurveNormalDir = function (position, c, num) {
2471                 var i = Math.floor(position),
2472                     lbda,
2473                     p1, p2, t, A, B, C, D, dx, dy, d,
2474                     li, p_org, pp,
2475                     points, le;
2476 
2477 
2478                 if (c.bezierDegree === 1) {
2479                     if (i === c.numberPoints - 1) {
2480                         i--;
2481                     }
2482                     t = position;
2483                 } else if (c.bezierDegree === 3) {
2484                     // i is start of the Bezier segment
2485                     // t is the position in the Bezier segment
2486                     if (c.elType === 'sector') {
2487                         points = c.points.slice(3, c.numberPoints - 3);
2488                         le = points.length;
2489                     } else {
2490                         points = c.points;
2491                         le = points.length;
2492                     }
2493                     i = Math.floor((position * (le - 1)) / 3) * 3;
2494                     t = (position * (le - 1) - i) / 3;
2495                     if (i >= le - 1) {
2496                         i = le - 4;
2497                         t = 1;
2498                     }
2499                 } else {
2500                     return 0;
2501                 }
2502 
2503                 if (i < 0) {
2504                     return 1;
2505                 }
2506 
2507                 lbda = t - i;
2508                 if (c.bezierDegree === 1) {
2509                     p1 = c.points[i].usrCoords;
2510                     p2 = c.points[i + 1].usrCoords;
2511                     p_org = [
2512                         p1[0] + lbda * (p2[0] - p1[0]),
2513                         p1[1] + lbda * (p2[1] - p1[1]),
2514                         p1[2] + lbda * (p2[2] - p1[2])
2515                     ];
2516                     li = Mat.crossProduct(p1, p2);
2517                     pp = Mat.crossProduct([1, 0, 0], li);
2518                     pp = [pp[0], -pp[2], pp[1]];
2519                     li = Mat.crossProduct(p_org, pp);
2520 
2521                 } else {
2522                     A = points[i].usrCoords;
2523                     B = points[i + 1].usrCoords;
2524                     C = points[i + 2].usrCoords;
2525                     D = points[i + 3].usrCoords;
2526                     dx =
2527                         (1 - t) * (1 - t) * (B[1] - A[1]) +
2528                         2 * (1 - t) * t * (C[1] - B[1]) +
2529                         t * t * (D[1] - C[1]);
2530                     dy =
2531                         (1 - t) * (1 - t) * (B[2] - A[2]) +
2532                         2 * (1 - t) * t * (C[2] - B[2]) +
2533                         t * t * (D[2] - C[2]);
2534                     d = Mat.hypot(dx, dy);
2535                     dx /= d;
2536                     dy /= d;
2537                     p1 = p.coords.usrCoords;
2538                     p2 = [1, p1[1] - dy, p1[2] + dx];
2539 
2540                     li = [
2541                         p1[2] * p2[1] - p1[1] * p2[2],
2542                         p2[2] - p1[2],
2543                         p1[1] - p2[1]
2544                     ];
2545                 }
2546 
2547                 switch (num) {
2548                     case 0:
2549                         return li[0];
2550                     case 1:
2551                         return li[1];
2552                     case 2:
2553                         return li[2];
2554                     default:
2555                         return li;
2556                 }
2557             };
2558 
2559             l = board.create(
2560                 "line",
2561                 [
2562                     function () {
2563                         var t;
2564 
2565                         if (p.type === Const.OBJECT_TYPE_GLIDER) {
2566                             t = p.position;
2567                         } else {
2568                             t = Geometry.projectPointToCurve(p, c, board)[1];
2569                         }
2570 
2571                         return getCurveNormalDir(t, c);
2572                     }
2573                 ],
2574                 attr
2575             );
2576             p.addChild(l);
2577             l.glider = p;
2578         }
2579     } else if (c.type === Const.OBJECT_TYPE_TURTLE) {
2580         l = board.create(
2581             "line",
2582             [
2583                 function () {
2584                     var el,
2585                         j,
2586                         i = Math.floor(p.position),
2587                         lbda = p.position - i;
2588 
2589                     // run through all curves of this turtle
2590                     for (j = 0; j < c.objects.length; j++) {
2591                         el = c.objects[j];
2592 
2593                         if (el.type === Const.OBJECT_TYPE_CURVE) {
2594                             if (i < el.numberPoints) {
2595                                 break;
2596                             }
2597 
2598                             i -= el.numberPoints;
2599                         }
2600                     }
2601 
2602                     if (i === el.numberPoints - 1) {
2603                         i -= 1;
2604                         lbda = 1;
2605                     }
2606 
2607                     if (i < 0) {
2608                         return 1;
2609                     }
2610 
2611                     return (
2612                         (el.Y(i) + lbda * (el.Y(i + 1) - el.Y(i))) * (el.Y(i) - el.Y(i + 1)) -
2613                         (el.X(i) + lbda * (el.X(i + 1) - el.X(i))) * (el.X(i + 1) - el.X(i))
2614                     );
2615                 },
2616                 function () {
2617                     var el,
2618                         j,
2619                         i = Math.floor(p.position);
2620 
2621                     // run through all curves of this turtle
2622                     for (j = 0; j < c.objects.length; j++) {
2623                         el = c.objects[j];
2624                         if (el.type === Const.OBJECT_TYPE_CURVE) {
2625                             if (i < el.numberPoints) {
2626                                 break;
2627                             }
2628 
2629                             i -= el.numberPoints;
2630                         }
2631                     }
2632 
2633                     if (i === el.numberPoints - 1) {
2634                         i -= 1;
2635                     }
2636 
2637                     if (i < 0) {
2638                         return 0;
2639                     }
2640 
2641                     return el.X(i + 1) - el.X(i);
2642                 },
2643                 function () {
2644                     var el,
2645                         j,
2646                         i = Math.floor(p.position);
2647 
2648                     // run through all curves of this turtle
2649                     for (j = 0; j < c.objects.length; j++) {
2650                         el = c.objects[j];
2651                         if (el.type === Const.OBJECT_TYPE_CURVE) {
2652                             if (i < el.numberPoints) {
2653                                 break;
2654                             }
2655 
2656                             i -= el.numberPoints;
2657                         }
2658                     }
2659 
2660                     if (i === el.numberPoints - 1) {
2661                         i -= 1;
2662                     }
2663 
2664                     if (i < 0) {
2665                         return 0;
2666                     }
2667 
2668                     return el.Y(i + 1) - el.Y(i);
2669                 }
2670             ],
2671             attr
2672         );
2673     } else {
2674         throw new Error(
2675             "JSXGraph: Can't create normal with parent types '" +
2676             typeof parents[0] +
2677             "' and '" +
2678             typeof parents[1] +
2679             "'." +
2680             "\nPossible parent types: [point,line], [point,circle], [glider]"
2681         );
2682     }
2683 
2684     l.elType = "normal";
2685     l.setParents(parents);
2686 
2687     if (Type.exists(p._is_new)) {
2688         l.addChild(p);
2689         delete p._is_new;
2690     } else {
2691         p.addChild(l);
2692     }
2693     c.addChild(l);
2694 
2695     return l;
2696 };
2697 
2698 /**
2699  * @class This element is used to provide a constructor for the radical axis with respect to two circles with distinct centers.
2700  * The angular bisector of the polar lines of the circle centers with respect to the other circle is always the radical axis.
2701  * The radical axis passes through the intersection points when the circles intersect.
2702  * When a circle about the midpoint of circle centers, passing through the circle centers, intersects the circles, the polar lines pass through those intersection points.
2703  * @pseudo
2704  * @name RadicalAxis
2705  * @augments JXG.Line
2706  * @constructor
2707  * @type JXG.Line
2708  * @throws {Exception} If the element cannot be constructed with the given parent objects an exception is thrown.
2709  * @param {JXG.Circle} circle Circle one of the two respective circles.
2710  * @param {JXG.Circle} circle Circle the other of the two respective circles.
2711  * @example
2712  * // Create the radical axis line with respect to two circles
2713  *   var board = JXG.JSXGraph.initBoard('7b7233a0-f363-47dd-9df5-5018d0d17a98', {boundingbox: [-1, 9, 9, -1], axis: true, showcopyright: false, shownavigation: false});
2714  *   var p1 = board.create('point', [2, 3]);
2715  *   var p2 = board.create('point', [1, 4]);
2716  *   var c1 = board.create('circle', [p1, p2]);
2717  *   var p3 = board.create('point', [6, 5]);
2718  *   var p4 = board.create('point', [8, 6]);
2719  *   var c2 = board.create('circle', [p3, p4]);
2720  *   var r1 = board.create('radicalaxis', [c1, c2]);
2721  * </pre><div class="jxgbox" id="JXG7b7233a0-f363-47dd-9df5-5018d0d17a98" class="jxgbox" style="width:400px; height:400px;"></div>
2722  * <script type='text/javascript'>
2723  *   var rlex1_board = JXG.JSXGraph.initBoard('JXG7b7233a0-f363-47dd-9df5-5018d0d17a98', {boundingbox: [-1, 9, 9, -1], axis: true, showcopyright: false, shownavigation: false});
2724  *   var rlex1_p1 = rlex1_board.create('point', [2, 3]);
2725  *   var rlex1_p2 = rlex1_board.create('point', [1, 4]);
2726  *   var rlex1_c1 = rlex1_board.create('circle', [rlex1_p1, rlex1_p2]);
2727  *   var rlex1_p3 = rlex1_board.create('point', [6, 5]);
2728  *   var rlex1_p4 = rlex1_board.create('point', [8, 6]);
2729  *   var rlex1_c2 = rlex1_board.create('circle', [rlex1_p3, rlex1_p4]);
2730  *   var rlex1_r1 = rlex1_board.create('radicalaxis', [rlex1_c1, rlex1_c2]);
2731  * </script><pre>
2732  */
2733 JXG.createRadicalAxis = function (board, parents, attributes) {
2734     var el, el1, el2;
2735 
2736     if (
2737         parents.length !== 2 ||
2738         parents[0].elementClass !== Const.OBJECT_CLASS_CIRCLE ||
2739         parents[1].elementClass !== Const.OBJECT_CLASS_CIRCLE
2740     ) {
2741         // Failure
2742         throw new Error(
2743             "JSXGraph: Can't create 'radical axis' with parent types '" +
2744             typeof parents[0] +
2745             "' and '" +
2746             typeof parents[1] +
2747             "'." +
2748             "\nPossible parent type: [circle,circle]"
2749         );
2750     }
2751 
2752     el1 = board.select(parents[0]);
2753     el2 = board.select(parents[1]);
2754 
2755     el = board.create(
2756         "line",
2757         [
2758             function () {
2759                 var a = el1.stdform,
2760                     b = el2.stdform;
2761 
2762                 return Mat.matVecMult(Mat.transpose([a.slice(0, 3), b.slice(0, 3)]), [
2763                     b[3],
2764                     -a[3]
2765                 ]);
2766             }
2767         ],
2768         attributes
2769     );
2770 
2771     el.elType = "radicalaxis";
2772     el.setParents([el1.id, el2.id]);
2773 
2774     el1.addChild(el);
2775     el2.addChild(el);
2776 
2777     return el;
2778 };
2779 
2780 /**
2781  * @class This element is used to provide a constructor for the polar line of a point with respect to a conic or a circle.
2782  * @pseudo
2783  * @description The polar line is the unique reciprocal relationship of a point with respect to a conic.
2784  * The lines through the intersections of a conic and the polar line of a point with respect to that conic and through that point are tangent to the conic.
2785  * A point on a conic has the polar line of that point with respect to that conic as the tangent line to that conic at that point.
2786  * See {@link https://en.wikipedia.org/wiki/Pole_and_polar} for more information on pole and polar.
2787  * @name PolarLine
2788  * @augments JXG.Line
2789  * @constructor
2790  * @type JXG.Line
2791  * @throws {Exception} If the element cannot be constructed with the given parent objects an exception is thrown.
2792  * @param {JXG.Conic,JXG.Circle_JXG.Point} el1,el2 or
2793  * @param {JXG.Point_JXG.Conic,JXG.Circle} el1,el2 The result will be the polar line of the point with respect to the conic or the circle.
2794  * @example
2795  * // Create the polar line of a point with respect to a conic
2796  * var p1 = board.create('point', [-1, 2]);
2797  * var p2 = board.create('point', [ 1, 4]);
2798  * var p3 = board.create('point', [-1,-2]);
2799  * var p4 = board.create('point', [ 0, 0]);
2800  * var p5 = board.create('point', [ 4,-2]);
2801  * var c1 = board.create('conic',[p1,p2,p3,p4,p5]);
2802  * var p6 = board.create('point', [-1, 1]);
2803  * var l1 = board.create('polarline', [c1, p6]);
2804  * </pre><div class="jxgbox" id="JXG7b7233a0-f363-47dd-9df5-6018d0d17a98" class="jxgbox" style="width:400px; height:400px;"></div>
2805  * <script type='text/javascript'>
2806  * var plex1_board = JXG.JSXGraph.initBoard('JXG7b7233a0-f363-47dd-9df5-6018d0d17a98', {boundingbox: [-3, 5, 5, -3], axis: true, showcopyright: false, shownavigation: false});
2807  * var plex1_p1 = plex1_board.create('point', [-1, 2]);
2808  * var plex1_p2 = plex1_board.create('point', [ 1, 4]);
2809  * var plex1_p3 = plex1_board.create('point', [-1,-2]);
2810  * var plex1_p4 = plex1_board.create('point', [ 0, 0]);
2811  * var plex1_p5 = plex1_board.create('point', [ 4,-2]);
2812  * var plex1_c1 = plex1_board.create('conic',[plex1_p1,plex1_p2,plex1_p3,plex1_p4,plex1_p5]);
2813  * var plex1_p6 = plex1_board.create('point', [-1, 1]);
2814  * var plex1_l1 = plex1_board.create('polarline', [plex1_c1, plex1_p6]);
2815  * </script><pre>
2816  * @example
2817  * // Create the polar line of a point with respect to a circle.
2818  * var p1 = board.create('point', [ 1, 1]);
2819  * var p2 = board.create('point', [ 2, 3]);
2820  * var c1 = board.create('circle',[p1,p2]);
2821  * var p3 = board.create('point', [ 6, 6]);
2822  * var l1 = board.create('polarline', [c1, p3]);
2823  * </pre><div class="jxgbox" id="JXG7b7233a0-f363-47dd-9df5-7018d0d17a98" class="jxgbox" style="width:400px; height:400px;"></div>
2824  * <script type='text/javascript'>
2825  * var plex2_board = JXG.JSXGraph.initBoard('JXG7b7233a0-f363-47dd-9df5-7018d0d17a98', {boundingbox: [-3, 7, 7, -3], axis: true, showcopyright: false, shownavigation: false});
2826  * var plex2_p1 = plex2_board.create('point', [ 1, 1]);
2827  * var plex2_p2 = plex2_board.create('point', [ 2, 3]);
2828  * var plex2_c1 = plex2_board.create('circle',[plex2_p1,plex2_p2]);
2829  * var plex2_p3 = plex2_board.create('point', [ 6, 6]);
2830  * var plex2_l1 = plex2_board.create('polarline', [plex2_c1, plex2_p3]);
2831  * </script><pre>
2832  */
2833 JXG.createPolarLine = function (board, parents, attributes) {
2834     var el,
2835         el1,
2836         el2,
2837         firstParentIsConic,
2838         secondParentIsConic,
2839         firstParentIsPoint,
2840         secondParentIsPoint;
2841 
2842     if (parents.length > 1) {
2843         firstParentIsConic =
2844             parents[0].type === Const.OBJECT_TYPE_CONIC ||
2845             parents[0].elementClass === Const.OBJECT_CLASS_CIRCLE;
2846         secondParentIsConic =
2847             parents[1].type === Const.OBJECT_TYPE_CONIC ||
2848             parents[1].elementClass === Const.OBJECT_CLASS_CIRCLE;
2849 
2850         firstParentIsPoint = Type.isPoint(parents[0]);
2851         secondParentIsPoint = Type.isPoint(parents[1]);
2852     }
2853 
2854     if (
2855         parents.length !== 2 ||
2856         !(
2857             (firstParentIsConic && secondParentIsPoint) ||
2858             (firstParentIsPoint && secondParentIsConic)
2859         )
2860     ) {
2861         // Failure
2862         throw new Error(
2863             "JSXGraph: Can't create 'polar line' with parent types '" +
2864             typeof parents[0] +
2865             "' and '" +
2866             typeof parents[1] +
2867             "'." +
2868             "\nPossible parent type: [conic|circle,point], [point,conic|circle]"
2869         );
2870     }
2871 
2872     if (secondParentIsPoint) {
2873         el1 = board.select(parents[0]);
2874         el2 = board.select(parents[1]);
2875     } else {
2876         el1 = board.select(parents[1]);
2877         el2 = board.select(parents[0]);
2878     }
2879 
2880     // Polar lines have been already provided in the tangent element.
2881     el = board.create("tangent", [el1, el2], attributes);
2882 
2883     el.elType = "polarline";
2884     return el;
2885 };
2886 
2887 /**
2888  *
2889  * @class This element is used to provide a constructor for the tangent through a point to a conic or a circle.
2890  * @pseudo
2891  * @description Construct the tangent line through a point to a conic or a circle. There will be either two, one or no
2892  * such tangent, depending if the point is outside of the conic, on the conic, or inside of the conic.
2893  * Similar to the intersection of a line with a circle, the specific tangent can be chosen with a third (optional) parameter
2894  * <i>number</i>.
2895  * <p>
2896  * Attention: from a technical point of view, the point from which the tangent to the conic/circle is constructed is not an element of
2897  * the tangent line.
2898  * @name TangentTo
2899  * @augments JXG.Line
2900  * @constructor
2901  * @type JXG.Line
2902  * @throws {Exception} If the element cannot be constructed with the given parent objects an exception is thrown.
2903  * @param {JXG.Conic,JXG.Circle_JXG.Point_Number} conic,point,[number=0] The result will be the tangent line through
2904  * the point with respect to the conic or circle.
2905  *
2906  * @example
2907  *  var c = board.create('circle', [[3, 0], [3, 4]]);
2908  *  var p = board.create('point', [0, 6]);
2909  *  var t0 = board.create('tangentto', [c, p, 0], { color: 'black', polar: {visible: true}, point: {visible: true} });
2910  *  var t1 = board.create('tangentto', [c, p, 1], { color: 'black' });
2911  *
2912  * </pre><div id="JXGd4b359c7-3a29-44c3-a19d-d51b42a00c8b" class="jxgbox" style="width: 300px; height: 300px;"></div>
2913  * <script type="text/javascript">
2914  *     (function() {
2915  *         var board = JXG.JSXGraph.initBoard('JXGd4b359c7-3a29-44c3-a19d-d51b42a00c8b',
2916  *             {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false});
2917  *             var c = board.create('circle', [[3, 0], [3, 4]]);
2918  *             var p = board.create('point', [0, 6]);
2919  *             var t0 = board.create('tangentto', [c, p, 0], { color: 'black', polar: {visible: true}, point: {visible: true} });
2920  *             var t1 = board.create('tangentto', [c, p, 1], { color: 'black' });
2921  *
2922  *     })();
2923  *
2924  * </script><pre>
2925  *
2926  * @example
2927  *  var p = board.create('point', [0, 6]);
2928  *  var ell = board.create('ellipse', [[-5, 1], [-2, -1], [-3, 2]]);
2929  *  var t0 = board.create('tangentto', [ell, p, 0]);
2930  *  var t1 = board.create('tangentto', [ell, p, 1]);
2931  *
2932  * </pre><div id="JXG6e625663-1c3e-4e08-a9df-574972a374e8" class="jxgbox" style="width: 300px; height: 300px;"></div>
2933  * <script type="text/javascript">
2934  *     (function() {
2935  *         var board = JXG.JSXGraph.initBoard('JXG6e625663-1c3e-4e08-a9df-574972a374e8',
2936  *             {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false});
2937  *             var p = board.create('point', [0, 6]);
2938  *             var ell = board.create('ellipse', [[-5, 1], [-2, -1], [-3, 2]]);
2939  *             var t0 = board.create('tangentto', [ell, p, 0]);
2940  *             var t1 = board.create('tangentto', [ell, p, 1]);
2941  *
2942  *     })();
2943  *
2944  * </script><pre>
2945  *
2946  */
2947 JXG.createTangentTo = function (board, parents, attributes) {
2948     var el, attr,
2949         conic, pointFrom, num,
2950         intersect, polar;
2951 
2952     conic = board.select(parents[0]);
2953     pointFrom = Type.providePoints(board, parents[1], attributes, 'point')[0];
2954     num = Type.def(parents[2], 0);
2955 
2956     if (
2957         (conic.type !== Const.OBJECT_TYPE_CIRCLE && conic.type !== Const.OBJECT_TYPE_CONIC) ||
2958         (pointFrom.elementClass !== Const.OBJECT_CLASS_POINT)
2959     ) {
2960         throw new Error(
2961             "JSXGraph: Can't create tangentto with parent types '" +
2962             typeof parents[0] +
2963             "' and '" +
2964             typeof parents[1] +
2965             "' and '" +
2966             typeof parents[2] +
2967             "'." +
2968             "\nPossible parent types: [circle|conic,point,number]"
2969         );
2970     }
2971 
2972     attr = Type.copyAttributes(attributes, board.options, 'tangentto');
2973     // A direct analytic geometry approach would be in
2974     // Richter-Gebert: Perspectives on projective geometry, 11.3
2975     polar = board.create('polar', [conic, pointFrom], attr.polar);
2976     intersect = board.create('intersection', [polar, conic, num], attr.point);
2977 
2978     el = board.create('tangent', [conic, intersect], attr);
2979 
2980     /**
2981      * The intersection point of the conic/circle with the polar line of the tangentto construction.
2982      * @memberOf TangentTo.prototype
2983      * @name point
2984      * @type JXG.Point
2985      */
2986     el.point = intersect;
2987 
2988     /**
2989      * The polar line of the tangentto construction.
2990      * @memberOf TangentTo.prototype
2991      * @name polar
2992      * @type JXG.Line
2993      */
2994     el.polar = polar;
2995 
2996     el.elType = 'tangentto';
2997 
2998     return el;
2999 };
3000 
3001 /**
3002  * Register the element type tangent at JSXGraph
3003  * @private
3004  */
3005 JXG.registerElement("tangent", JXG.createTangent);
3006 JXG.registerElement("normal", JXG.createNormal);
3007 JXG.registerElement('tangentto', JXG.createTangentTo);
3008 JXG.registerElement("polar", JXG.createTangent);
3009 JXG.registerElement("radicalaxis", JXG.createRadicalAxis);
3010 JXG.registerElement("polarline", JXG.createPolarLine);
3011 
3012 export default JXG.Line;
3013 // export default {
3014 //     Line: JXG.Line,
3015 //     createLine: JXG.createLine,
3016 //     createTangent: JXG.createTangent,
3017 //     createPolar: JXG.createTangent,
3018 //     createSegment: JXG.createSegment,
3019 //     createAxis: JXG.createAxis,
3020 //     createArrow: JXG.createArrow,
3021 //     createRadicalAxis: JXG.createRadicalAxis,
3022 //     createPolarLine: JXG.createPolarLine
3023 // };
3024