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, attr; 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 attr = JXG.copyAttributes(attributes, board.options, 'line'); 57 58 l1 = board.create('line', [p1, p2], attr); 59 l2 = board.create('line', [p2, p3], attr); 60 l3 = board.create('line', [p3, p1], attr); 61 62 var g = board.create('group', [p1, p2, p3]); 63 64 ret = [p1, p2, p3, l1, l2, l3, g]; 65 ret.points = [p1, p2, p3]; 66 ret.lines = [l1, l2, l3]; 67 ret.group = g; 68 69 for(i=1; i<=3; i++) { 70 ret['point'+i] = ret.points[i-1]; 71 ret['line'+i] = ret.lines[i-1]; 72 } 73 ret.multipleElements = true; 74 75 // special treatment for triangle because of backwards compatibility: 76 ret.A = p1; ret.B = p2; ret.C = p3; 77 ret.a = l2; ret.b = l3; ret.c = l1; 78 79 return ret; 80 } else { 81 throw new Error("JSXGraph: Can't create triangle with parent types '" + (typeof parents[0]) + "' and '" + (typeof parents[1]) + "' and '" + (typeof parents[2]) + "'."); 82 } 83 }; 84 85 JXG.JSXGraph.registerElement('triangle', JXG.createTriangle); 86