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