1 /* 2 Copyright 2008-2022 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 dual licensed under the GNU LGPL or MIT License. 13 14 You can redistribute it and/or modify it under the terms of the 15 16 * GNU Lesser General Public License as published by 17 the Free Software Foundation, either version 3 of the License, or 18 (at your option) any later version 19 OR 20 * MIT License: https://github.com/jsxgraph/jsxgraph/blob/master/LICENSE.MIT 21 22 JSXGraph is distributed in the hope that it will be useful, 23 but WITHOUT ANY WARRANTY; without even the implied warranty of 24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 25 GNU Lesser General Public License for more details. 26 27 You should have received a copy of the GNU Lesser General Public License and 28 the MIT License along with JSXGraph. If not, see <http://www.gnu.org/licenses/> 29 and <http://opensource.org/licenses/MIT/>. 30 */ 31 32 33 /*global JXG: true, define: true, console: true, window: true*/ 34 /*jslint nomen: true, plusplus: true*/ 35 36 /* depends: 37 jxg 38 options 39 math/math 40 math/geometry 41 math/numerics 42 base/coords 43 base/constants 44 base/element 45 parser/geonext 46 utils/type 47 elements: 48 transform 49 */ 50 51 /** 52 * @fileoverview The geometry object Point is defined in this file. Point stores all 53 * style and functional properties that are required to draw and move a point on 54 * a board. 55 */ 56 57 define([ 58 'jxg', 'options', 'math/math', 'math/geometry', 'base/constants', 'base/element', 59 'utils/type', 'base/coordselement' 60 ], function (JXG, Options, Mat, Geometry, Const, GeometryElement, Type, CoordsElement) { 61 62 "use strict"; 63 64 /** 65 * A point is the basic geometric element. Based on points lines and circles can be constructed which can be intersected 66 * which in turn are points again which can be used to construct new lines, circles, polygons, etc. This class holds methods for 67 * all kind of points like free points, gliders, and intersection points. 68 * @class Creates a new point object. Do not use this constructor to create a point. Use {@link JXG.Board#create} with 69 * type {@link Point}, {@link Glider}, or {@link Intersection} instead. 70 * @augments JXG.GeometryElement 71 * @augments JXG.CoordsElement 72 * @param {string|JXG.Board} board The board the new point is drawn on. 73 * @param {Array} coordinates An array with the user coordinates of the point. 74 * @param {Object} attributes An object containing visual properties like in {@link JXG.Options#point} and 75 * {@link JXG.Options#elements}, and optional a name and an id. 76 * @see JXG.Board#generateName 77 */ 78 JXG.Point = function (board, coordinates, attributes) { 79 this.constructor(board, attributes, Const.OBJECT_TYPE_POINT, Const.OBJECT_CLASS_POINT); 80 this.element = this.board.select(attributes.anchor); 81 this.coordsConstructor(coordinates); 82 83 this.elType = 'point'; 84 85 /* Register point at board. */ 86 this.id = this.board.setId(this, 'P'); 87 this.board.renderer.drawPoint(this); 88 this.board.finalizeAdding(this); 89 90 this.createLabel(); 91 92 }; 93 94 /** 95 * Inherits here from {@link JXG.GeometryElement}. 96 */ 97 JXG.Point.prototype = new GeometryElement(); 98 Type.copyPrototypeMethods(JXG.Point, CoordsElement, 'coordsConstructor'); 99 100 JXG.extend(JXG.Point.prototype, /** @lends JXG.Point.prototype */ { 101 /** 102 * Checks whether (x,y) is near the point. 103 * @param {Number} x Coordinate in x direction, screen coordinates. 104 * @param {Number} y Coordinate in y direction, screen coordinates. 105 * @returns {Boolean} True if (x,y) is near the point, False otherwise. 106 * @private 107 */ 108 hasPoint: function (x, y) { 109 var coordsScr = this.coords.scrCoords, r, 110 prec, type, 111 unit = Type.evaluate(this.visProp.sizeunit); 112 113 if (Type.isObject(Type.evaluate(this.visProp.precision))) { 114 type = this.board._inputDevice; 115 prec = Type.evaluate(this.visProp.precision[type]); 116 } else { 117 // 'inherit' 118 prec = this.board.options.precision.hasPoint; 119 } 120 r = parseFloat(Type.evaluate(this.visProp.size)); 121 if (unit === 'user') { 122 r *= Math.sqrt(this.board.unitX * this.board.unitY); 123 } 124 125 r += parseFloat(Type.evaluate(this.visProp.strokewidth)) * 0.5; 126 if (r < prec) { 127 r = prec; 128 } 129 130 return ((Math.abs(coordsScr[1] - x) < r + 2) && (Math.abs(coordsScr[2] - y) < r + 2)); 131 }, 132 133 /** 134 * Updates the position of the point. 135 */ 136 update: function (fromParent) { 137 if (!this.needsUpdate) { 138 return this; 139 } 140 141 this.updateCoords(fromParent); 142 143 if (Type.evaluate(this.visProp.trace)) { 144 this.cloneToBackground(true); 145 } 146 147 return this; 148 }, 149 150 /** 151 * Applies the transformations of the element to {@link JXG.Point#baseElement}. 152 * Point transformations are relative to a base element. 153 * @param {Boolean} fromParent True if the drag comes from a child element. This is the case if a line 154 * through two points is dragged. Otherwise, the element is the drag element and we apply the 155 * the inverse transformation to the baseElement if is different from the element. 156 * @returns {JXG.CoordsElement} Reference to this object. 157 */ 158 updateTransform: function (fromParent) { 159 var c, i; 160 161 if (this.transformations.length === 0 || this.baseElement === null) { 162 return this; 163 } 164 165 if (this === this.baseElement) { 166 // Case of bindTo 167 c = this.transformations[0].apply(this.baseElement, 'self'); 168 this.coords.setCoordinates(Const.COORDS_BY_USER, c); 169 } else { 170 c = this.transformations[0].apply(this.baseElement); 171 } 172 this.coords.setCoordinates(Const.COORDS_BY_USER, c); 173 174 for (i = 1; i < this.transformations.length; i++) { 175 this.coords.setCoordinates(Const.COORDS_BY_USER, this.transformations[i].apply(this)); 176 } 177 return this; 178 }, 179 180 /** 181 * Calls the renderer to update the drawing. 182 * @private 183 */ 184 updateRenderer: function () { 185 this.updateRendererGeneric('updatePoint'); 186 return this; 187 }, 188 189 // documented in JXG.GeometryElement 190 bounds: function () { 191 return this.coords.usrCoords.slice(1).concat(this.coords.usrCoords.slice(1)); 192 }, 193 194 /** 195 * Convert the point to intersection point and update the construction. 196 * To move the point visual onto the intersection, a call of board update is necessary. 197 * 198 * @param {String|Object} el1, el2, i, j The intersecting objects and the numbers. 199 **/ 200 makeIntersection: function (el1, el2, i, j) { 201 var func; 202 203 el1 = this.board.select(el1); 204 el2 = this.board.select(el2); 205 206 func = Geometry.intersectionFunction(this.board, el1, el2, i, j, 207 Type.evaluate(this.visProp.alwaysintersect)); 208 this.addConstraint([func]); 209 210 try { 211 el1.addChild(this); 212 el2.addChild(this); 213 } catch (e) { 214 throw new Error("JSXGraph: Can't create 'intersection' with parent types '" + 215 (typeof el1) + "' and '" + (typeof el2) + "'."); 216 } 217 218 this.type = Const.OBJECT_TYPE_INTERSECTION; 219 this.elType = 'intersection'; 220 this.parents = [el1.id, el2.id, i, j]; 221 222 this.generatePolynomial = function () { 223 var poly1 = el1.generatePolynomial(this), 224 poly2 = el2.generatePolynomial(this); 225 226 if ((poly1.length === 0) || (poly2.length === 0)) { 227 return []; 228 } 229 230 return [poly1[0], poly2[0]]; 231 }; 232 233 this.prepareUpdate().update(); 234 }, 235 236 /** 237 * Set the style of a point. 238 * Used for GEONExT import and should not be used to set the point's face and size. 239 * @param {Number} i Integer to determine the style. 240 * @private 241 */ 242 setStyle: function (i) { 243 var facemap = [ 244 // 0-2 245 'cross', 'cross', 'cross', 246 // 3-6 247 'circle', 'circle', 'circle', 'circle', 248 // 7-9 249 'square', 'square', 'square', 250 // 10-12 251 'plus', 'plus', 'plus' 252 ], sizemap = [ 253 // 0-2 254 2, 3, 4, 255 // 3-6 256 1, 2, 3, 4, 257 // 7-9 258 2, 3, 4, 259 // 10-12 260 2, 3, 4 261 ]; 262 263 this.visProp.face = facemap[i]; 264 this.visProp.size = sizemap[i]; 265 266 this.board.renderer.changePointStyle(this); 267 return this; 268 }, 269 270 /** 271 * @deprecated Use JXG#normalizePointFace instead 272 * @param s 273 * @returns {*} 274 */ 275 normalizeFace: function (s) { 276 JXG.deprecated('Point.normalizeFace()', 'JXG.normalizePointFace()'); 277 return Options.normalizePointFace(s); 278 }, 279 280 /** 281 * Set the face of a point element. 282 * @param {String} f String which determines the face of the point. See {@link JXG.GeometryElement#face} for a list of available faces. 283 * @see JXG.GeometryElement#face 284 * @deprecated Use setAttribute() 285 */ 286 face: function (f) { 287 JXG.deprecated('Point.face()', 'Point.setAttribute()'); 288 this.setAttribute({face: f}); 289 }, 290 291 /** 292 * Set the size of a point element 293 * @param {Number} s Integer which determines the size of the point. 294 * @see JXG.GeometryElement#size 295 * @deprecated Use setAttribute() 296 */ 297 size: function (s) { 298 JXG.deprecated('Point.size()', 'Point.setAttribute()'); 299 this.setAttribute({size: s}); 300 }, 301 302 /** 303 * Test if the point is on (is incident with) element "el". 304 * 305 * @param {JXG.GeometryElement} el 306 * @param {Number} tol 307 * @returns {Boolean} 308 * 309 * @example 310 * var circ = board.create('circle', [[-2, -2], 1]); 311 * var seg = board.create('segment', [[-1, -3], [0,0]]); 312 * var line = board.create('line', [[1, 3], [2, -2]]); 313 * var po = board.create('point', [-1, 0], {color: 'blue'}); 314 * var curve = board.create('functiongraph', ['sin(x)'], {strokeColor: 'blue'}); 315 * var pol = board.create('polygon', [[2,2], [4,2], [4,3]], {strokeColor: 'blue'}); 316 * 317 * var point = board.create('point', [-1, 1], { 318 * attractors: [line, seg, circ, po, curve, pol], 319 * attractorDistance: 0.2 320 * }); 321 * 322 * var txt = board.create('text', [-4, 3, function() { 323 * return 'point on line: ' + point.isOn(line) + '<br>' + 324 * 'point on seg: ' + point.isOn(seg) + '<br>' + 325 * 'point on circ = ' + point.isOn(circ) + '<br>' + 326 * 'point on point = ' + point.isOn(po) + '<br>' + 327 * 'point on curve = ' + point.isOn(curve) + '<br>' + 328 * 'point on polygon = ' + point.isOn(pol) + '<br>'; 329 * }]); 330 * 331 * </pre><div id="JXG6c7d7404-758a-44eb-802c-e9644b9fab71" class="jxgbox" style="width: 300px; height: 300px;"></div> 332 * <script type="text/javascript"> 333 * (function() { 334 * var board = JXG.JSXGraph.initBoard('JXG6c7d7404-758a-44eb-802c-e9644b9fab71', 335 * {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false}); 336 * var circ = board.create('circle', [[-2, -2], 1]); 337 * var seg = board.create('segment', [[-1, -3], [0,0]]); 338 * var line = board.create('line', [[1, 3], [2, -2]]); 339 * var po = board.create('point', [-1, 0], {color: 'blue'}); 340 * var curve = board.create('functiongraph', ['sin(x)'], {strokeColor: 'blue'}); 341 * var pol = board.create('polygon', [[2,2], [4,2], [4,3]], {strokeColor: 'blue'}); 342 * 343 * var point = board.create('point', [-1, 1], { 344 * attractors: [line, seg, circ, po, curve, pol], 345 * attractorDistance: 0.2 346 * }); 347 * 348 * var txt = board.create('text', [-4, 3, function() { 349 * return 'point on line: ' + point.isOn(line) + '<br>' + 350 * 'point on seg: ' + point.isOn(seg) + '<br>' + 351 * 'point on circ = ' + point.isOn(circ) + '<br>' + 352 * 'point on point = ' + point.isOn(po) + '<br>' + 353 * 'point on curve = ' + point.isOn(curve) + '<br>' + 354 * 'point on polygon = ' + point.isOn(pol) + '<br>'; 355 * }]); 356 * 357 * })(); 358 * 359 * </script><pre> 360 * 361 */ 362 isOn: function(el, tol) { 363 var arr, crds; 364 365 tol = tol || Mat.eps; 366 367 if (Type.isPoint(el)) { 368 return this.Dist(el) < tol; 369 } else if (el.elementClass === Const.OBJECT_CLASS_LINE) { 370 if (el.elType === 'segment' && !Type.evaluate(this.visProp.alwaysintersect)) { 371 arr = JXG.Math.Geometry.projectCoordsToSegment( 372 this.coords.usrCoords, 373 el.point1.coords.usrCoords, 374 el.point2.coords.usrCoords); 375 if (arr[1] >= 0 && arr[1] <= 1 && 376 Geometry.distPointLine(this.coords.usrCoords, el.stdform) < tol) { 377 return true; 378 } else { 379 return false; 380 } 381 } else { 382 return Geometry.distPointLine(this.coords.usrCoords, el.stdform) < tol; 383 } 384 } else if (el.elementClass === Const.OBJECT_CLASS_CIRCLE) { 385 if (Type.evaluate(el.visProp.hasinnerpoints)) { 386 return this.Dist(el.center) < el.Radius() + tol; 387 } 388 return Math.abs(this.Dist(el.center) - el.Radius()) < tol; 389 } else if (el.elementClass === Const.OBJECT_CLASS_CURVE) { 390 crds = Geometry.projectPointToCurve(this, el, this.board)[0]; 391 return Geometry.distance(this.coords.usrCoords, crds.usrCoords, 3) < tol; 392 } else if (el.type === Const.OBJECT_TYPE_POLYGON) { 393 if (Type.evaluate(el.visProp.hasinnerpoints)) { 394 if (el.pnpoly(this.coords.usrCoords[1], this.coords.usrCoords[2], JXG.COORDS_BY_USER)) { 395 return true; 396 } 397 } 398 arr = Geometry.projectCoordsToPolygon(this.coords.usrCoords, el); 399 return Geometry.distance(this.coords.usrCoords, arr, 3) < tol; 400 } else if (el.type === Const.OBJECT_TYPE_TURTLE) { 401 crds = Geometry.projectPointToTurtle(this, el, this.board); 402 return Geometry.distance(this.coords.usrCoords, crds.usrCoords, 3) < tol; 403 } 404 405 // TODO: Arc, Sector 406 return false; 407 }, 408 409 // Already documented in GeometryElement 410 cloneToBackground: function () { 411 var copy = {}; 412 413 copy.id = this.id + 'T' + this.numTraces; 414 this.numTraces += 1; 415 416 copy.coords = this.coords; 417 copy.visProp = Type.deepCopy(this.visProp, this.visProp.traceattributes, true); 418 copy.visProp.layer = this.board.options.layer.trace; 419 copy.elementClass = Const.OBJECT_CLASS_POINT; 420 copy.board = this.board; 421 Type.clearVisPropOld(copy); 422 423 copy.visPropCalc = { 424 visible: Type.evaluate(copy.visProp.visible) 425 }; 426 427 this.board.renderer.drawPoint(copy); 428 this.traces[copy.id] = copy.rendNode; 429 430 return this; 431 } 432 433 }); 434 435 /** 436 * @class This element is used to provide a constructor for a general point. A free point is created if the given parent elements are all numbers 437 * and the property fixed is not set or set to false. If one or more parent elements is not a number but a string containing a GEONE<sub>x</sub>T 438 * constraint or a function the point will be considered as constrained). That means that the user won't be able to change the point's 439 * position directly. 440 * @pseudo 441 * @description 442 * @name Point 443 * @augments JXG.Point 444 * @constructor 445 * @type JXG.Point 446 * @throws {Exception} If the element cannot be constructed with the given parent objects an exception is thrown. 447 * @param {Number,string,function_Number,string,function_Number,string,function} z_,x,y Parent elements can be two or three elements of type number, a string containing a GEONE<sub>x</sub>T 448 * constraint, or a function which takes no parameter and returns a number. Every parent element determines one coordinate. If a coordinate is 449 * given by a number, the number determines the initial position of a free point. If given by a string or a function that coordinate will be constrained 450 * that means the user won't be able to change the point's position directly by mouse because it will be calculated automatically depending on the string 451 * or the function's return value. If two parent elements are given the coordinates will be interpreted as 2D affine Euclidean coordinates, if three such 452 * parent elements are given they will be interpreted as homogeneous coordinates. 453 * @param {JXG.Point_JXG.Transformation_Array} Point,Transformation A point can also be created providing a transformation or an array of transformations. 454 * The resulting point is a clone of the base point transformed by the given Transformation. {@see JXG.Transformation}. 455 * 456 * @example 457 * // Create a free point using affine Euclidean coordinates 458 * var p1 = board.create('point', [3.5, 2.0]); 459 * </pre><div class="jxgbox" id="JXG672f1764-7dfa-4abc-a2c6-81fbbf83e44b" style="width: 200px; height: 200px;"></div> 460 * <script type="text/javascript"> 461 * var board = JXG.JSXGraph.initBoard('JXG672f1764-7dfa-4abc-a2c6-81fbbf83e44b', {boundingbox: [-1, 5, 5, -1], axis: true, showcopyright: false, shownavigation: false}); 462 * var p1 = board.create('point', [3.5, 2.0]); 463 * </script><pre> 464 * @example 465 * // Create a constrained point using anonymous function 466 * var p2 = board.create('point', [3.5, function () { return p1.X(); }]); 467 * </pre><div class="jxgbox" id="JXG4fd4410c-3383-4e80-b1bb-961f5eeef224" style="width: 200px; height: 200px;"></div> 468 * <script type="text/javascript"> 469 * var fpex1_board = JXG.JSXGraph.initBoard('JXG4fd4410c-3383-4e80-b1bb-961f5eeef224', {boundingbox: [-1, 5, 5, -1], axis: true, showcopyright: false, shownavigation: false}); 470 * var fpex1_p1 = fpex1_board.create('point', [3.5, 2.0]); 471 * var fpex1_p2 = fpex1_board.create('point', [3.5, function () { return fpex1_p1.X(); }]); 472 * </script><pre> 473 * @example 474 * // Create a point using transformations 475 * var trans = board.create('transform', [2, 0.5], {type:'scale'}); 476 * var p3 = board.create('point', [p2, trans]); 477 * </pre><div class="jxgbox" id="JXG630afdf3-0a64-46e0-8a44-f51bd197bb8d" style="width: 400px; height: 400px;"></div> 478 * <script type="text/javascript"> 479 * var fpex2_board = JXG.JSXGraph.initBoard('JXG630afdf3-0a64-46e0-8a44-f51bd197bb8d', {boundingbox: [-1, 9, 9, -1], axis: true, showcopyright: false, shownavigation: false}); 480 * var fpex2_trans = fpex2_board.create('transform', [2, 0.5], {type:'scale'}); 481 * var fpex2_p2 = fpex2_board.create('point', [3.5, 2.0]); 482 * var fpex2_p3 = fpex2_board.create('point', [fpex2_p2, fpex2_trans]); 483 * </script><pre> 484 */ 485 JXG.createPoint = function (board, parents, attributes) { 486 var el, attr; 487 488 attr = Type.copyAttributes(attributes, board.options, 'point'); 489 el = CoordsElement.create(JXG.Point, board, parents, attr); 490 if (!el) { 491 throw new Error("JSXGraph: Can't create point with parent types '" + 492 (typeof parents[0]) + "' and '" + (typeof parents[1]) + "'." + 493 "\nPossible parent types: [x,y], [z,x,y], [element,transformation]"); 494 } 495 496 return el; 497 }; 498 499 /** 500 * @class This element is used to provide a constructor for a glider point. 501 * @pseudo 502 * @description A glider is a point which lives on another geometric element like a line, circle, curve, turtle. 503 * @name Glider 504 * @augments JXG.Point 505 * @constructor 506 * @type JXG.Point 507 * @throws {Exception} If the element cannot be constructed with the given parent objects an exception is thrown. 508 * @param {Number_Number_Number_JXG.GeometryElement} z_,x_,y_,GlideObject Parent elements can be two or three elements of type number and the object the glider lives on. 509 * The coordinates are completely optional. If not given the origin is used. If you provide two numbers for coordinates they will be interpreted as affine Euclidean 510 * coordinates, otherwise they will be interpreted as homogeneous coordinates. In any case the point will be projected on the glide object. 511 * @example 512 * // Create a glider with user defined coordinates. If the coordinates are not on 513 * // the circle (like in this case) the point will be projected onto the circle. 514 * var p1 = board.create('point', [2.0, 2.0]); 515 * var c1 = board.create('circle', [p1, 2.0]); 516 * var p2 = board.create('glider', [2.0, 1.5, c1]); 517 * </pre><div class="jxgbox" id="JXG4f65f32f-e50a-4b50-9b7c-f6ec41652930" style="width: 300px; height: 300px;"></div> 518 * <script type="text/javascript"> 519 * var gpex1_board = JXG.JSXGraph.initBoard('JXG4f65f32f-e50a-4b50-9b7c-f6ec41652930', {boundingbox: [-1, 5, 5, -1], axis: true, showcopyright: false, shownavigation: false}); 520 * var gpex1_p1 = gpex1_board.create('point', [2.0, 2.0]); 521 * var gpex1_c1 = gpex1_board.create('circle', [gpex1_p1, 2.0]); 522 * var gpex1_p2 = gpex1_board.create('glider', [2.0, 1.5, gpex1_c1]); 523 * </script><pre> 524 * @example 525 * // Create a glider with default coordinates (1,0,0). Same premises as above. 526 * var p1 = board.create('point', [2.0, 2.0]); 527 * var c1 = board.create('circle', [p1, 2.0]); 528 * var p2 = board.create('glider', [c1]); 529 * </pre><div class="jxgbox" id="JXG4de7f181-631a-44b1-a12f-bc4d995609e8" style="width: 200px; height: 200px;"></div> 530 * <script type="text/javascript"> 531 * var gpex2_board = JXG.JSXGraph.initBoard('JXG4de7f181-631a-44b1-a12f-bc4d995609e8', {boundingbox: [-1, 5, 5, -1], axis: true, showcopyright: false, shownavigation: false}); 532 * var gpex2_p1 = gpex2_board.create('point', [2.0, 2.0]); 533 * var gpex2_c1 = gpex2_board.create('circle', [gpex2_p1, 2.0]); 534 * var gpex2_p2 = gpex2_board.create('glider', [gpex2_c1]); 535 * </script><pre> 536 *@example 537 * //animate example 2 538 * var p1 = board.create('point', [2.0, 2.0]); 539 * var c1 = board.create('circle', [p1, 2.0]); 540 * var p2 = board.create('glider', [c1]); 541 * var button1 = board.create('button', [1, 7, 'start animation',function(){p2.startAnimation(1,4)}]); 542 * var button2 = board.create('button', [1, 5, 'stop animation',function(){p2.stopAnimation()}]); 543 * </pre><div class="jxgbox" id="JXG4de7f181-631a-44b1-a12f-bc4d133709e8" style="width: 200px; height: 200px;"></div> 544 * <script type="text/javascript"> 545 * var gpex3_board = JXG.JSXGraph.initBoard('JXG4de7f181-631a-44b1-a12f-bc4d133709e8', {boundingbox: [-1, 10, 10, -1], axis: true, showcopyright: false, shownavigation: false}); 546 * var gpex3_p1 = gpex3_board.create('point', [2.0, 2.0]); 547 * var gpex3_c1 = gpex3_board.create('circle', [gpex3_p1, 2.0]); 548 * var gpex3_p2 = gpex3_board.create('glider', [gpex3_c1]); 549 * gpex3_board.create('button', [1, 7, 'start animation',function(){gpex3_p2.startAnimation(1,4)}]); 550 * gpex3_board.create('button', [1, 5, 'stop animation',function(){gpex3_p2.stopAnimation()}]); 551 * </script><pre> 552 */ 553 JXG.createGlider = function (board, parents, attributes) { 554 var el, coords, 555 attr = Type.copyAttributes(attributes, board.options, 'glider'); 556 557 if (parents.length === 1) { 558 coords = [0, 0]; 559 } else { 560 coords = parents.slice(0, 2); 561 } 562 el = board.create('point', coords, attr); 563 564 // eltype is set in here 565 el.makeGlider(parents[parents.length - 1]); 566 567 return el; 568 }; 569 570 571 /** 572 * @class An intersection point is a point which lives on two JSXGraph elements, i.e. it is one point of the the set 573 * consisting of the intersection points of the two elements. The following element types can be (mutually) intersected: line, circle, 574 * curve, polygon, polygonal chain. 575 * 576 * @pseudo 577 * @name Intersection 578 * @augments JXG.Point 579 * @constructor 580 * @type JXG.Point 581 * @throws {Exception} If the element cannot be constructed with the given parent objects an exception is thrown. 582 * @param {JXG.Line,JXG.Circle_JXG.Line,JXG.Circle_Number} el1,el2,i The result will be a intersection point on el1 and el2. i determines the 583 * intersection point if two points are available: <ul> 584 * <li>i==0: use the positive square root,</li> 585 * <li>i==1: use the negative square root.</li></ul> 586 * @example 587 * // Create an intersection point of circle and line 588 * var p1 = board.create('point', [2.0, 2.0]); 589 * var c1 = board.create('circle', [p1, 2.0]); 590 * 591 * var p2 = board.create('point', [2.0, 2.0]); 592 * var p3 = board.create('point', [2.0, 2.0]); 593 * var l1 = board.create('line', [p2, p3]); 594 * 595 * var i = board.create('intersection', [c1, l1, 0]); 596 * </pre><div class="jxgbox" id="JXGe5b0e190-5200-4bc3-b995-b6cc53dc5dc0" style="width: 300px; height: 300px;"></div> 597 * <script type="text/javascript"> 598 * var ipex1_board = JXG.JSXGraph.initBoard('JXGe5b0e190-5200-4bc3-b995-b6cc53dc5dc0', {boundingbox: [-1, 7, 7, -1], axis: true, showcopyright: false, shownavigation: false}); 599 * var ipex1_p1 = ipex1_board.create('point', [4.0, 4.0]); 600 * var ipex1_c1 = ipex1_board.create('circle', [ipex1_p1, 2.0]); 601 * var ipex1_p2 = ipex1_board.create('point', [1.0, 1.0]); 602 * var ipex1_p3 = ipex1_board.create('point', [5.0, 3.0]); 603 * var ipex1_l1 = ipex1_board.create('line', [ipex1_p2, ipex1_p3]); 604 * var ipex1_i = ipex1_board.create('intersection', [ipex1_c1, ipex1_l1, 0]); 605 * </script><pre> 606 */ 607 JXG.createIntersectionPoint = function (board, parents, attributes) { 608 var el, el1, el2, func, i, j, 609 attr = Type.copyAttributes(attributes, board.options, 'intersection'); 610 611 // make sure we definitely have the indices 612 parents.push(0, 0); 613 614 el1 = board.select(parents[0]); 615 el2 = board.select(parents[1]); 616 617 i = parents[2] || 0; 618 j = parents[3] || 0; 619 620 el = board.create('point', [0, 0, 0], attr); 621 622 // el.visProp.alwaysintersect is evaluated as late as in the returned function 623 func = Geometry.intersectionFunction(board, el1, el2, i, j, el.visProp.alwaysintersect); 624 el.addConstraint([func]); 625 626 try { 627 el1.addChild(el); 628 el2.addChild(el); 629 } catch (e) { 630 throw new Error("JSXGraph: Can't create 'intersection' with parent types '" + 631 (typeof parents[0]) + "' and '" + (typeof parents[1]) + "'."); 632 } 633 634 el.type = Const.OBJECT_TYPE_INTERSECTION; 635 el.elType = 'intersection'; 636 el.setParents([el1.id, el2.id]); 637 638 /** 639 * Array of length 2 containing the numbers i and j. 640 * The intersection point is i-th intersection point. 641 * j is unused. 642 * @type Array 643 * @private 644 */ 645 el.intersectionNumbers = [i, j]; 646 el.getParents = function() { 647 return this.parents.concat(this.intersectionNumbers); 648 }; 649 650 el.generatePolynomial = function () { 651 var poly1 = el1.generatePolynomial(el), 652 poly2 = el2.generatePolynomial(el); 653 654 if ((poly1.length === 0) || (poly2.length === 0)) { 655 return []; 656 } 657 658 return [poly1[0], poly2[0]]; 659 }; 660 661 return el; 662 }; 663 664 /** 665 * @class This element is used to provide a constructor for the "other" intersection point. 666 * @pseudo 667 * @description An intersection point is a point which lives on two Lines or Circles or one Line and one Circle at the same time, i.e. 668 * an intersection point of the two elements. Additionally, one intersection point is provided. The function returns the other intersection point. 669 * @name OtherIntersection 670 * @augments JXG.Point 671 * @constructor 672 * @type JXG.Point 673 * @throws {Exception} If the element cannot be constructed with the given parent objects an exception is thrown. 674 * @param {JXG.Line,JXG.Circle_JXG.Line,JXG.Circle_JXG.Point} el1,el2,p The result will be a intersection point on el1 and el2. i determines the 675 * intersection point different from p: 676 * @example 677 * // Create an intersection point of circle and line 678 * var p1 = board.create('point', [2.0, 2.0]); 679 * var c1 = board.create('circle', [p1, 2.0]); 680 * 681 * var p2 = board.create('point', [2.0, 2.0]); 682 * var p3 = board.create('point', [2.0, 2.0]); 683 * var l1 = board.create('line', [p2, p3]); 684 * 685 * var i = board.create('intersection', [c1, l1, 0]); 686 * var j = board.create('otherintersection', [c1, l1, i]); 687 * </pre><div class="jxgbox" id="JXG45e25f12-a1de-4257-a466-27a2ae73614c" style="width: 300px; height: 300px;"></div> 688 * <script type="text/javascript"> 689 * var ipex2_board = JXG.JSXGraph.initBoard('JXG45e25f12-a1de-4257-a466-27a2ae73614c', {boundingbox: [-1, 7, 7, -1], axis: true, showcopyright: false, shownavigation: false}); 690 * var ipex2_p1 = ipex2_board.create('point', [4.0, 4.0]); 691 * var ipex2_c1 = ipex2_board.create('circle', [ipex2_p1, 2.0]); 692 * var ipex2_p2 = ipex2_board.create('point', [1.0, 1.0]); 693 * var ipex2_p3 = ipex2_board.create('point', [5.0, 3.0]); 694 * var ipex2_l1 = ipex2_board.create('line', [ipex2_p2, ipex2_p3]); 695 * var ipex2_i = ipex2_board.create('intersection', [ipex2_c1, ipex2_l1, 0], {name:'D'}); 696 * var ipex2_j = ipex2_board.create('otherintersection', [ipex2_c1, ipex2_l1, ipex2_i], {name:'E'}); 697 * </script><pre> 698 */ 699 JXG.createOtherIntersectionPoint = function (board, parents, attributes) { 700 var el, el1, el2, other; 701 702 if (parents.length !== 3 || 703 !Type.isPoint(parents[2]) || 704 (parents[0].elementClass !== Const.OBJECT_CLASS_LINE && parents[0].elementClass !== Const.OBJECT_CLASS_CIRCLE) || 705 (parents[1].elementClass !== Const.OBJECT_CLASS_LINE && parents[1].elementClass !== Const.OBJECT_CLASS_CIRCLE)) { 706 // Failure 707 throw new Error("JSXGraph: Can't create 'other intersection point' with parent types '" + 708 (typeof parents[0]) + "', '" + (typeof parents[1]) + "'and '" + (typeof parents[2]) + "'." + 709 "\nPossible parent types: [circle|line,circle|line,point]"); 710 } 711 712 el1 = board.select(parents[0]); 713 el2 = board.select(parents[1]); 714 other = board.select(parents[2]); 715 716 el = board.create('point', [function () { 717 var c = Geometry.meet(el1.stdform, el2.stdform, 0, el1.board); 718 719 if (Math.abs(other.X() - c.usrCoords[1]) > Mat.eps || 720 Math.abs(other.Y() - c.usrCoords[2]) > Mat.eps || 721 Math.abs(other.Z() - c.usrCoords[0]) > Mat.eps) { 722 return c; 723 } 724 725 return Geometry.meet(el1.stdform, el2.stdform, 1, el1.board); 726 }], attributes); 727 728 el.type = Const.OBJECT_TYPE_INTERSECTION; 729 el.elType = 'otherintersection'; 730 el.setParents([el1.id, el2.id, other]); 731 732 el1.addChild(el); 733 el2.addChild(el); 734 735 el.generatePolynomial = function () { 736 var poly1 = el1.generatePolynomial(el), 737 poly2 = el2.generatePolynomial(el); 738 739 if ((poly1.length === 0) || (poly2.length === 0)) { 740 return []; 741 } 742 743 return [poly1[0], poly2[0]]; 744 }; 745 746 return el; 747 }; 748 749 /** 750 * @class This element is used to provide a constructor for the pole point of a line with respect to a conic or a circle. 751 * @pseudo 752 * @description The pole point is the unique reciprocal relationship of a line with respect to a conic. 753 * The lines tangent to the intersections of a conic and a line intersect at the pole point of that line with respect to that conic. 754 * A line tangent to a conic has the pole point of that line with respect to that conic as the tangent point. 755 * See {@link http://en.wikipedia.org/wiki/Pole_and_polar} for more information on pole and polar. 756 * @name PolePoint 757 * @augments JXG.Point 758 * @constructor 759 * @type JXG.Point 760 * @throws {Exception} If the element cannot be constructed with the given parent objects an exception is thrown. 761 * @param {JXG.Conic,JXG.Circle_JXG.Point} el1,el2 or 762 * @param {JXG.Point_JXG.Conic,JXG.Circle} el1,el2 The result will be the pole point of the line with respect to the conic or the circle. 763 * @example 764 * // Create the pole point of a line with respect to a conic 765 * var p1 = board.create('point', [-1, 2]); 766 * var p2 = board.create('point', [ 1, 4]); 767 * var p3 = board.create('point', [-1,-2]); 768 * var p4 = board.create('point', [ 0, 0]); 769 * var p5 = board.create('point', [ 4,-2]); 770 * var c1 = board.create('conic',[p1,p2,p3,p4,p5]); 771 * var p6 = board.create('point', [-1, 4]); 772 * var p7 = board.create('point', [2, -2]); 773 * var l1 = board.create('line', [p6, p7]); 774 * var p8 = board.create('polepoint', [c1, l1]); 775 * </pre><div class="jxgbox" id="JXG7b7233a0-f363-47dd-9df5-8018d0d17a98" class="jxgbox" style="width:400px; height:400px;"></div> 776 * <script type='text/javascript'> 777 * var ppex1_board = JXG.JSXGraph.initBoard('JXG7b7233a0-f363-47dd-9df5-8018d0d17a98', {boundingbox: [-3, 5, 5, -3], axis: true, showcopyright: false, shownavigation: false}); 778 * var ppex1_p1 = ppex1_board.create('point', [-1, 2]); 779 * var ppex1_p2 = ppex1_board.create('point', [ 1, 4]); 780 * var ppex1_p3 = ppex1_board.create('point', [-1,-2]); 781 * var ppex1_p4 = ppex1_board.create('point', [ 0, 0]); 782 * var ppex1_p5 = ppex1_board.create('point', [ 4,-2]); 783 * var ppex1_c1 = ppex1_board.create('conic',[ppex1_p1,ppex1_p2,ppex1_p3,ppex1_p4,ppex1_p5]); 784 * var ppex1_p6 = ppex1_board.create('point', [-1, 4]); 785 * var ppex1_p7 = ppex1_board.create('point', [2, -2]); 786 * var ppex1_l1 = ppex1_board.create('line', [ppex1_p6, ppex1_p7]); 787 * var ppex1_p8 = ppex1_board.create('polepoint', [ppex1_c1, ppex1_l1]); 788 * </script><pre> 789 * @example 790 * // Create the pole point of a line with respect to a circle 791 * var p1 = board.create('point', [1, 1]); 792 * var p2 = board.create('point', [2, 3]); 793 * var c1 = board.create('circle',[p1,p2]); 794 * var p3 = board.create('point', [-1, 4]); 795 * var p4 = board.create('point', [4, -1]); 796 * var l1 = board.create('line', [p3, p4]); 797 * var p5 = board.create('polepoint', [c1, l1]); 798 * </pre><div class="jxgbox" id="JXG7b7233a0-f363-47dd-9df5-9018d0d17a98" class="jxgbox" style="width:400px; height:400px;"></div> 799 * <script type='text/javascript'> 800 * var ppex2_board = JXG.JSXGraph.initBoard('JXG7b7233a0-f363-47dd-9df5-9018d0d17a98', {boundingbox: [-3, 7, 7, -3], axis: true, showcopyright: false, shownavigation: false}); 801 * var ppex2_p1 = ppex2_board.create('point', [1, 1]); 802 * var ppex2_p2 = ppex2_board.create('point', [2, 3]); 803 * var ppex2_c1 = ppex2_board.create('circle',[ppex2_p1,ppex2_p2]); 804 * var ppex2_p3 = ppex2_board.create('point', [-1, 4]); 805 * var ppex2_p4 = ppex2_board.create('point', [4, -1]); 806 * var ppex2_l1 = ppex2_board.create('line', [ppex2_p3, ppex2_p4]); 807 * var ppex2_p5 = ppex2_board.create('polepoint', [ppex2_c1, ppex2_l1]); 808 * </script><pre> 809 */ 810 JXG.createPolePoint = function (board, parents, attributes) { 811 var el, el1, el2, 812 firstParentIsConic, secondParentIsConic, 813 firstParentIsLine, secondParentIsLine; 814 815 if (parents.length > 1) { 816 firstParentIsConic = (parents[0].type === Const.OBJECT_TYPE_CONIC || 817 parents[0].elementClass === Const.OBJECT_CLASS_CIRCLE); 818 secondParentIsConic = (parents[1].type === Const.OBJECT_TYPE_CONIC || 819 parents[1].elementClass === Const.OBJECT_CLASS_CIRCLE); 820 821 firstParentIsLine = (parents[0].elementClass === Const.OBJECT_CLASS_LINE); 822 secondParentIsLine = (parents[1].elementClass === Const.OBJECT_CLASS_LINE); 823 } 824 825 /* if (parents.length !== 2 || !(( 826 parents[0].type === Const.OBJECT_TYPE_CONIC || 827 parents[0].elementClass === Const.OBJECT_CLASS_CIRCLE) && 828 parents[1].elementClass === Const.OBJECT_CLASS_LINE || 829 parents[0].elementClass === Const.OBJECT_CLASS_LINE && ( 830 parents[1].type === Const.OBJECT_TYPE_CONIC || 831 parents[1].elementClass === Const.OBJECT_CLASS_CIRCLE))) {*/ 832 if (parents.length !== 2 || 833 !((firstParentIsConic && secondParentIsLine) || 834 (firstParentIsLine && secondParentIsConic))) { 835 // Failure 836 throw new Error("JSXGraph: Can't create 'pole point' with parent types '" + 837 (typeof parents[0]) + "' and '" + (typeof parents[1]) + "'." + 838 "\nPossible parent type: [conic|circle,line], [line,conic|circle]"); 839 } 840 841 if (secondParentIsLine) { 842 el1 = board.select(parents[0]); 843 el2 = board.select(parents[1]); 844 } else { 845 el1 = board.select(parents[1]); 846 el2 = board.select(parents[0]); 847 } 848 849 el = board.create('point', 850 [function () { 851 var q = el1.quadraticform, 852 s = el2.stdform.slice(0, 3); 853 854 return [JXG.Math.Numerics.det([s, q[1], q[2]]), 855 JXG.Math.Numerics.det([q[0], s, q[2]]), 856 JXG.Math.Numerics.det([q[0], q[1], s])]; 857 }], attributes); 858 859 el.elType = 'polepoint'; 860 el.setParents([el1.id, el2.id]); 861 862 el1.addChild(el); 863 el2.addChild(el); 864 865 return el; 866 }; 867 868 JXG.registerElement('point', JXG.createPoint); 869 JXG.registerElement('glider', JXG.createGlider); 870 JXG.registerElement('intersection', JXG.createIntersectionPoint); 871 JXG.registerElement('otherintersection', JXG.createOtherIntersectionPoint); 872 JXG.registerElement('polepoint', JXG.createPolePoint); 873 874 return { 875 Point: JXG.Point, 876 createPoint: JXG.createPoint, 877 createGlider: JXG.createGlider, 878 createIntersection: JXG.createIntersectionPoint, 879 createOtherIntersection: JXG.createOtherIntersectionPoint, 880 createPolePoint: JXG.createPolePoint 881 }; 882 }); 883