1 /*
  2     Copyright 2008-2021
  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 <http://www.gnu.org/licenses/>
 29     and <http://opensource.org/licenses/MIT/>.
 30  */
 31 
 32 
 33 /*global JXG: true, define: true*/
 34 /*jslint nomen: true, plusplus: true*/
 35 
 36 /* depends:
 37  jxg
 38  math/math
 39  math/geometry
 40  base/constants
 41  base/element
 42  base/coords
 43  utils/type
 44   elements:
 45    text
 46  */
 47 
 48 /**
 49  * @fileoverview In this file the geometry object Ticks is defined. Ticks provides
 50  * methods for creation and management of ticks on an axis.
 51  * @author graphjs
 52  * @version 0.1
 53  */
 54 
 55 define([
 56     'jxg', 'math/math', 'math/geometry', 'math/numerics', 'base/constants', 'base/element', 'base/coords', 'utils/type', 'base/text'
 57 ], function (JXG, Mat, Geometry, Numerics, Const, GeometryElement, Coords, Type, Text) {
 58 
 59     "use strict";
 60 
 61     /**
 62      * Creates ticks for an axis.
 63      * @class Ticks provides methods for creation and management
 64      * of ticks on an axis.
 65      * @param {JXG.Line} line Reference to the axis the ticks are drawn on.
 66      * @param {Number|Array} ticks Number defining the distance between two major ticks or an array defining static ticks.
 67      * @param {Object} attributes Properties
 68      * @see JXG.Line#addTicks
 69      * @constructor
 70      * @extends JXG.GeometryElement
 71      */
 72     JXG.Ticks = function (line, ticks, attributes) {
 73         this.constructor(line.board, attributes, Const.OBJECT_TYPE_TICKS, Const.OBJECT_CLASS_OTHER);
 74 
 75         /**
 76          * The line the ticks belong to.
 77          * @type JXG.Line
 78          */
 79         this.line = line;
 80 
 81         /**
 82          * The board the ticks line is drawn on.
 83          * @type JXG.Board
 84          */
 85         this.board = this.line.board;
 86 
 87         /**
 88          * A function calculating ticks delta depending on the ticks number.
 89          * @type Function
 90          */
 91         this.ticksFunction = null;
 92 
 93         /**
 94          * Array of fixed ticks.
 95          * @type Array
 96          */
 97         this.fixedTicks = null;
 98 
 99         /**
100          * Equidistant ticks. Distance is defined by ticksFunction
101          * @type Boolean
102          */
103         this.equidistant = false;
104 
105         this.labelsData = [];
106 
107         if (Type.isFunction(ticks)) {
108             this.ticksFunction = ticks;
109             throw new Error("Function arguments are no longer supported.");
110         }
111 
112         if (Type.isArray(ticks)) {
113             this.fixedTicks = ticks;
114         } else {
115             if (Math.abs(ticks) < Mat.eps || ticks < 0) {
116                 ticks = attributes.defaultdistance;
117             }
118 
119             /*
120              * Ticks function:
121              * determines the distance (in user units) of two major ticks
122              */
123             this.ticksFunction = this.makeTicksFunction(ticks);
124 
125             this.equidistant = true;
126         }
127 
128         /**
129          * Least distance between two ticks, measured in pixels.
130          * @type int
131          */
132         this.minTicksDistance = attributes.minticksdistance;
133 
134         /**
135          * Stores the ticks coordinates
136          * @type Array
137          */
138         this.ticks = [];
139 
140         /**
141          * Distance between two major ticks in user coordinates
142          * @type Number
143          */
144         this.ticksDelta = 1;
145 
146         /**
147          * Array where the labels are saved. There is an array element for every tick,
148          * even for minor ticks which don't have labels. In this case the array element
149          * contains just <tt>null</tt>.
150          * @type Array
151          */
152         this.labels = [];
153 
154         /**
155          * A list of labels which have to be displayed in updateRenderer.
156          * @type Array
157          */
158         this.labelData = [];
159 
160         /**
161          * To ensure the uniqueness of label ids this counter is used.
162          * @type number
163          */
164         this.labelCounter = 0;
165 
166         this.id = this.line.addTicks(this);
167         this.elType = 'ticks';
168         this.inherits.push(this.labels);
169         this.board.setId(this, 'Ti');
170     };
171 
172     JXG.Ticks.prototype = new GeometryElement();
173 
174     JXG.extend(JXG.Ticks.prototype, /** @lends JXG.Ticks.prototype */ {
175 
176         /**
177          * Ticks function:
178          * determines the distance (in user units) of two major ticks.
179          * See above in constructor and in @see JXG.GeometryElement#setAttribute
180          *
181          * @private
182          * @param {Number} ticks Distance between two major ticks
183          * @returns {Function} returns method ticksFunction
184          */
185         makeTicksFunction: function (ticks) {
186             return function () {
187                 var delta, b, dist;
188 
189                 if (Type.evaluate(this.visProp.insertticks)) {
190                     b = this.getLowerAndUpperBounds(this.getZeroCoordinates(), 'ticksdistance');
191                     dist = b.upper - b.lower;
192 
193                     delta = Math.pow(10, Math.floor(Math.log(0.6 * dist) / Math.LN10));
194                     if (dist <= 6 * delta) {
195                         delta *= 0.5;
196                     }
197                     return delta;
198                 }
199 
200                 // upto 0.99.1:
201                 return ticks;
202             };
203         },
204 
205         /**
206          * Checks whether (x,y) is near the line.
207          * Only available for line elements,  not for ticks on curves.
208          * @param {Number} x Coordinate in x direction, screen coordinates.
209          * @param {Number} y Coordinate in y direction, screen coordinates.
210          * @returns {Boolean} True if (x,y) is near the line, False otherwise.
211          */
212         hasPoint: function (x, y) {
213             var i, t,
214                 len = (this.ticks && this.ticks.length) || 0,
215                 r, type;
216 
217             if (Type.isObject(Type.evaluate(this.visProp.precision))) {
218                 type = this.board._inputDevice;
219                 r = Type.evaluate(this.visProp.precision[type]);
220             } else {
221                 // 'inherit'
222                 r = this.board.options.precision.hasPoint;
223             }
224             r += Type.evaluate(this.visProp.strokewidth) * 0.5;
225             if (!Type.evaluate(this.line.visProp.scalable) ||
226                 this.line.elementClass === Const.OBJECT_CLASS_CURVE) {
227                 return false;
228             }
229 
230             // Ignore non-axes and axes that are not horizontal or vertical
231             if (this.line.stdform[1] !== 0 && this.line.stdform[2] !== 0 && this.line.type !== Const.OBJECT_TYPE_AXIS) {
232                 return false;
233             }
234 
235             for (i = 0; i < len; i++) {
236                 t = this.ticks[i];
237 
238                 // Skip minor ticks
239                 if (t[2]) {
240                     // Ignore ticks at zero
241                     if (!((this.line.stdform[1] === 0 && Math.abs(t[0][0] - this.line.point1.coords.scrCoords[1]) < Mat.eps) ||
242                             (this.line.stdform[2] === 0 && Math.abs(t[1][0] - this.line.point1.coords.scrCoords[2]) < Mat.eps))) {
243                         // tick length is not zero, ie. at least one pixel
244                         if (Math.abs(t[0][0] - t[0][1]) >= 1 || Math.abs(t[1][0] - t[1][1]) >= 1) {
245                             if (this.line.stdform[1] === 0) {
246                                 // Allow dragging near axes only.
247                                 if (Math.abs(y - (t[1][0] + t[1][1]) * 0.5) < 2 * r && t[0][0] - r < x && x < t[0][1] + r) {
248                                     return true;
249                                 }
250                             } else if (this.line.stdform[2] === 0) {
251                                 if (Math.abs(x - (t[0][0] + t[0][1]) * 0.5) < 2 * r && t[1][0] - r < y && y < t[1][1] + r) {
252                                     return true;
253                                 }
254                             }
255                         }
256                     }
257                 }
258             }
259 
260             return false;
261         },
262 
263         /**
264          * Sets x and y coordinate of the tick.
265          * @param {number} method The type of coordinates used here. Possible values are {@link JXG.COORDS_BY_USER} and {@link JXG.COORDS_BY_SCREEN}.
266          * @param {Array} coords coordinates in screen/user units
267          * @param {Array} oldcoords previous coordinates in screen/user units
268          * @returns {JXG.Ticks} this element
269          */
270         setPositionDirectly: function (method, coords, oldcoords) {
271             var dx, dy,
272                 c = new Coords(method, coords, this.board),
273                 oldc = new Coords(method, oldcoords, this.board),
274                 bb = this.board.getBoundingBox();
275 
276             if (this.line.type !== Const.OBJECT_TYPE_AXIS ||
277                 !Type.evaluate(this.line.visProp.scalable)) {
278 
279                 return this;
280             }
281 
282             if (Math.abs(this.line.stdform[1]) < Mat.eps &&
283                 Math.abs(c.usrCoords[1] * oldc.usrCoords[1]) > Mat.eps) {
284 
285                 // Horizontal line
286                 dx = oldc.usrCoords[1] / c.usrCoords[1];
287                 bb[0] *= dx;
288                 bb[2] *= dx;
289                 this.board.setBoundingBox(bb, this.board.keepaspectratio, 'update');
290 
291             } else if (Math.abs(this.line.stdform[2]) < Mat.eps &&
292                        Math.abs(c.usrCoords[2] * oldc.usrCoords[2]) > Mat.eps) {
293 
294                 // Vertical line
295                 dy = oldc.usrCoords[2] / c.usrCoords[2];
296                 bb[3] *= dy;
297                 bb[1] *= dy;
298                 this.board.setBoundingBox(bb, this.board.keepaspectratio, 'update');
299             }
300 
301             return this;
302         },
303 
304         /**
305          * (Re-)calculates the ticks coordinates.
306          * @private
307          */
308         calculateTicksCoordinates: function () {
309             var coordsZero, bounds,
310                 r_max, bb;
311 
312             if (this.line.elementClass === Const.OBJECT_CLASS_LINE) {
313                 // Calculate Ticks width and height in Screen and User Coordinates
314                 this.setTicksSizeVariables();
315 
316                 // If the parent line is not finite, we can stop here.
317                 if (Math.abs(this.dx) < Mat.eps &&
318                     Math.abs(this.dy) < Mat.eps) {
319                     return;
320                 }
321             }
322 
323             // Get Zero (coords element for lines , number for curves)
324             coordsZero = this.getZeroCoordinates();
325 
326             // Calculate lower bound and upper bound limits based on distance
327             // between p1 and center and p2 and center
328             if (this.line.elementClass === Const.OBJECT_CLASS_LINE) {
329                 bounds = this.getLowerAndUpperBounds(coordsZero);
330             } else {
331                 bounds = {
332                     lower: this.line.minX(),
333                     upper: this.line.maxX()
334                 };
335             }
336 
337             if (Type.evaluate(this.visProp.type) === 'polar') {
338                 bb = this.board.getBoundingBox();
339                 r_max = Math.max(Math.sqrt(bb[0] * bb[0] + bb[1] * bb[1]),
340                     Math.sqrt(bb[2] * bb[2] + bb[3] * bb[3]));
341                 bounds.upper = r_max;
342             }
343 
344             // Clean up
345             this.ticks = [];
346             this.labelsData = [];
347             // Create Ticks Coordinates and Labels
348             if (this.equidistant) {
349                 this.generateEquidistantTicks(coordsZero, bounds);
350             } else {
351                 this.generateFixedTicks(coordsZero, bounds);
352             }
353 
354             return this;
355         },
356 
357         /**
358          * Sets the variables used to set the height and slope of each tick.
359          *
360          * @private
361          */
362         setTicksSizeVariables: function (pos) {
363             var d, mi, ma, len,
364                 distMaj = Type.evaluate(this.visProp.majorheight) * 0.5,
365                 distMin = Type.evaluate(this.visProp.minorheight) * 0.5;
366 
367             // For curves:
368             if (Type.exists(pos)) {
369                 mi = this.line.minX();
370                 ma = this.line.maxX();
371                 len = this.line.points.length;
372                 if (len < 2) {
373                     this.dxMaj = 0;
374                     this.dyMaj = 0;
375                 } else if (Mat.relDif(pos, mi) < Mat.eps) {
376                     this.dxMaj = this.line.points[0].usrCoords[2] - this.line.points[1].usrCoords[2];
377                     this.dyMaj = this.line.points[1].usrCoords[1] - this.line.points[0].usrCoords[1];
378                 } else if (Mat.relDif(pos, ma) < Mat.eps) {
379                     this.dxMaj = this.line.points[len - 2].usrCoords[2] - this.line.points[len - 1].usrCoords[2];
380                     this.dyMaj = this.line.points[len - 1].usrCoords[1] - this.line.points[len - 2].usrCoords[1];
381                 } else {
382                     this.dxMaj = -Numerics.D(this.line.Y)(pos);
383                     this.dyMaj = Numerics.D(this.line.X)(pos);
384                 }
385             } else {
386                 // ticks width and height in screen units
387                 this.dxMaj = this.line.stdform[1];
388                 this.dyMaj = this.line.stdform[2];
389             }
390             this.dxMin = this.dxMaj;
391             this.dyMin = this.dyMaj;
392 
393             // ticks width and height in user units
394             this.dx = this.dxMaj;
395             this.dy = this.dyMaj;
396 
397             // After this, the length of the vector (dxMaj, dyMaj) in screen coordinates is equal to distMaj pixel.
398             d = Math.sqrt(
399                 this.dxMaj * this.dxMaj * this.board.unitX * this.board.unitX +
400                     this.dyMaj * this.dyMaj * this.board.unitY * this.board.unitY
401             );
402             this.dxMaj *= distMaj / d * this.board.unitX;
403             this.dyMaj *= distMaj / d * this.board.unitY;
404             this.dxMin *= distMin / d * this.board.unitX;
405             this.dyMin *= distMin / d * this.board.unitY;
406 
407             // Grid-like ticks?
408             this.minStyle= (Type.evaluate(this.visProp.minorheight) < 0) ? 'infinite' : 'finite';
409             this.majStyle= (Type.evaluate(this.visProp.majorheight) < 0) ? 'infinite' : 'finite';
410         },
411 
412         /**
413          * Returns the coordinates of the point zero of the line.
414          *
415          * If the line is an {@link Axis}, the coordinates of the projection of the board's zero point is returned
416          *
417          * Otherwise, the coordinates of the point that acts as zero are
418          * established depending on the value of {@link JXG.Ticks#anchor}
419          *
420          * @returns {JXG.Coords} Coords object for the zero point on the line
421          * @private
422          */
423         getZeroCoordinates: function () {
424             var c1x, c1y, c1z, c2x, c2y, c2z, t, mi, ma,
425                 ev_a = Type.evaluate(this.visProp.anchor);
426 
427             if (this.line.elementClass === Const.OBJECT_CLASS_LINE) {
428                 if (this.line.type === Const.OBJECT_TYPE_AXIS) {
429                     return Geometry.projectPointToLine({
430                         coords: {
431                             usrCoords: [1, 0, 0]
432                         }
433                     }, this.line, this.board);
434                 }
435                 c1z = this.line.point1.coords.usrCoords[0];
436                 c1x = this.line.point1.coords.usrCoords[1];
437                 c1y = this.line.point1.coords.usrCoords[2];
438                 c2z = this.line.point2.coords.usrCoords[0];
439                 c2x = this.line.point2.coords.usrCoords[1];
440                 c2y = this.line.point2.coords.usrCoords[2];
441 
442                 if (ev_a === 'right') {
443                     return this.line.point2.coords;
444                 }
445                 if (ev_a === 'middle') {
446                     return new Coords(Const.COORDS_BY_USER, [
447                         (c1z + c2z) * 0.5,
448                         (c1x + c2x) * 0.5,
449                         (c1y + c2y) * 0.5
450                     ], this.board);
451                 }
452                 if (Type.isNumber(ev_a)) {
453                     return new Coords(Const.COORDS_BY_USER, [
454                         c1z + (c2z - c1z) * ev_a,
455                         c1x + (c2x - c1x) * ev_a,
456                         c1y + (c2y - c1y) * ev_a
457                     ], this.board);
458                 }
459                 return this.line.point1.coords;
460             }
461             mi = this.line.minX();
462             ma = this.line.maxX();
463             if (ev_a === 'right') {
464                 t = ma;
465             } else if (ev_a === 'middle') {
466                 t = (mi + ma) * 0.5;
467             } else if (Type.isNumber(ev_a)) {
468                 t = mi * (1 - ev_a) + ma * ev_a;
469                 // t = ev_a;
470             } else {
471                 t = mi;
472             }
473             return t;
474         },
475 
476         /**
477          * Calculate the lower and upper bounds for tick rendering
478          * If {@link JXG.Ticks#includeBoundaries} is false, the boundaries will exclude point1 and point2
479          *
480          * @param  {JXG.Coords} coordsZero
481          * @returns {String} type  (Optional) If type=='ticksdistance' the bounds are
482          *                         the intersection of the line with the bounding box of the board.
483          *                         Otherwise, it is the projection of the corners of the bounding box
484          *                         to the line. The first case is needed to automatically
485          *                         generate ticks. The second case is for drawing of the ticks.
486          * @returns {Object}     contains the lower and upper bounds
487          *
488          * @private
489          */
490         getLowerAndUpperBounds: function (coordsZero, type) {
491             var lowerBound, upperBound,
492                 fA, lA,
493                 point1, point2, isPoint1inBoard, isPoint2inBoard,
494                 // We use the distance from zero to P1 and P2 to establish lower and higher points
495                 dZeroPoint1, dZeroPoint2,
496                 ev_sf = Type.evaluate(this.line.visProp.straightfirst),
497                 ev_sl = Type.evaluate(this.line.visProp.straightlast),
498                 ev_i = Type.evaluate(this.visProp.includeboundaries);
499 
500             // The line's defining points that will be adjusted to be within the board limits
501             if (this.line.elementClass === Const.OBJECT_CLASS_CURVE) {
502                 return {
503                     lower: this.line.minX(),
504                     upper: this.line.maxX()
505                 };
506             }
507 
508             point1 = new Coords(Const.COORDS_BY_USER, this.line.point1.coords.usrCoords, this.board);
509             point2 = new Coords(Const.COORDS_BY_USER, this.line.point2.coords.usrCoords, this.board);
510             // Are the original defining points within the board?
511             isPoint1inBoard = (Math.abs(point1.usrCoords[0]) >= Mat.eps &&
512                 point1.scrCoords[1] >= 0.0 && point1.scrCoords[1] <= this.board.canvasWidth &&
513                 point1.scrCoords[2] >= 0.0 && point1.scrCoords[2] <= this.board.canvasHeight);
514             isPoint2inBoard = (Math.abs(point2.usrCoords[0]) >= Mat.eps &&
515                 point2.scrCoords[1] >= 0.0 && point2.scrCoords[1] <= this.board.canvasWidth &&
516                 point2.scrCoords[2] >= 0.0 && point2.scrCoords[2] <= this.board.canvasHeight);
517 
518             // Adjust line limit points to be within the board
519             if (Type.exists(type) || type === 'tickdistance') {
520                 // The good old calcStraight is needed for determining the distance between major ticks.
521                 // Here, only the visual area is of importance
522                 Geometry.calcStraight(this.line, point1, point2, Type.evaluate(this.line.visProp.margin));
523             } else {
524                 // This function projects the corners of the board to the line.
525                 // This is important for diagonal lines with infinite tick lines.
526                 Geometry.calcLineDelimitingPoints(this.line, point1, point2);
527             }
528 
529             // Shorten ticks bounds such that ticks are not through arrow heads
530             fA = Type.evaluate(this.line.visProp.firstarrow);
531             lA = Type.evaluate(this.line.visProp.lastarrow);
532             if (fA || lA) {
533                 this.board.renderer.getPositionArrowHead(this.line, point1, point2,
534                         Type.evaluate(this.line.visProp.strokewidth));
535 
536                 if (fA) {
537                     point1.setCoordinates(Const.COORDS_BY_SCREEN, [
538                         point1.scrCoords[1],
539                         point1.scrCoords[2]
540                     ]);
541                 }
542                 if (lA) {
543                     point2.setCoordinates(Const.COORDS_BY_SCREEN, [
544                         point2.scrCoords[1],
545                         point2.scrCoords[2]
546                     ]);
547                 }
548                 // if (fA) {
549                 //     point1.setCoordinates(Const.COORDS_BY_SCREEN, [
550                 //         point1.scrCoords[1] - obj.d1x,
551                 //         point1.scrCoords[2] - obj.d1y
552                 //     ]);
553                 // }
554                 // if (lA) {
555                 //     point2.setCoordinates(Const.COORDS_BY_SCREEN, [
556                 //         point2.scrCoords[1] - obj.d2x,
557                 //         point2.scrCoords[2] - obj.d2y
558                 //     ]);
559                 // }
560             }
561 
562 
563             // Calculate (signed) distance from Zero to P1 and to P2
564             dZeroPoint1 = this.getDistanceFromZero(coordsZero, point1);
565             dZeroPoint2 = this.getDistanceFromZero(coordsZero, point2);
566 
567             // We have to establish if the direction is P1->P2 or P2->P1 to set the lower and upper
568             // boundaries appropriately. As the distances contain also a sign to indicate direction,
569             // we can compare dZeroPoint1 and dZeroPoint2 to establish the line direction
570             if (dZeroPoint1 < dZeroPoint2) { // Line goes P1->P2
571                 lowerBound = dZeroPoint1;
572                 if (!ev_sf && isPoint1inBoard && !ev_i) {
573                     lowerBound += Mat.eps;
574                 }
575                 upperBound = dZeroPoint2;
576                 if (!ev_sl && isPoint2inBoard && !ev_i) {
577                     upperBound -= Mat.eps;
578                 }
579             } else if (dZeroPoint2 < dZeroPoint1) { // Line goes P2->P1
580                 lowerBound = dZeroPoint2;
581                 if (!ev_sl && isPoint2inBoard && !ev_i) {
582                     lowerBound += Mat.eps;
583                 }
584                 upperBound = dZeroPoint1;
585                 if (!ev_sf && isPoint1inBoard && !ev_i) {
586                     upperBound -= Mat.eps;
587                 }
588             } else { // P1 = P2 = Zero, we can't do a thing
589                 lowerBound = 0;
590                 upperBound = 0;
591             }
592 
593             return {
594                 lower: lowerBound,
595                 upper: upperBound
596             };
597         },
598 
599         /**
600          * Calculates the distance in user coordinates from zero to a given point including its sign.
601          * Sign is positive, if the direction from zero to point is the same as the direction
602          * zero to point2 of the line.
603          *
604          * @param  {JXG.Coords} zero  coordinates of the point considered zero
605          * @param  {JXG.Coords} point coordinates of the point to find out the distance
606          * @returns {Number}           distance between zero and point, including its sign
607          * @private
608          */
609         getDistanceFromZero: function (zero, point) {
610             var p1, p2,
611                 dirLine, dirPoint,
612                 distance;
613 
614             p1 = this.line.point1.coords;
615             p2 = this.line.point2.coords;
616             distance = zero.distance(Const.COORDS_BY_USER, point);
617 
618             // Establish sign
619             dirLine = [p2.usrCoords[0] - p1.usrCoords[0],
620                 p2.usrCoords[1] - p1.usrCoords[1],
621                 p2.usrCoords[2] - p1.usrCoords[2]];
622             dirPoint = [point.usrCoords[0] - zero.usrCoords[0],
623                 point.usrCoords[1] - zero.usrCoords[1],
624                 point.usrCoords[2] - zero.usrCoords[2]];
625             if (Mat.innerProduct(dirLine, dirPoint, 3) < 0) {
626                 distance *= -1;
627             }
628 
629             return distance;
630         },
631 
632         /**
633          * Creates ticks coordinates and labels automatically.
634          * The frequency of ticks is affected by the values of {@link JXG.Ticks#insertTicks} and {@link JXG.Ticks#ticksDistance}
635          *
636          * @param  {JXG.Coords} coordsZero coordinates of the point considered zero
637          * @param  {Object}     bounds     contains the lower and upper boundaries for ticks placement
638          * @private
639          */
640         generateEquidistantTicks: function (coordsZero, bounds) {
641             var tickPosition,
642                 eps2 = Mat.eps,
643                 deltas,
644                 // Distance between two major ticks in user coordinates
645                 ticksDelta = (this.equidistant ? this.ticksFunction(1) : this.ticksDelta),
646                 ev_it = Type.evaluate(this.visProp.insertticks),
647                 ev_mt = Type.evaluate(this.visProp.minorticks);
648 
649             if (this.line.elementClass === Const.OBJECT_CLASS_LINE) {
650                 // Calculate X and Y distance between two major ticks
651                 deltas = this.getXandYdeltas();
652             }
653 
654             // adjust ticks distance
655             ticksDelta *= Type.evaluate(this.visProp.scale);
656             if (ev_it && this.minTicksDistance > Mat.eps) {
657                 ticksDelta = this.adjustTickDistance(ticksDelta, coordsZero, deltas);
658                 ticksDelta /= (ev_mt + 1);
659             } else if (!ev_it) {
660                 ticksDelta /= (ev_mt + 1);
661             }
662             this.ticksDelta = ticksDelta;
663 
664             if (ticksDelta < Mat.eps) {
665                 return;
666             }
667 
668             // Position ticks from zero to the positive side while not reaching the upper boundary
669             tickPosition = 0;
670             if (!Type.evaluate(this.visProp.drawzero)) {
671                 tickPosition = ticksDelta;
672             }
673             while (tickPosition <= bounds.upper + eps2) {
674                 // Only draw ticks when we are within bounds, ignore case where tickPosition < lower < upper
675                 if (tickPosition >= bounds.lower - eps2) {
676                     this.processTickPosition(coordsZero, tickPosition, ticksDelta, deltas);
677                 }
678                 tickPosition += ticksDelta;
679 
680                 // Emergency out
681                 if ((bounds.upper - tickPosition) > ticksDelta * 10000) {
682                     break;
683                 }
684             }
685 
686             // Position ticks from zero (not inclusive) to the negative side while not reaching the lower boundary
687             tickPosition = -ticksDelta;
688             while (tickPosition >= bounds.lower - eps2) {
689                 // Only draw ticks when we are within bounds, ignore case where lower < upper < tickPosition
690                 if (tickPosition <= bounds.upper + eps2) {
691                     this.processTickPosition(coordsZero, tickPosition, ticksDelta, deltas);
692                 }
693                 tickPosition -= ticksDelta;
694 
695                 // Emergency out
696                 if ((tickPosition - bounds.lower) > ticksDelta * 10000) {
697                     break;
698                 }
699             }
700         },
701 
702         /**
703          * Auxiliary method used by {@link JXG.Ticks#generateEquidistantTicks} to adjust the
704          * distance between two ticks depending on {@link JXG.Ticks#minTicksDistance} value
705          *
706          * @param  {Number}     ticksDelta  distance between two major ticks in user coordinates
707          * @param  {JXG.Coords} coordsZero  coordinates of the point considered zero
708          * @param  {Object}     deltas      x and y distance in pixel between two user units
709          * @param  {Object}     bounds      upper and lower bound of the tick positions in user units.
710          * @private
711          */
712         adjustTickDistance: function (ticksDelta, coordsZero, deltas) {
713             var nx, ny, bounds,
714                 distScr,
715                 sgn = 1,
716                 ev_minti = Type.evaluate(this.visProp.minorticks);
717 
718             if (this.line.elementClass === Const.OBJECT_CLASS_CURVE) {
719                 return ticksDelta;
720             }
721             bounds = this.getLowerAndUpperBounds(coordsZero, 'ticksdistance');
722             nx = coordsZero.usrCoords[1] + deltas.x * ticksDelta;
723             ny = coordsZero.usrCoords[2] + deltas.y * ticksDelta;
724             distScr = coordsZero.distance(Const.COORDS_BY_SCREEN, new Coords(Const.COORDS_BY_USER, [nx, ny], this.board));
725 
726             if (ticksDelta === 0.0) {
727                 return 0.0;
728             }
729 
730             while (distScr / (ev_minti + 1) < this.minTicksDistance) {
731                 if (sgn === 1) {
732                     ticksDelta *= 2;
733                 } else {
734                     ticksDelta *= 5;
735                 }
736                 sgn *= -1;
737 
738                 nx = coordsZero.usrCoords[1] + deltas.x * ticksDelta;
739                 ny = coordsZero.usrCoords[2] + deltas.y * ticksDelta;
740                 distScr = coordsZero.distance(Const.COORDS_BY_SCREEN, new Coords(Const.COORDS_BY_USER, [nx, ny], this.board));
741             }
742             return ticksDelta;
743         },
744 
745         /**
746          * Auxiliary method used by {@link JXG.Ticks#generateEquidistantTicks} to create a tick
747          * in the line at the given tickPosition.
748          *
749          * @param  {JXG.Coords} coordsZero    coordinates of the point considered zero
750          * @param  {Number}     tickPosition  current tick position relative to zero
751          * @param  {Number}     ticksDelta    distance between two major ticks in user coordinates
752          * @param  {Object}     deltas      x and y distance between two major ticks
753          * @private
754          */
755         processTickPosition: function (coordsZero, tickPosition, ticksDelta, deltas) {
756             var x, y, tickCoords, ti,
757                 labelVal = null;
758 
759             // Calculates tick coordinates
760             if (this.line.elementClass === Const.OBJECT_CLASS_LINE) {
761                 x = coordsZero.usrCoords[1] + tickPosition * deltas.x;
762                 y = coordsZero.usrCoords[2] + tickPosition * deltas.y;
763             } else {
764                 x = this.line.X(coordsZero + tickPosition);
765                 y = this.line.Y(coordsZero + tickPosition);
766             }
767             tickCoords = new Coords(Const.COORDS_BY_USER, [x, y], this.board);
768             if (this.line.elementClass === Const.OBJECT_CLASS_CURVE) {
769                 labelVal = coordsZero + tickPosition;
770                 this.setTicksSizeVariables(labelVal);
771 
772             }
773 
774             // Test if tick is a major tick.
775             // This is the case if tickPosition/ticksDelta is
776             // a multiple of the number of minorticks+1
777             tickCoords.major = Math.round(tickPosition / ticksDelta) % (Type.evaluate(this.visProp.minorticks) + 1) === 0;
778 
779             // Compute the start position and the end position of a tick.
780             // If both positions are out of the canvas, ti is empty.
781             ti = this.createTickPath(tickCoords, tickCoords.major);
782             if (ti.length === 3) {
783                 this.ticks.push(ti);
784                 if (tickCoords.major && Type.evaluate(this.visProp.drawlabels)) {
785                     // major tick label
786                     this.labelsData.push(
787                         this.generateLabelData(
788                             this.generateLabelText(tickCoords, coordsZero, labelVal),
789                             tickCoords,
790                             this.ticks.length
791                         )
792                     );
793                 } else {
794                     // minor ticks have no labels
795                     this.labelsData.push(null);
796                 }
797             }
798         },
799 
800         /**
801          * Creates ticks coordinates and labels based on {@link JXG.Ticks#fixedTicks} and {@link JXG.Ticks#labels}.
802          *
803          * @param  {JXG.Coords} coordsZero Coordinates of the point considered zero
804          * @param  {Object}     bounds     contains the lower and upper boundaries for ticks placement
805          * @private
806          */
807         generateFixedTicks: function (coordsZero, bounds) {
808             var tickCoords, labelText, i, ti,
809                 x, y,
810                 eps2 = Mat.eps, fixedTick,
811                 hasLabelOverrides = Type.isArray(this.visProp.labels),
812                 deltas,
813                 ev_dl = Type.evaluate(this.visProp.drawlabels);
814 
815             // Calculate X and Y distance between two major points in the line
816             if (this.line.elementClass === Const.OBJECT_CLASS_LINE) {
817                 deltas = this.getXandYdeltas();
818             }
819             for (i = 0; i < this.fixedTicks.length; i++) {
820                 if (this.line.elementClass === Const.OBJECT_CLASS_LINE) {
821                     fixedTick = this.fixedTicks[i];
822                     x = coordsZero.usrCoords[1] + fixedTick * deltas.x;
823                     y = coordsZero.usrCoords[2] + fixedTick * deltas.y;
824                 } else {
825                     fixedTick = coordsZero + this.fixedTicks[i];
826                     x = this.line.X(fixedTick);
827                     y = this.line.Y(fixedTick);
828                 }
829                 tickCoords = new Coords(Const.COORDS_BY_USER, [x, y], this.board);
830 
831                 if (this.line.elementClass === Const.OBJECT_CLASS_CURVE) {
832                     this.setTicksSizeVariables(fixedTick);
833                 }
834 
835                 // Compute the start position and the end position of a tick.
836                 // If tick is out of the canvas, ti is empty.
837                 ti = this.createTickPath(tickCoords, true);
838                 if (ti.length === 3 && fixedTick >= bounds.lower - eps2 &&
839                     fixedTick <= bounds.upper + eps2) {
840                     this.ticks.push(ti);
841 
842                     if (ev_dl &&
843                             (hasLabelOverrides || Type.exists(this.visProp.labels[i]))) {
844                         labelText = hasLabelOverrides ?
845                                         Type.evaluate(this.visProp.labels[i]) : fixedTick;
846                         this.labelsData.push(
847                             this.generateLabelData(
848                                 this.generateLabelText(tickCoords, coordsZero, labelText),
849                                 tickCoords,
850                                 i
851                             )
852                         );
853                     } else {
854                         this.labelsData.push(null);
855                     }
856                 }
857             }
858         },
859 
860         /**
861          * Calculates the x and y distance in pixel between two units in user space.
862          *
863          * @returns {Object}
864          * @private
865          */
866         getXandYdeltas: function () {
867             var
868                 // Auxiliary points to store the start and end of the line according to its direction
869                 point1UsrCoords, point2UsrCoords,
870                 distP1P2 = this.line.point1.Dist(this.line.point2);
871 
872             if (this.line.type === Const.OBJECT_TYPE_AXIS) {
873                 // When line is an Axis, direction depends on Board Coordinates system
874 
875                 // assume line.point1 and line.point2 are in correct order
876                 point1UsrCoords = this.line.point1.coords.usrCoords;
877                 point2UsrCoords = this.line.point2.coords.usrCoords;
878 
879                 // Check if direction is incorrect, then swap
880                 if (point1UsrCoords[1] > point2UsrCoords[1] ||
881                         (Math.abs(point1UsrCoords[1] - point2UsrCoords[1]) < Mat.eps &&
882                         point1UsrCoords[2] > point2UsrCoords[2])) {
883                     point1UsrCoords = this.line.point2.coords.usrCoords;
884                     point2UsrCoords = this.line.point1.coords.usrCoords;
885                 }
886             } else /* if (this.line.elementClass === Const.OBJECT_CLASS_LINE)*/ {
887                 // line direction is always from P1 to P2 for non Axis types
888                 point1UsrCoords = this.line.point1.coords.usrCoords;
889                 point2UsrCoords = this.line.point2.coords.usrCoords;
890             }
891             return {
892                 x: (point2UsrCoords[1] - point1UsrCoords[1]) / distP1P2,
893                 y: (point2UsrCoords[2] - point1UsrCoords[2]) / distP1P2
894             };
895         },
896 
897         /**
898          * Check if (parts of) the tick is inside the canvas. The tick intersects the boundary
899          * at two positions: [x[0], y[0]] and [x[1], y[1]] in screen coordinates.
900          * @param  {Array}  x Array of length two
901          * @param  {Array}  y Array of length two
902          * @return {Boolean}   true if parts of the tick are inside of the canvas or on the boundary.
903          */
904         _isInsideCanvas: function(x, y, m) {
905             var cw = this.board.canvasWidth,
906                 ch = this.board.canvasHeight;
907 
908             if (m === undefined) {
909                 m = 0;
910             }
911             return (x[0] >= m && x[0] <= cw - m && y[0] >= m && y[0] <= ch - m) ||
912                     (x[1] >= m && x[1] <= cw - m && y[1] >= m && y[1] <= ch - m);
913         },
914 
915         /**
916          * @param {JXG.Coords} coords Coordinates of the tick on the line.
917          * @param {Boolean} major True if tick is major tick.
918          * @returns {Array} Array of length 3 containing path coordinates in screen coordinates
919          *                 of the tick (arrays of length 2). 3rd entry is true if major tick otherwise false.
920          *                 If the tick is outside of the canvas, the return array is empty.
921          * @private
922          */
923         createTickPath: function (coords, major) {
924             var c, lineStdForm, intersection,
925                 dxs, dys, dxr, dyr, alpha,
926                 style,
927                 x = [-2000000, -2000000],
928                 y = [-2000000, -2000000],
929                 i, r, r_max, bb, full, delta;
930 
931             c = coords.scrCoords;
932             if (major) {
933                 dxs = this.dxMaj;
934                 dys = this.dyMaj;
935                 style = this.majStyle;
936             } else {
937                 dxs = this.dxMin;
938                 dys = this.dyMin;
939                 style = this.minStyle;
940             }
941             lineStdForm = [-dys * c[1] - dxs * c[2], dys, dxs];
942 
943             // For all ticks regardless if of finite or infinite
944             // tick length the intersection with the canvas border is
945             // computed.
946             if (major && Type.evaluate(this.visProp.type) === 'polar') {
947                 // polar style
948                 bb = this.board.getBoundingBox();
949                 full = 2.0 * Math.PI;
950                 delta = full / 180;
951                 //ratio = this.board.unitY / this.board.X;
952 
953                 // usrCoords: Test if 'circle' is inside of the canvas
954                 c = coords.usrCoords;
955                 r = Math.sqrt(c[1] * c[1] + c[2] * c[2]);
956                 r_max = Math.max(Math.sqrt(bb[0] * bb[0] + bb[1] * bb[1]),
957                                 Math.sqrt(bb[2] * bb[2] + bb[3] * bb[3]));
958 
959                 if (r < r_max) {
960                     // Now, switch to screen coords
961                     x = [];
962                     y = [];
963                     for (i = 0; i <= full; i += delta) {
964                         x.push(this.board.origin.scrCoords[1] + r * Math.cos(i) * this.board.unitX);
965                         y.push(this.board.origin.scrCoords[2] + r * Math.sin(i) * this.board.unitY);
966                     }
967                     return [x, y, major];
968                 }
969 
970             } else {
971                 // line style
972                 if (style === 'infinite') {
973                     intersection = Geometry.meetLineBoard(lineStdForm, this.board);
974                     x[0] = intersection[0].scrCoords[1];
975                     x[1] = intersection[1].scrCoords[1];
976                     y[0] = intersection[0].scrCoords[2];
977                     y[1] = intersection[1].scrCoords[2];
978                 } else {
979                     if (Type.evaluate(this.visProp.face) === '>') {
980                         alpha = Math.PI/4;
981                     } else if (Type.evaluate(this.visProp.face) === '<') {
982                             alpha = -Math.PI/4;
983                     } else {
984                         alpha = 0;
985                     }
986                     dxr = Math.cos(alpha) * dxs - Math.sin(alpha) * dys;
987                     dyr = Math.sin(alpha) * dxs + Math.cos(alpha) * dys;
988 
989                     x[0] = c[1] + dxr * Type.evaluate(this.visProp.tickendings[0]);
990                     y[0] = c[2] - dyr * Type.evaluate(this.visProp.tickendings[0]);
991                     x[1] = c[1];
992                     y[1] = c[2];
993 
994                     alpha = -alpha;
995                     dxr = Math.cos(alpha) * dxs - Math.sin(alpha) * dys;
996                     dyr = Math.sin(alpha) * dxs + Math.cos(alpha) * dys;
997 
998                     x[2] = c[1] - dxr * Type.evaluate(this.visProp.tickendings[1]);
999                     y[2] = c[2] + dyr * Type.evaluate(this.visProp.tickendings[1]);
1000                 }
1001 
1002                 // Check if (parts of) the tick is inside the canvas.
1003                 if (this._isInsideCanvas(x, y)) {
1004                     return [x, y, major];
1005                 }
1006             }
1007 
1008             return [];
1009         },
1010 
1011         /**
1012          * Format label texts. Show the desired number of digits
1013          * and use utf-8 minus sign.
1014          * @param  {Number} value Number to be displayed
1015          * @return {String}       The value converted into a string.
1016          * @private
1017          */
1018         formatLabelText: function(value) {
1019             var labelText = value.toString(),
1020                 ev_s = Type.evaluate(this.visProp.scalesymbol);
1021 
1022             // if value is Number
1023             if (Type.isNumber(value)) {
1024                 if (labelText.length > Type.evaluate(this.visProp.maxlabellength) ||
1025                         labelText.indexOf('e') !== -1) {
1026                     labelText = value.toPrecision(Type.evaluate(this.visProp.precision)).toString();
1027                 }
1028 
1029                 if (Type.evaluate(this.visProp.beautifulscientificticklabels)) {
1030                     labelText = this.beautifyScientificNotationLabel(labelText);
1031                 }
1032 
1033                 if (labelText.indexOf('.') > -1 && labelText.indexOf('e') === -1) {
1034                     // trim trailing zeros
1035                     labelText = labelText.replace(/0+$/, '');
1036                     // trim trailing .
1037                     labelText = labelText.replace(/\.$/, '');
1038                 }
1039             }
1040 
1041             if (ev_s.length > 0) {
1042                 if (labelText === '1') {
1043                     labelText = ev_s;
1044                 } else if (labelText === '-1') {
1045                     labelText = '-' + ev_s;
1046                 } else if (labelText !== '0') {
1047                     labelText = labelText + ev_s;
1048                 }
1049             }
1050 
1051             if (Type.evaluate(this.visProp.useunicodeminus)) {
1052                 labelText = labelText.replace(/-/g, '\u2212');
1053             }
1054             return labelText;
1055         },
1056 
1057         /**
1058          * Formats label texts to make labels displayed in scientific notation look beautiful.
1059          * For example, label 5.00e+6 will become 5•10⁶, label -1.00e-7 will become into -1•10⁻⁷
1060          * @param {String} labelText - The label that we want to convert
1061          * @returns {String} If labelText was not in scientific notation, return labelText without modifications.
1062          * Otherwise returns beautified labelText with proper superscript notation.
1063          */
1064         beautifyScientificNotationLabel: function(labelText) {
1065             var returnString;
1066 
1067             if (labelText.indexOf('e') === -1) {
1068                 return labelText;
1069             }
1070 
1071             // Clean up trailing 0's, so numbers like 5.00e+6.0 for example become into 5e+6
1072             returnString = parseFloat(labelText.substring(0, labelText.indexOf('e'))) +
1073                             labelText.substring(labelText.indexOf('e'));
1074 
1075             // Replace symbols like -,0,1,2,3,4,5,6,7,8,9 with their superscript version.
1076             // Gets rid of + symbol since there is no need for it anymore.
1077             returnString = returnString.replace(/e(.*)$/g, function(match,$1){
1078                 var temp = '\u2022' + '10';
1079                 // Note: Since board ticks do not support HTTP elements like <sub>, we need to replace
1080                 // all the numbers with superscript Unicode characters.
1081                 temp +=  $1
1082                     .replace(/-/g, "\u207B")
1083                     .replace(/\+/g, '')
1084                     .replace(/0/g,'\u2070')
1085                     .replace(/1/g,'\u00B9')
1086                     .replace(/2/g,'\u00B2')
1087                     .replace(/3/g,'\u00B3')
1088                     .replace(/4/g,'\u2074')
1089                     .replace(/5/g,'\u2075')
1090                     .replace(/6/g,'\u2076')
1091                     .replace(/7/g,'\u2077')
1092                     .replace(/8/g,'\u2078')
1093                     .replace(/9/g,'\u2079');
1094 
1095                 return temp;
1096             });
1097 
1098             return returnString;
1099         },
1100 
1101         /**
1102          * Creates the label text for a given tick. A value for the text can be provided as a number or string
1103          *
1104          * @param  {JXG.Coords}    tick  The Coords-object of the tick to create a label for
1105          * @param  {JXG.Coords}    zero  The Coords-object of line's zero
1106          * @param  {Number|String} value A predefined value for this tick
1107          * @returns {String}
1108          * @private
1109          */
1110         generateLabelText: function (tick, zero, value) {
1111             var labelText, distance;
1112 
1113             // No value provided, equidistant, so assign distance as value
1114             if (!Type.exists(value)) { // could be null or undefined
1115                 distance = this.getDistanceFromZero(zero, tick);
1116                 if (Math.abs(distance) < Mat.eps) { // Point is zero
1117                     return '0';
1118                 }
1119                 value = distance / Type.evaluate(this.visProp.scale);
1120             }
1121             labelText = this.formatLabelText(value);
1122 
1123             return labelText;
1124         },
1125 
1126         /**
1127          * Create a tick label data, i.e. text and coordinates
1128          * @param  {String}     labelText
1129          * @param  {JXG.Coords} tick
1130          * @param  {Number}     tickNumber
1131          * @returns {Object} with properties 'x', 'y', 't' (text), 'i' (tick number) or null in case of o label
1132          * @private
1133          */
1134         generateLabelData: function (labelText, tick, tickNumber) {
1135              var xa, ya, m, fs;
1136 
1137              // Test if large portions of the label are inside of the canvas
1138              // This is the last chance to abandon the creation of the label if it is mostly
1139              // outside of the canvas.
1140              fs = Type.evaluate(this.visProp.label.fontsize);
1141              xa = [tick.scrCoords[1], tick.scrCoords[1]];
1142              ya = [tick.scrCoords[2], tick.scrCoords[2]];
1143              m = (fs === undefined) ? 12 : fs;
1144              m *= 0.5;
1145              if (!this._isInsideCanvas(xa, ya, m)) {
1146                  return null;
1147              }
1148 
1149              xa = Type.evaluate(this.visProp.label.offset[0]);
1150              ya = Type.evaluate(this.visProp.label.offset[1]);
1151 
1152              return {
1153                  x: tick.usrCoords[1] + xa / (this.board.unitX),
1154                  y: tick.usrCoords[2] + ya / (this.board.unitY),
1155                  t: labelText,
1156                  i: tickNumber
1157              };
1158          },
1159 
1160         /**
1161          * Recalculate the tick positions and the labels.
1162          * @returns {JXG.Ticks}
1163          */
1164         update: function () {
1165             if (this.needsUpdate) {
1166                 //this.visPropCalc.visible = Type.evaluate(this.visProp.visible);
1167                 // A canvas with no width or height will create an endless loop, so ignore it
1168                 if (this.board.canvasWidth !== 0 && this.board.canvasHeight !== 0) {
1169                     this.calculateTicksCoordinates();
1170                 }
1171                 // this.updateVisibility(this.line.visPropCalc.visible);
1172                 //
1173                 // for (var i = 0; i < this.labels.length; i++) {
1174                 //     if (this.labels[i] !== null) {
1175                 //         this.labels[i].prepareUpdate()
1176                 //             .updateVisibility(this.line.visPropCalc.visible)
1177                 //             .updateRenderer();
1178                 //     }
1179                 // }
1180             }
1181 
1182             return this;
1183         },
1184 
1185         /**
1186          * Uses the boards renderer to update the arc.
1187          * @returns {JXG.Ticks} Reference to the object.
1188          */
1189         updateRenderer: function () {
1190             if (!this.needsUpdate) {
1191                 return this;
1192             }
1193 
1194             if (this.visPropCalc.visible) {
1195                 this.board.renderer.updateTicks(this);
1196             }
1197             this.updateRendererLabels();
1198 
1199             this.setDisplayRendNode();
1200             // if (this.visPropCalc.visible != this.visPropOld.visible) {
1201             //     this.board.renderer.display(this, this.visPropCalc.visible);
1202             //     this.visPropOld.visible = this.visPropCalc.visible;
1203             // }
1204 
1205             this.needsUpdate = false;
1206             return this;
1207         },
1208 
1209         /**
1210          * Updates the label elements of the major ticks.
1211          *
1212          * @private
1213          * @returns {JXG.Ticks} Reference to the object.
1214          */
1215         updateRendererLabels: function() {
1216             var i, j,
1217                 lenData, lenLabels,
1218                 attr,
1219                 label, ld,
1220                 visible;
1221 
1222             // The number of labels needed
1223             lenData = this.labelsData.length;
1224             // The number of labels which already exist
1225             // The existing labels are stored in this.labels[]
1226             // The new label positions and label values are stored in this.labelsData[]
1227             lenLabels = this.labels.length;
1228 
1229             for (i = 0, j = 0; i < lenData; i++) {
1230                 if (this.labelsData[i] === null) {
1231                     // This is a tick without label
1232                     continue;
1233                 }
1234 
1235                 ld = this.labelsData[i];
1236                 if (j < lenLabels) {
1237                     // Take an already existing text element
1238                     label = this.labels[j];
1239                     label.setText(ld.t);
1240                     label.setCoords(ld.x, ld.y);
1241                     j++;
1242                 } else {
1243                     // A new text element is needed
1244                     this.labelCounter += 1;
1245 
1246                     attr = {
1247                         isLabel: true,
1248                         layer: this.board.options.layer.line,
1249                         highlightStrokeColor: this.board.options.text.strokeColor,
1250                         highlightStrokeWidth: this.board.options.text.strokeWidth,
1251                         highlightStrokeOpacity: this.board.options.text.strokeOpacity,
1252                         priv: this.visProp.priv
1253                     };
1254                     attr = Type.deepCopy(attr, this.visProp.label);
1255                     attr.id = this.id + ld.i + 'Label' + this.labelCounter;
1256 
1257                     label = Text.createText(this.board, [ld.x, ld.y, ld.t], attr);
1258                     label.isDraggable = false;
1259                     label.dump = false;
1260                     this.labels.push(label);
1261                 }
1262 
1263                 // Look-ahead if the label inherits visiblity.
1264                 // If yes, update label.
1265                 visible = Type.evaluate(this.visProp.label.visible);
1266                 if (visible === 'inherit') {
1267                     visible = this.visPropCalc.visible;
1268                 }
1269 
1270                 label.prepareUpdate()
1271                     .updateVisibility(visible)
1272                     .updateRenderer();
1273 
1274                 label.distanceX = Type.evaluate(this.visProp.label.offset[0]);
1275                 label.distanceY = Type.evaluate(this.visProp.label.offset[1]);
1276             }
1277 
1278             // Hide unused labels
1279             lenData = j;
1280             for (j = lenData; j < lenLabels; j++) {
1281                 this.board.renderer.display(this.labels[j], false);
1282                 // Tick labels have the attribute "visible: 'inherit'"
1283                 // This must explicitely set to false, otherwise
1284                 // this labels would be set to visible in the upcoming
1285                 // update of the labels.
1286                 this.labels[j].visProp.visible = this.labels[j].visPropCalc.visible = false;
1287             }
1288 
1289             return this;
1290         },
1291 
1292         hideElement: function () {
1293             var i;
1294 
1295             JXG.deprecated('Element.hideElement()', 'Element.setDisplayRendNode()');
1296 
1297             this.visPropCalc.visible = false;
1298             this.board.renderer.display(this, false);
1299             for (i = 0; i < this.labels.length; i++) {
1300                 if (Type.exists(this.labels[i])) {
1301                     this.labels[i].hideElement();
1302                 }
1303             }
1304 
1305             return this;
1306         },
1307 
1308         showElement: function () {
1309             var i;
1310 
1311             JXG.deprecated('Element.showElement()', 'Element.setDisplayRendNode()');
1312 
1313             this.visPropCalc.visible = true;
1314             this.board.renderer.display(this, false);
1315 
1316             for (i = 0; i < this.labels.length; i++) {
1317                 if (Type.exists(this.labels[i])) {
1318                     this.labels[i].showElement();
1319                 }
1320             }
1321 
1322             return this;
1323         }
1324     });
1325 
1326     /**
1327      * @class Ticks are used as distance markers on a line or curve.
1328      * They are
1329      * mainly used for axis elements and slider elements. Ticks may stretch infinitely
1330      * or finitely, which can be set with {@link Ticks#majorHeight} and {@link Ticks#minorHeight}.
1331      *
1332      * @pseudo
1333      * @description Ticks are markers on straight line elements or curves.
1334      * @name Ticks
1335      * @augments JXG.Ticks
1336      * @constructor
1337      * @type JXG.Ticks
1338      * @throws {Exception} If the element cannot be constructed with the given parent objects an exception is thrown.
1339      * @param {JXG.Line|JXG.Curve} line The parents consist of the line or curve the ticks are going to be attached to.
1340      * @param {Number|Array} distance Number defining the distance between two major ticks or an
1341      * array defining static ticks. In case a number is specified, the ticks are <i>equidistant</i>,
1342      * in case of an array, a fixed number of static ticks is created at user-supplied positions.
1343      * Alternatively, the distance can be specified with the attribute
1344      * "ticksDistance". For arbitrary lines (and not axes) a "zero coordinate" is determined
1345      * which defines where the first tick is positioned. This zero coordinate
1346      * can be altered with the attribute "anchor". Possible values are "left", "middle", "right" or a number.
1347      * The default value is "left".
1348      *
1349      * @example
1350      * // Create an axis providing two coordinate pairs.
1351      *   var p1 = board.create('point', [0, 3]);
1352      *   var p2 = board.create('point', [1, 3]);
1353      *   var l1 = board.create('line', [p1, p2]);
1354      *   var t = board.create('ticks', [l1], {ticksDistance: 2});
1355      * </pre><div class="jxgbox" id="JXGee7f2d68-75fc-4ec0-9931-c76918427e63" style="width: 300px; height: 300px;"></div>
1356      * <script type="text/javascript">
1357      * (function () {
1358      *   var board = JXG.JSXGraph.initBoard('JXGee7f2d68-75fc-4ec0-9931-c76918427e63', {boundingbox: [-1, 7, 7, -1], showcopyright: false, shownavigation: false});
1359      *   var p1 = board.create('point', [0, 3]);
1360      *   var p2 = board.create('point', [1, 3]);
1361      *   var l1 = board.create('line', [p1, p2]);
1362      *   var t = board.create('ticks', [l1, 2], {ticksDistance: 2});
1363      * })();
1364      * </script><pre>
1365      */
1366     JXG.createTicks = function (board, parents, attributes) {
1367         var el, dist,
1368             attr = Type.copyAttributes(attributes, board.options, 'ticks');
1369 
1370         if (parents.length < 2) {
1371             dist = attr.ticksdistance;
1372         } else {
1373             dist = parents[1];
1374         }
1375 
1376         if (parents[0].elementClass === Const.OBJECT_CLASS_LINE ||
1377             parents[0].elementClass === Const.OBJECT_CLASS_CURVE) {
1378             el = new JXG.Ticks(parents[0], dist, attr);
1379         } else {
1380             throw new Error("JSXGraph: Can't create Ticks with parent types '" + (typeof parents[0]) + "'.");
1381         }
1382 
1383         // deprecated
1384         if (Type.isFunction(attr.generatelabelvalue)) {
1385             el.generateLabelText = attr.generatelabelvalue;
1386         }
1387         if (Type.isFunction(attr.generatelabeltext)) {
1388             el.generateLabelText = attr.generatelabeltext;
1389         }
1390 
1391         el.setParents(parents[0]);
1392         el.isDraggable = true;
1393         el.fullUpdate(parents[0].visPropCalc.visible);
1394 
1395         return el;
1396     };
1397 
1398     /**
1399      * @class Hatches can be used to mark congruent lines or curves.
1400      * @pseudo
1401      * @description
1402      * @name Hatch
1403      * @augments JXG.Ticks
1404      * @constructor
1405      * @type JXG.Ticks
1406      * @throws {Exception} If the element cannot be constructed with the given parent objects an exception is thrown.
1407      * @param {JXG.Line|JXG.curve} line The line or curve the hatch marks are going to be attached to.
1408      * @param {Number} numberofhashes Number of dashes.
1409      * @example
1410      * // Create an axis providing two coords pairs.
1411      *   var p1 = board.create('point', [0, 3]);
1412      *   var p2 = board.create('point', [1, 3]);
1413      *   var l1 = board.create('line', [p1, p2]);
1414      *   var t = board.create('hatch', [l1, 3]);
1415      * </pre><div class="jxgbox" id="JXG4a20af06-4395-451c-b7d1-002757cf01be" style="width: 300px; height: 300px;"></div>
1416      * <script type="text/javascript">
1417      * (function () {
1418      *   var board = JXG.JSXGraph.initBoard('JXG4a20af06-4395-451c-b7d1-002757cf01be', {boundingbox: [-1, 7, 7, -1], showcopyright: false, shownavigation: false});
1419      *   var p1 = board.create('point', [0, 3]);
1420      *   var p2 = board.create('point', [1, 3]);
1421      *   var l1 = board.create('line', [p1, p2]);
1422      *   var t = board.create('hatch', [l1, 3]);
1423      * })();
1424      * </script><pre>
1425      *
1426      * @example
1427      * // Alter the position of the hatch
1428      *
1429      * var p = board.create('point', [-5, 0]);
1430      * var q = board.create('point', [5, 0]);
1431      * var li = board.create('line', [p, q]);
1432      * var h = board.create('hatch', [li, 2], {anchor: 0.2});
1433      *
1434      * </pre><div id="JXG05d720ee-99c9-11e6-a9c7-901b0e1b8723" class="jxgbox" style="width: 300px; height: 300px;"></div>
1435      * <script type="text/javascript">
1436      *     (function() {
1437      *         var board = JXG.JSXGraph.initBoard('JXG05d720ee-99c9-11e6-a9c7-901b0e1b8723',
1438      *             {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false});
1439      *
1440      *     var p = board.create('point', [-5, 0]);
1441      *     var q = board.create('point', [5, 0]);
1442      *     var li = board.create('line', [p, q]);
1443      *     var h = board.create('hatch', [li, 2], {anchor: 0.2});
1444      *
1445      *     })();
1446      *
1447      * </script><pre>
1448      *
1449      * @example
1450      * // Alternative hatch faces
1451      *
1452      * var li = board.create('line', [[-6,0], [6,3]]);
1453      * var h1 = board.create('hatch', [li, 2], {tickEndings: [1,1], face:'|'});
1454      * var h2 = board.create('hatch', [li, 2], {tickEndings: [1,1], face:'>', anchor: 0.3});
1455      * var h3 = board.create('hatch', [li, 2], {tickEndings: [1,1], face:'<', anchor: 0.7});
1456      *
1457      * </pre><div id="JXG974f7e89-eac8-4187-9aa3-fb8068e8384b" class="jxgbox" style="width: 300px; height: 300px;"></div>
1458      * <script type="text/javascript">
1459      *     (function() {
1460      *         var board = JXG.JSXGraph.initBoard('JXG974f7e89-eac8-4187-9aa3-fb8068e8384b',
1461      *             {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false});
1462      *     // Alternative hatch faces
1463      *
1464      *     var li = board.create('line', [[-6,0], [6,3]]);
1465      *     var h1 = board.create('hatch', [li, 2], {tickEndings: [1,1], face:'|'});
1466      *     var h2 = board.create('hatch', [li, 2], {tickEndings: [1,1], face:'>', anchor: 0.3});
1467      *     var h3 = board.create('hatch', [li, 2], {tickEndings: [1,1], face:'<', anchor: 0.7});
1468      *
1469      *     })();
1470      *
1471      * </script><pre>
1472      *
1473      */
1474     JXG.createHatchmark = function (board, parents, attributes) {
1475         var num, i, base, width, totalwidth, el,
1476             pos = [],
1477             attr = Type.copyAttributes(attributes, board.options, 'hatch');
1478 
1479         if ((parents[0].elementClass !== Const.OBJECT_CLASS_LINE &&
1480             parents[0].elementClass !== Const.OBJECT_CLASS_CURVE) || typeof parents[1] !== 'number') {
1481             throw new Error("JSXGraph: Can't create Hatch mark with parent types '" + (typeof parents[0]) + "' and '" + (typeof parents[1]) + " and ''" + (typeof parents[2]) + "'.");
1482         }
1483 
1484         num = parents[1];
1485         width = attr.ticksdistance;
1486         totalwidth = (num - 1) * width;
1487         base = -totalwidth * 0.5;
1488 
1489         for (i = 0; i < num; i++) {
1490             pos[i] = base + i * width;
1491         }
1492 
1493         el = board.create('ticks', [parents[0], pos], attr);
1494         el.elType = 'hatch';
1495 
1496         return el;
1497     };
1498 
1499     JXG.registerElement('ticks', JXG.createTicks);
1500     JXG.registerElement('hash', JXG.createHatchmark);
1501     JXG.registerElement('hatch', JXG.createHatchmark);
1502 
1503     return {
1504         Ticks: JXG.Ticks,
1505         createTicks: JXG.createTicks,
1506         createHashmark: JXG.createHatchmark,
1507         createHatchmark: JXG.createHatchmark
1508     };
1509 });
1510