1 /*
  2     Copyright 2008,2009
  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: you can redistribute it and/or modify
 13     it under the terms of the GNU Lesser General Public License as published by
 14     the Free Software Foundation, either version 3 of the License, or
 15     (at your option) any later version.
 16 
 17     JSXGraph is distributed in the hope that it will be useful,
 18     but WITHOUT ANY WARRANTY; without even the implied warranty of
 19     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 20     GNU Lesser General Public License for more details.
 21 
 22     You should have received a copy of the GNU Lesser General Public License
 23     along with JSXGraph.  If not, see <http://www.gnu.org/licenses/>.
 24 */
 25 
 26 /**
 27  * @fileoverview Example file for a triangle implemented as a extension to JSXGraph.
 28  */
 29 
 30 /**
 31  * Creates a new triangle using three points and the given attributes.
 32  * @param {JXG.Board} board The board the triangle is put on.
 33  * @param {Array} parents Array of three points defining the triangle.
 34  * @param {Object} attributes Visual properties that are assigned to the constructed lines.
 35  * @type Object
 36  * @return An object with the following members: <br />
 37  * <table><tr><th>Type</th><th>Member</th></tr>
 38  *   <tr><td>JXG.Point</td><td>A</td></tr>
 39  *   <tr><td>JXG.Point</td><td>B</td></tr>
 40  *   <tr><td>JXG.Point</td><td>C</td></tr>
 41  *   <tr><td>JXG.Line</td><td>a</td></tr>
 42  *   <tr><td>JXG.Line</td><td>b</td></tr>
 43  *   <tr><td>JXG.Line</td><td>c</td></tr>
 44  *   <tr><td>JXG.Group</td><td>G</td></tr>
 45  * </table>
 46  */
 47 JXG.createTriangle = function(board, parents, attributes) {
 48 
 49     var p1, p2, p3, l1, l2, l3, ret, i;
 50 
 51     if(JXG.isPoint(parents[0]) && JXG.isPoint(parents[1]) && JXG.isPoint(parents[2])) {
 52         p1 = parents[0];
 53         p2 = parents[1];
 54         p3 = parents[2];
 55 
 56         attributes = JXG.checkAttributes(attributes,{});
 57         attributes.straightFirst = false;
 58         attributes.straightLast = false;
 59 
 60         l1 = board.create('line', [p1, p2], attributes);
 61         l2 = board.create('line', [p2, p3], attributes);
 62         l3 = board.create('line', [p3, p1], attributes);
 63 
 64         var g = board.create('group', [p1, p2, p3]);
 65 //        g.addPoints([p1, p2, p3]);
 66 
 67         ret = [p1, p2, p3, l1, l2, l3, g];
 68         ret.points = [p1, p2, p3];
 69         ret.lines = [l1, l2, l3];
 70         ret.group = g;
 71         for(i=1; i<=3; i++) {
 72             ret['point'+i] = ret.points[i-1];
 73             ret['line'+i] = ret.lines[i-1];
 74         }
 75         ret.multipleElements = true;
 76 
 77         // special treatment for triangle because of backwards compatibility:
 78         ret.A = p1; ret.B = p2; ret.C = p3;
 79         ret.a = l2; ret.b = l3; ret.c = l1;
 80 
 81         return ret;
 82     } else {
 83         throw new Error("JSXGraph: Can't create triangle with parent types '" + (typeof parents[0]) + "' and '" + (typeof parents[1]) + "' and '" + (typeof parents[2]) + "'.");
 84     }
 85 };
 86 
 87 JXG.JSXGraph.registerElement('triangle', JXG.createTriangle);
 88