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  utils/type
 39  base/element
 40   elements:
 41    point
 42    segment
 43    ticks
 44  */
 45 
 46 /**
 47  * @fileoverview Geometry objects for measurements are defined in this file. This file stores all
 48  * style and functional properties that are required to use a tape measure on
 49  * a board.
 50  */
 51 
 52 define([
 53     'jxg', 'utils/type', 'base/element', 'base/point', 'base/line', 'base/ticks'
 54 ], function (JXG, Type, GeometryElement, Point, Line, Ticks) {
 55 
 56     "use strict";
 57 
 58     /**
 59      * @class A tape measure can be used to measure distances between points.
 60      * @pseudo
 61      * @description
 62      * @name Tapemeasure
 63      * @augments Segment
 64      * @constructor
 65      * @type JXG.Segment
 66      * @throws {Exception} If the element cannot be constructed with the given parent objects an exception is thrown.
 67      * @param {Array_Array} start,end, The two arrays give the initial position where the tape measure
 68      * is drawn on the board.
 69      * @example
 70      * // Create a tape measure
 71      * var p1 = board.create('point', [0,0]);
 72      * var p2 = board.create('point', [1,1]);
 73      * var p3 = board.create('point', [3,1]);
 74      * var tape = board.create('tapemeasure', [[1, 2], [4, 2]], {name:'dist'});
 75      * </pre><div class="jxgbox" id="JXG6d9a2cda-22fe-4cd1-9d94-34283b1bdc01" style="width: 200px; height: 200px;"></div>
 76      * <script type="text/javascript">
 77      *   (function () {
 78      *     var board = JXG.JSXGraph.initBoard('JXG6d9a2cda-22fe-4cd1-9d94-34283b1bdc01', {boundingbox: [-1, 5, 5, -1], axis: true, showcopyright: false, shownavigation: false});
 79      *     var p1 = board.create('point', [0,0]);
 80      *     var p2 = board.create('point', [1,1]);
 81      *     var p3 = board.create('point', [3,1]);
 82      *     var tape = board.create('tapemeasure', [[1, 2], [4, 2]], {name:'dist'} );
 83      *   })();
 84      * </script><pre>
 85      */
 86     JXG.createTapemeasure = function (board, parents, attributes) {
 87         var pos0, pos1,
 88             attr, withTicks, withText, precision,
 89             li, p1, p2, n, ti;
 90 
 91         pos0 = parents[0];
 92         pos1 = parents[1];
 93 
 94         // start point
 95         attr = Type.copyAttributes(attributes, board.options, 'tapemeasure', 'point1');
 96         p1 = board.create('point', pos0,  attr);
 97 
 98         // end point
 99         attr = Type.copyAttributes(attributes, board.options, 'tapemeasure', 'point2');
100         p2 = board.create('point', pos1,  attr);
101 
102         p1.setAttribute({ignoredSnapToPoints: [p2]});
103         p2.setAttribute({ignoredSnapToPoints: [p1]});
104 
105         // tape measure line
106         attr = Type.copyAttributes(attributes, board.options, 'tapemeasure');
107         withTicks = attr.withticks;
108         withText = attr.withlabel;
109         precision = attr.precision;
110 
111         // Below, we will replace the label by the measurement function.
112         if (withText) {
113             attr.withlabel = true;
114         }
115         li = board.create('segment', [p1, p2], attr);
116         // p1, p2 are already added to li.inherits
117 
118         if (withText) {
119             if (attributes.name && attributes.name !== '') {
120                 n = attributes.name + ' = ';
121             } else {
122                 n = '';
123             }
124             li.label.setText(function () {
125                 return n + Type.toFixed(p1.Dist(p2), precision);
126             });
127         }
128 
129         if (withTicks) {
130             attr = Type.copyAttributes(attributes, board.options, 'tapemeasure', 'ticks');
131             //ticks  = 2;
132             ti = board.create('ticks', [li, 0.1], attr);
133             li.inherits.push(ti);
134         }
135 
136         // override the segments's remove method to ensure the removal of all elements
137         /** @ignore */
138         li.remove = function () {
139             if (withTicks) {
140                 li.removeTicks(ti);
141             }
142 
143             board.removeObject(p2);
144             board.removeObject(p1);
145 
146             GeometryElement.prototype.remove.call(this);
147         };
148 
149         /**
150          * Returns the length of the tape measure.
151          * @name Value
152          * @memberOf Tapemeasure.prototype
153          * @function
154          * @returns {Number} length of tape measure.
155          */
156         li.Value = function () {
157             return p1.Dist(p2);
158         };
159 
160         p1.dump = false;
161         p2.dump = false;
162 
163         li.elType = 'tapemeasure';
164         li.getParents = function() {
165             return [[p1.X(), p1.Y()], [p2.X(), p2.Y()]];
166         };
167 
168         li.subs = {
169             point1: p1,
170             point2: p2
171         };
172 
173         if (withTicks) {
174             ti.dump = false;
175         }
176 
177         li.methodMap = JXG.deepCopy(li.methodMap, {
178             Value: 'Value'
179         });
180 
181         li.prepareUpdate().update();
182         if (!board.isSuspendedUpdate) {
183             li.updateVisibility().updateRenderer();
184             // The point updates are necessary in case of snapToGrid==true
185             li.point1.updateVisibility().updateRenderer();
186             li.point2.updateVisibility().updateRenderer();
187         }
188 
189         return li;
190     };
191 
192     JXG.registerElement('tapemeasure', JXG.createTapemeasure);
193 
194     return {
195         createTapemeasure: JXG.createTapemeasure
196     };
197 });
198