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