1 /* 2 Copyright 2008-2016 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 class="jxgbox"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 // 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 117 if (withText) { 118 if (attributes.name && attributes.name !== '') { 119 n = attributes.name + ' = '; 120 } else { 121 n = ''; 122 } 123 li.label.setText(function () { 124 return n + p1.Dist(p2).toFixed(precision); 125 }); 126 } 127 128 if (withTicks) { 129 attr = Type.copyAttributes(attributes, board.options, 'tapemeasure', 'ticks'); 130 //ticks = 2; 131 ti = board.create('ticks', [li, 0.1], attr); 132 } 133 134 // override the segments's remove method to ensure the removal of all elements 135 /** @ignore */ 136 li.remove = function () { 137 if (withTicks) { 138 li.removeTicks(ti); 139 } 140 141 board.removeObject(p2); 142 board.removeObject(p1); 143 144 GeometryElement.prototype.remove.call(this); 145 }; 146 147 /** @ignore */ 148 li.Value = function () { 149 return p1.Dist(p2); 150 }; 151 152 p1.dump = false; 153 p2.dump = false; 154 155 li.elType = 'tapemeasure'; 156 li.getParents = function() { 157 return [[p1.X(), p1.Y()], [p2.X(), p2.Y()]]; 158 }; 159 160 li.subs = { 161 point1: p1, 162 point2: p2 163 }; 164 165 if (withTicks) { 166 ti.dump = false; 167 } 168 169 li.methodMap = JXG.deepCopy(li.methodMap, { 170 Value: 'Value' 171 }); 172 173 return li; 174 }; 175 176 JXG.registerElement('tapemeasure', JXG.createTapemeasure); 177 178 return { 179 createTapemeasure: JXG.createTapemeasure 180 }; 181 }); 182