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.extend(JXG.Options, { 27 square: { 28 points: { 29 withLabel: false, 30 visible: false, 31 name: '' 32 }, 33 lines: { 34 straightFirst: false, 35 straightLast: false, 36 withLabel: true 37 } 38 } 39 }); 40 41 /** 42 * @class A square is a rectangle with four sides each of the same length. 43 * @pseudo 44 * @name Square 45 * @augments JXG.Composition 46 * @constructor 47 * @type JXG.Composition 48 * @throws {Error} If the element cannot be constructed with the given parent objects an exception is thrown. 49 * @param {JXG.Point_JXG.Point} p1,p2 To build the square two additional points will be created. Those will be placed 50 * at a pi/2 rotation of the two given points around the center of the square. 51 * @example 52 * var p1 = board.create('point', [2.0, 2.0]), 53 * p2 = board.create('point', [1.0, 0.5]), 54 * 55 * a = board.create('square', [p1, p2]); 56 * </pre><div id="3558cfad-59f0-4385-8602-7ac14cded0df" style="width: 300px; height: 300px;"></div> 57 * <script type="text/javascript"> 58 * (function () { 59 * var board = JXG.JSXGraph.initBoard('3558cfad-59f0-4385-8602-7ac14cded0df', {boundingbox: [-1, 7, 7, -1], axis: true, showcopyright: false, shownavigation: false}), 60 * p1 = board.create('point', [2.0, 2.0]), 61 * p2 = board.create('point', [1.0, 0.5]), 62 * 63 * a = board.create('square', [p1, p2]); 64 * })(); 65 * </script><pre> 66 */ 67 JXG.createSquare = function(board, parents, attributes) { 68 var p1, p2, p3, p4, l1, l2, l3, l4, ret, i, attr; 69 70 if(JXG.isPoint(parents[0]) && JXG.isPoint(parents[1])) { 71 p1 = parents[0]; 72 p2 = parents[1]; 73 74 attr = JXG.copyAttributes(attributes, board.options, 'square', 'points'); 75 76 p3 = board.create('point', [ 77 function () { 78 return (-p1.Y() + (p1.X() + p2.X())/2 + (p1.Y() + p2.Y())/2); 79 }, 80 function () { 81 return (p1.X() - (p1.X() + p2.X())/2 + (p1.Y() + p2.Y())/2); 82 } 83 ], attr); 84 85 p4 = board.create('point', [ 86 function () { 87 return (-p2.Y() + (p1.X() + p2.X())/2 + (p1.Y() + p2.Y())/2); 88 }, 89 function () { 90 return (p2.X() - (p1.X() + p2.X())/2 + (p1.Y() + p2.Y())/2); 91 } 92 ], attr); 93 94 attr = JXG.copyAttributes(attributes, board.options, 'square', 'lines'); 95 l1 = board.create('line', [p1, p3], attr); 96 l2 = board.create('line', [p1, p4], attr); 97 l3 = board.create('line', [p2, p3], attr); 98 l4 = board.create('line', [p2, p4], attr); 99 100 ret = new JXG.Composition(/** @lends Square.prototype */{ 101 /** 102 * Contains the square's points. The first two points are the ones given as parent elements by the user. 103 * The third and the fourth point are a rotation of pi/2 of the first resp. the second point around the 104 * center of the square. 105 * @type Array 106 */ 107 points: [p1, p2, p3, p4], 108 109 /** 110 * Contains the square's stroke lines. The first line is the one from the point 1 to point 3, the second line 111 * the one from point 1 to point 4. The same with the third and the fourth line but with point 2 instead of 112 * point 1. The order of the points is the same as in {@link Square#points}. 113 * @type Array 114 */ 115 lines: [l1, l2, l3, l4] 116 }); 117 118 for(i = 1; i <= 4; i++) { 119 ret['point'+i] = ret.points[i-1]; 120 ret['line'+i] = ret.lines[i-1]; 121 } 122 123 return ret; 124 } else { 125 throw new Error("JSXGraph: Can't create square with parent types '" + (typeof parents[0]) + "' and '" + (typeof parents[1]) + "'."); 126 } 127 }; 128 129 JXG.JSXGraph.registerElement('square', JXG.createSquare); 130