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  see define call
 38  */
 39 
 40 /**
 41  * @fileoverview Example file for a triangle implemented as a extension to JSXGraph.
 42  */
 43 
 44 define([
 45     'jxg', 'options', 'utils/type', 'base/constants', 'base/line', 'base/polygon', 'base/point', 'base/element'
 46 ], function (JXG, Options, Type, Const, Line, Polygon, Point, GeometryElement) {
 47 
 48     "use strict";
 49 
 50     var priv = {
 51             removeSlopeTriangle: function () {
 52                 Polygon.Polygon.prototype.remove.call(this);
 53 
 54                 this.board.removeObject(this.toppoint);
 55                 this.board.removeObject(this.glider);
 56 
 57                 this.board.removeObject(this.baseline);
 58                 this.board.removeObject(this.basepoint);
 59             },
 60             Value: function () {
 61                 return this.tangent.getSlope();
 62             }
 63         };
 64 
 65     // default attributes
 66     Options.slopetriangle = {
 67         fillColor: 'red',
 68         fillOpacity: 0.4,
 69 
 70         glider: {},
 71         baseline: {
 72             visible: false,
 73             withLabel: false,
 74             name: ''
 75         },
 76         basepoint: {
 77             visible: false,
 78             withLabel: false,
 79             name: ''
 80         },
 81         toppoint: {
 82             visible: false,
 83             withLabel: false,
 84             name: ''
 85         }
 86     };
 87 
 88     /**
 89      * Creates a new slope triangle using a tangent on a curve, circle, line or turtle.
 90      * @param {JXG.Board} board The board the triangle is put on.
 91      * @param {Array} parents A tangent line.
 92      * @param {Object} attributes Visual properties that are assigned to the constructed lines.
 93      * @returns {JXG.Point} A glider
 94      */
 95     JXG.createSlopeTriangle = function (board, parents, attributes) {
 96         var el, tangent, tglide, glider, toppoint, baseline, basepoint, attr;
 97 
 98         if (parents[0].type === Const.OBJECT_TYPE_TANGENT) {
 99             tangent = parents[0];
100             tglide = tangent.glider;
101 
102             attr = Type.copyAttributes(attributes, board.options, 'slopetriangle', 'basepoint');
103             basepoint = board.create('point', [function () {
104                 return [tglide.X() + 1,  tglide.Y()];
105             }], attr);
106 
107             attr = Type.copyAttributes(attributes, board.options, 'slopetriangle', 'baseline');
108             baseline = board.create('line', [tglide, basepoint], attr);
109 
110             attr = Type.copyAttributes(attributes, board.options, 'slopetriangle', 'glider');
111             glider = board.create('glider', [tglide.X() + 1, tglide.Y(), baseline], attr);
112 
113             attr = Type.copyAttributes(attributes, board.options, 'slopetriangle', 'toppoint');
114             toppoint = board.create('point', [function () {
115                 return [glider.X(), glider.Y() + (glider.X() - tglide.X()) * tangent.getSlope()];
116             }], attr);
117 
118             attr = Type.copyAttributes(attributes, board.options, 'slopetriangle');
119             el = board.create('polygon', [tglide, glider, toppoint], attr);
120 
121             el.Value = priv.Value;
122 
123             el.tangent = tangent;
124             el.glider = glider;
125             el.basepoint = basepoint;
126             el.baseline = baseline;
127             el.toppoint = toppoint;
128 
129             el.methodMap = JXG.deepCopy(el.methodMap, {
130                 tangent: 'tangent',
131                 glider: 'glider',
132                 basepoint: 'basepoint',
133                 baseline: 'baseline',
134                 toppoint: 'toppoint',
135                 Value: 'Value',
136                 V: 'Value'
137             });
138 
139             el.remove = priv.removeSlopeTriangle;
140 
141             return el;
142         }
143 
144         throw new Error("JSXGraph: Can't create slope triangle with parent types '" + (typeof parents[0]) + "'.");
145     };
146 
147     JXG.registerElement('slopetriangle', JXG.createSlopeTriangle);
148 
149     return {
150         createSlopeTriangle: JXG.createSlopeTriangle
151     };
152 });
153