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 JXG.createSquare = function(board, parentArr, atts) {
 27     var p1, p2, p3, p4, l1, l2, l3, l4, ret, i;
 28     
 29     if(JXG.isPoint(parentArr[0]) && JXG.isPoint(parentArr[1])) {
 30         
 31         p1 = parentArr[0];
 32         p2 = parentArr[1];
 33         
 34         p3 = board.create('point', [function () { return (-p1.Y() + (p1.X() + p2.X())/2 + (p1.Y() + p2.Y())/2); }, function () { return (p1.X() - (p1.X() + p2.X())/2 + (p1.Y() + p2.Y())/2);}]);
 35         p4 = board.create('point', [function () { return (-p2.Y() + (p1.X() + p2.X())/2 + (p1.Y() + p2.Y())/2); }, function () { return (p2.X() - (p1.X() + p2.X())/2 + (p1.Y() + p2.Y())/2);}]);
 36         
 37         l1 = board.create('line', [p1, p3], {straightFirst: false, straightLast: false});
 38         l2 = board.create('line', [p1, p4], {straightFirst: false, straightLast: false});
 39         l3 = board.create('line', [p2, p3], {straightFirst: false, straightLast: false});
 40         l4 = board.create('line', [p2, p4], {straightFirst: false, straightLast: false}); 
 41         
 42         ret = [p1, p2, p3, p4, l1, l2, l3, l4];
 43         ret.points = [p1, p2, p3, p4];
 44         ret.lines = [l1, l2, l3, l4];
 45 
 46         for(i = 1; i <= 4; i++) {
 47             ret['point'+i] = ret.points[i-1];
 48             ret['line'+i] = ret.lines[i-1];
 49         }
 50         ret.multipleElements = true;
 51 
 52         return ret;
 53     } else {
 54         throw new Error("JSXGraph: Can't create square with parent types '" + (typeof parentArr[0]) + "' and '" + (typeof parentArr[1]) + "'.");    
 55     }
 56 };
 57 
 58 JXG.JSXGraph.registerElement('square', JXG.createSquare);
 59