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