      1 /*
      2     Copyright 2008-2013
      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 atape 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 id="6d9a2cda-22fe-4cd1-9d94-34283b1bdc01" style="width: 200px; height: 200px;"></div>
     76      * <script type="text/javascript">
     77      *   (function () {
     78      *     var board = JXG.JSXGraph.initBoard('6d9a2cda-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         attr = Type.copyAttributes(attributes, board.options, 'tapemeasure');
     95         withTicks = attr.withticks;
     96         withText = attr.withlabel;
     97         precision = attr.precision;
     98
     99         // start point
    100         attr = Type.copyAttributes(attributes, board.options, 'tapemeasure', 'point1');
    101         p1 = board.create('point', pos0,  attr);
    102
    103         // end point
    104         attr = Type.copyAttributes(attributes, board.options, 'tapemeasure', 'point2');
    105         p2 = board.create('point', pos1,  attr);
    106
    107         // tape measure line
    108         attr = Type.copyAttributes(attributes, board.options, 'tapemeasure');
    109
    110         // Below, we will replace the label by the measurement function.
    111         if (withText) {
    112             attr.withlabel = true;
    113         }
    114         li = board.create('segment', [p1, p2], attr);
    115
    116         if (withText) {
    117             if (attributes.name && attributes.name !== '') {
    118                 n = attributes.name + ' = ';
    119             } else {
    120                 n = '';
    121             }
    122             li.label.setText(function () {
    123                 return n + p1.Dist(p2).toFixed(precision);
    124             });
    125         }
    126
    127         if (withTicks) {
    128             attr = Type.copyAttributes(attributes, board.options, 'tapemeasure', 'ticks');
    129             //ticks  = 2;
    130             ti = board.create('ticks', [li, 0.1], attr);
    131         }
    132
    133         // override the segments's remove method to ensure the removal of all elements
    134         /** @ignore */
    135         li.remove = function () {
    136             if (withTicks) {
    137                 li.removeTicks(ti);
    138             }
    139
    140             board.removeObject(p2);
    141             board.removeObject(p1);
    142
    143             GeometryElement.prototype.remove.call(this);
    144         };
    145
    146         /** @ignore */
    147         li.Value = function () {
    148             return p1.Dist(p2);
    149         };
    150
    151         p1.dump = false;
    152         p2.dump = false;
    153
    154         li.elType = 'tapemeasure';
    155         li.parents = parents;
    156         li.subs = {
    157             point1: p1,
    158             point2: p2
    159         };
    160
    161         if (withTicks) {
    162             ti.dump = false;
    163         }
    164
    165         li.methodMap = JXG.deepCopy(li.methodMap, {
    166             Value: 'Value'
    167         });
    168
    169         return li;
    170     };
    171
    172     JXG.registerElement('tapemeasure', JXG.createTapemeasure);
    173
    174     return {
    175         createTapemeasure: JXG.createTapemeasure
    176     };
    177 });
    178