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 centroid implemented as an extension to JSXGraph. 
 28  */
 29  
 30 /**
 31  * @class The centroid marks the center of three points.
 32  * @pseudo
 33  * @name Centroid
 34  * @augments JXG.Point
 35  * @constructor
 36  * @type JXG.Point
 37  * @throws {Error} If the element cannot be constructed with the given parent objects an exception is thrown.
 38  * @param {JXG.Point_JXG.Point_JXG.Point} p1,p2,p3 The result is the center of this three points
 39  * @example
 40  * // Create a centroid out of three free points
 41  * var p1 = board.create('point', [2.0, 2.0]),
 42  *     p2 = board.create('point', [1.0, 0.5]),
 43  *     p3 = board.create('point', [3.5, 1.0]),
 44  *
 45  *     c = board.create('centroid', [p1, p2, p3]);
 46  * </pre><div id="e86dd688-3a75-413f-9173-1776ee92416f" style="width: 300px; height: 300px;"></div>
 47  * <script type="text/javascript">
 48  * (function () {
 49  *   var board = JXG.JSXGraph.initBoard('e86dd688-3a75-413f-9173-1776ee92416f', {boundingbox: [-1, 7, 7, -1], axis: true, showcopyright: false, shownavigation: false}),
 50  *     p1 = board.create('point', [2.0, 2.0]),
 51  *     p2 = board.create('point', [1.0, 0.5]),
 52  *     p3 = board.create('point', [3.5, 1.0]),
 53  *
 54  *     c = board.create('centroid', [p1, p2, p3]);
 55  * })();
 56  * </script><pre>
 57  */
 58 JXG.createCentroid = function(board, parents, attributes) {
 59     
 60     if(JXG.isPoint(parents[0]) && JXG.isPoint(parents[1]) && JXG.isPoint(parents[2])) {
 61         var p1 = parents[0],
 62             p2 = parents[1],
 63             p3 = parents[2],
 64             attr = JXG.copyAttributes(attributes, board.options, 'point');
 65         
 66         var cent = board.create('point', [function () {return (p1.X() + p2.X() + p3.X())/3;}, function () {return (p1.Y() + p2.Y() + p3.Y())/3;}], attr);
 67         p1.addChild(cent);
 68         p2.addChild(cent);
 69         p3.addChild(cent);
 70 
 71         cent.elType = 'centroid';
 72         cent.parents = [parents[0].id, parents[1].id, parents[2].id];
 73 
 74         /**
 75          * The first one of the points given as parent elements.
 76          * @name Centroid#p1
 77          * @type JXG.Point
 78          */
 79         cent.p1 = p1;
 80 
 81         /**
 82          * The second one of the points given as parent elements.
 83          * @name Centroid#p2
 84          * @type JXG.Point
 85          */
 86         cent.p2 = p2;
 87 
 88         /**
 89          * The last one of the points given as parent elements.
 90          * @name Centroid#p3
 91          * @type JXG.Point
 92          */
 93         cent.p3 = p3;
 94 
 95         /**
 96          * documented in geometry element
 97          * @ignore
 98          */
 99         cent.generatePolynom = function() {
100             /* TODO generate polynom*/
101         };
102         
103         return cent;
104     } else {
105         throw new Error("JSXGraph: Can't create centroid with parent types '" + (typeof parents[0]) + "' and '" + (typeof parents[1]) + "' and '" + (typeof parents[2]) + "'.");    
106     }
107 };
108 
109 JXG.JSXGraph.registerElement('centroid', JXG.createCentroid);
110