1 /* 2 Copyright 2008-2015 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*/ 34 /*jslint nomen: true, plusplus: true*/ 35 36 /* depends: 37 jxg 38 math/geometry 39 math/math 40 base/coords 41 base/constants 42 utils/type 43 elements: 44 point 45 curve 46 circumcentre 47 transform 48 */ 49 50 define([ 51 'jxg', 'math/geometry', 'math/math', 'math/statistics', 'base/coords', 'base/constants', 'utils/type', 'base/point', 'base/curve', 52 'base/transformation', 'element/composition' 53 ], function (JXG, Geometry, Mat, Statistics, Coords, Const, Type, Point, Curve, Transform, Compositions) { 54 55 "use strict"; 56 57 /** 58 * @class A circular sector is a subarea of the area enclosed by a circle. It is enclosed by two radii and an arc. 59 * @pseudo 60 * @name Sector 61 * @augments JXG.Curve 62 * @constructor 63 * @type JXG.Curve 64 * @throws {Error} If the element cannot be constructed with the given parent objects an exception is thrown. 65 * 66 * First possiblity of input parameters are: 67 * @param {JXG.Point_JXG.Point_JXG.Point} p1,p2,p1 A sector is defined by three points: The sector's center <tt>p1</tt>, 68 * a second point <tt>p2</tt> defining the radius and a third point <tt>p3</tt> defining the angle of the sector. The 69 * Sector is always drawn counter clockwise from <tt>p2</tt> to <tt>p3</tt> 70 * 71 * Second possibility of input parameters are: 72 * @param {JXG.Line_JXG.Line_array,number_array,number_number,function} line, line2, coords1 or direction1, coords2 or direction2, radius The sector is defined by two lines. 73 * The two legs which define the sector are given by two coordinates arrays which are project initially two the two lines or by two directions (+/- 1). 74 * The last parameter is the radius of the sector. 75 * 76 * 77 * @example 78 * // Create a sector out of three free points 79 * var p1 = board.create('point', [1.5, 5.0]), 80 * p2 = board.create('point', [1.0, 0.5]), 81 * p3 = board.create('point', [5.0, 3.0]), 82 * 83 * a = board.create('sector', [p1, p2, p3]); 84 * </pre><div id="49f59123-f013-4681-bfd9-338b89893156" style="width: 300px; height: 300px;"></div> 85 * <script type="text/javascript"> 86 * (function () { 87 * var board = JXG.JSXGraph.initBoard('49f59123-f013-4681-bfd9-338b89893156', {boundingbox: [-1, 7, 7, -1], axis: true, showcopyright: false, shownavigation: false}), 88 * p1 = board.create('point', [1.5, 5.0]), 89 * p2 = board.create('point', [1.0, 0.5]), 90 * p3 = board.create('point', [5.0, 3.0]), 91 * 92 * a = board.create('sector', [p1, p2, p3]); 93 * })(); 94 * </script><pre> 95 * 96 * @example 97 * // Create a sector out of two lines, two directions and a radius 98 * var p1 = board.create('point', [-1, 4]), 99 * p2 = board.create('point', [4, 1]), 100 * q1 = board.create('point', [-2, -3]), 101 * q2 = board.create('point', [4,3]), 102 * 103 * li1 = board.create('line', [p1,p2], {strokeColor:'black', lastArrow:true}), 104 * li2 = board.create('line', [q1,q2], {lastArrow:true}), 105 * 106 * sec1 = board.create('sector', [li1, li2, [5.5, 0], [4, 3], 3]), 107 * sec2 = board.create('sector', [li1, li2, 1, -1, 4]); 108 * 109 * </pre><div id="bb9e2809-9895-4ff1-adfa-c9c71d50aa53" style="width: 300px; height: 300px;"></div> 110 * <script type="text/javascript"> 111 * (function () { 112 * var board = JXG.JSXGraph.initBoard('bb9e2809-9895-4ff1-adfa-c9c71d50aa53', {boundingbox: [-1, 7, 7, -1], axis: true, showcopyright: false, shownavigation: false}), 113 * p1 = board.create('point', [-1, 4]), 114 * p2 = board.create('point', [4, 1]), 115 * q1 = board.create('point', [-2, -3]), 116 * q2 = board.create('point', [4,3]), 117 * 118 * li1 = board.create('line', [p1,p2], {strokeColor:'black', lastArrow:true}), 119 * li2 = board.create('line', [q1,q2], {lastArrow:true}), 120 * 121 * sec1 = board.create('sector', [li1, li2, [5.5, 0], [4, 3], 3]), 122 * sec2 = board.create('sector', [li1, li2, 1, -1, 4]); 123 * })(); 124 * </script><pre> 125 */ 126 JXG.createSector = function (board, parents, attributes) { 127 var el, i, attr, 128 type = 'invalid', 129 s, v, 130 attrPoints = ['center', 'radiuspoint', 'anglepoint'], 131 points; 132 133 // Three points? 134 if (parents[0].elementClass === Const.OBJECT_CLASS_LINE && 135 parents[1].elementClass === Const.OBJECT_CLASS_LINE && 136 (Type.isArray(parents[2]) || Type.isNumber(parents[2])) && 137 (Type.isArray(parents[3]) || Type.isNumber(parents[3])) && 138 (Type.isNumber(parents[4]) || Type.isFunction(parents[4]))) { 139 140 type = '2lines'; 141 142 } else { 143 points = Type.providePoints(board, parents, attributes, 'sector', attrPoints); 144 if (points === false) { 145 throw new Error("JSXGraph: Can't create Sector with parent types '" + 146 (typeof parents[0]) + "' and '" + (typeof parents[1]) + "' and '" + 147 (typeof parents[2]) + "'."); 148 } 149 150 type = '3points'; 151 } 152 153 attr = Type.copyAttributes(attributes, board.options, 'sector'); 154 el = board.create('curve', [[0], [0]], attr); 155 el.type = Const.OBJECT_TYPE_SECTOR; 156 el.elType = 'sector'; 157 158 if (type === '2lines') { 159 el.Radius = function () { 160 return Type.evaluate(parents[4]); 161 }; 162 163 el.line1 = board.select(parents[0]); 164 el.line2 = board.select(parents[1]); 165 166 el.line1.addChild(el); 167 el.line2.addChild(el); 168 el.parents = [parents[0].id, parents[1].id]; 169 170 el.point1 = {visProp: {}}; 171 el.point2 = {visProp: {}}; 172 el.point3 = {visProp: {}}; 173 174 /* Intersection point */ 175 s = Geometry.meetLineLine(el.line1.stdform, el.line2.stdform, 0, board); 176 177 if (Type.isArray(parents[2])) { 178 /* project p1 to l1 */ 179 if (parents[2].length === 2) { 180 parents[2] = [1].concat(parents[2]); 181 } 182 /* 183 v = [0, el.line1.stdform[1], el.line1.stdform[2]]; 184 v = Mat.crossProduct(v, parents[2]); 185 v = Geometry.meetLineLine(v, el.line1.stdform, 0, board); 186 */ 187 v = Geometry.projectPointToLine({coords: {usrCoords: parents[2]}}, el.line1, board); 188 v = Statistics.subtract(v.usrCoords, s.usrCoords); 189 el.direction1 = (Mat.innerProduct(v, [0, el.line1.stdform[2], -el.line1.stdform[1]], 3) >= 0) ? +1 : -1; 190 } else { 191 el.direction1 = (parents[2] >= 0) ? 1 : -1; 192 } 193 194 if (Type.isArray(parents[3])) { 195 /* project p2 to l2 */ 196 if (parents[3].length === 2) { 197 parents[3] = [1].concat(parents[3]); 198 } 199 /* 200 v = [0, el.line2.stdform[1], el.line2.stdform[2]]; 201 v = Mat.crossProduct(v, parents[3]); 202 v = Geometry.meetLineLine(v, el.line2.stdform, 0, board); 203 */ 204 v = Geometry.projectPointToLine({coords: {usrCoords: parents[3]}}, el.line2, board); 205 v = Statistics.subtract(v.usrCoords, s.usrCoords); 206 el.direction2 = (Mat.innerProduct(v, [0, el.line2.stdform[2], -el.line2.stdform[1]], 3) >= 0) ? +1 : -1; 207 } else { 208 el.direction2 = (parents[3] >= 0) ? 1 : -1; 209 } 210 211 el.updateDataArray = function () { 212 var r, l1, l2, 213 A = [0, 0, 0], 214 B = [0, 0, 0], 215 C = [0, 0, 0], 216 ar; 217 218 l1 = this.line1; 219 l2 = this.line2; 220 221 // Intersection point of the lines 222 B = Mat.crossProduct(l1.stdform, l2.stdform); 223 224 if (Math.abs(B[0]) > Mat.eps * Mat.eps) { 225 B[1] /= B[0]; 226 B[2] /= B[0]; 227 B[0] /= B[0]; 228 } 229 // First point 230 r = this.direction1 * this.Radius(); 231 A = Statistics.add(B, [0, r * l1.stdform[2], -r * l1.stdform[1]]); 232 233 // Second point 234 r = this.direction2 * this.Radius(); 235 C = Statistics.add(B, [0, r * l2.stdform[2], -r * l2.stdform[1]]); 236 237 this.point2.coords = new Coords(Const.COORDS_BY_USER, A, el.board); 238 this.point1.coords = new Coords(Const.COORDS_BY_USER, B, el.board); 239 this.point3.coords = new Coords(Const.COORDS_BY_USER, C, el.board); 240 241 if (Math.abs(A[0]) < Mat.eps || Math.abs(B[0]) < Mat.eps || Math.abs(C[0]) < Mat.eps) { 242 this.dataX = [NaN]; 243 this.dataY = [NaN]; 244 return; 245 } 246 247 ar = Geometry.bezierArc(A, B, C, true, 1); 248 249 this.dataX = ar[0]; 250 this.dataY = ar[1]; 251 252 this.bezierDegree = 3; 253 }; 254 255 el.methodMap = JXG.deepCopy(el.methodMap, { 256 radius: 'getRadius', 257 getRadius: 'getRadius', 258 setRadius: 'setRadius' 259 }); 260 261 el.prepareUpdate().update(); 262 263 // end '2lines' 264 265 } else if (type === '3points') { 266 267 /** 268 * Midpoint of the sector. 269 * @memberOf Sector.prototype 270 * @name point1 271 * @type JXG.Point 272 */ 273 el.point1 = points[0]; 274 275 /** 276 * This point together with {@link Sector#point1} defines the radius.. 277 * @memberOf Sector.prototype 278 * @name point2 279 * @type JXG.Point 280 */ 281 el.point2 = points[1]; 282 283 /** 284 * Defines the sector's angle. 285 * @memberOf Sector.prototype 286 * @name point3 287 * @type JXG.Point 288 */ 289 el.point3 = points[2]; 290 291 /* Add arc as child to defining points */ 292 el.point1.addChild(el); 293 el.point2.addChild(el); 294 el.point3.addChild(el); 295 296 // useDirection is necessary for circumCircleSectors 297 el.useDirection = attributes.usedirection; 298 el.parents = [points[0].id, points[1].id, points[2].id]; 299 300 /** 301 * Defines the sectors orientation in case of circumCircleSectors. 302 * @memberOf Sector.prototype 303 * @name point4 304 * @type JXG.Point 305 */ 306 if (Type.exists(points[3])) { 307 el.point4 = points[3]; 308 el.point4.addChild(el); 309 } 310 311 el.methodMap = JXG.deepCopy(el.methodMap, { 312 arc: 'arc', 313 center: 'center', 314 radiuspoint: 'radiuspoint', 315 anglepoint: 'anglepoint', 316 radius: 'getRadius', 317 getRadius: 'getRadius', 318 setRadius: 'setRadius' 319 }); 320 321 /** 322 * documented in JXG.Curve 323 * @ignore 324 */ 325 el.updateDataArray = function () { 326 var ar, det, p0c, p1c, p2c, 327 A = this.point2, 328 B = this.point1, 329 C = this.point3, 330 phi, sgn = 1; 331 332 if (!A.isReal || !B.isReal || !C.isReal) { 333 this.dataX = [NaN]; 334 this.dataY = [NaN]; 335 return; 336 } 337 338 phi = Geometry.rad(A, B, C); 339 if ((this.visProp.selection === 'minor' && phi > Math.PI) || 340 (this.visProp.selection === 'major' && phi < Math.PI)) { 341 sgn = -1; 342 } 343 344 // This is true for circumCircleSectors. In that case there is 345 // a fourth parent element: [midpoint, point1, point3, point2] 346 if (this.useDirection && Type.exists(this.point4)) { 347 p0c = this.point2.coords.usrCoords; 348 p1c = this.point4.coords.usrCoords; 349 p2c = this.point3.coords.usrCoords; 350 det = (p0c[1] - p2c[1]) * (p0c[2] - p1c[2]) - (p0c[2] - p2c[2]) * (p0c[1] - p1c[1]); 351 352 if (det >= 0.0) { 353 C = this.point2; 354 A = this.point3; 355 } 356 } 357 358 A = A.coords.usrCoords; 359 B = B.coords.usrCoords; 360 C = C.coords.usrCoords; 361 362 ar = Geometry.bezierArc(A, B, C, true, sgn); 363 364 this.dataX = ar[0]; 365 this.dataY = ar[1]; 366 this.bezierDegree = 3; 367 }; 368 369 /** 370 * Returns the radius of the sector. 371 * @memberOf Sector.prototype 372 * @name Radius 373 * @function 374 * @returns {Number} The distance between {@link Sector#point1} and {@link Sector#point2}. 375 */ 376 el.Radius = function () { 377 return this.point2.Dist(this.point1); 378 }; 379 380 attr = Type.copyAttributes(attributes, board.options, 'sector', 'arc'); 381 attr.withLabel = false; 382 attr.name += '_arc'; 383 el.arc = board.create('arc', [el.point1, el.point2, el.point3], attr); 384 el.addChild(el.arc); 385 } // end '3points' 386 387 el.center = el.point1; 388 el.radiuspoint = el.point2; 389 el.anglepoint = el.point3; 390 391 // Default hasPoint method. Documented in geometry element 392 el.hasPointCurve = function (x, y) { 393 var angle, alpha, beta, 394 prec = this.board.options.precision.hasPoint / (this.board.unitX), 395 checkPoint = new Coords(Const.COORDS_BY_SCREEN, [x, y], this.board), 396 r = this.Radius(), 397 dist = this.center.coords.distance(Const.COORDS_BY_USER, checkPoint), 398 has = (Math.abs(dist - r) < prec); 399 400 if (has) { 401 angle = Geometry.rad(this.point2, this.center, checkPoint.usrCoords.slice(1)); 402 alpha = 0; 403 beta = Geometry.rad(this.point2, this.center, this.point3); 404 405 if (angle < alpha || angle > beta) { 406 has = false; 407 } 408 } 409 410 return has; 411 }; 412 413 /** 414 * Checks whether (x,y) is within the area defined by the sector. 415 * @memberOf Sector.prototype 416 * @name hasPointSector 417 * @function 418 * @param {Number} x Coordinate in x direction, screen coordinates. 419 * @param {Number} y Coordinate in y direction, screen coordinates. 420 * @returns {Boolean} True if (x,y) is within the sector defined by the arc, False otherwise. 421 */ 422 el.hasPointSector = function (x, y) { 423 var angle, 424 checkPoint = new Coords(Const.COORDS_BY_SCREEN, [x, y], this.board), 425 r = this.Radius(), 426 dist = this.point1.coords.distance(Const.COORDS_BY_USER, checkPoint), 427 alpha, 428 beta, 429 has = (dist < r); 430 431 if (has) { 432 angle = Geometry.rad(this.radiuspoint, this.center, checkPoint.usrCoords.slice(1)); 433 alpha = 0.0; 434 beta = Geometry.rad(this.radiuspoint, this.center, this.anglepoint); 435 436 if ((this.visProp.selection === 'minor' && beta > Math.PI) || 437 (this.visProp.selection === 'major' && beta < Math.PI)) { 438 alpha = beta; 439 beta = 2 * Math.PI; 440 } 441 //if (angle > Geometry.rad(this.point2, this.point1, this.point3)) { 442 if (angle < alpha || angle > beta) { 443 has = false; 444 } 445 } 446 return has; 447 }; 448 449 el.hasPoint = function (x, y) { 450 if (this.visProp.highlightonsector || this.visProp.hasinnerpoints) { 451 return this.hasPointSector(x, y); 452 } 453 454 return this.hasPointCurve(x, y); 455 }; 456 457 // documented in GeometryElement 458 el.getTextAnchor = function () { 459 return this.point1.coords; 460 }; 461 462 // documented in GeometryElement 463 // this method is very similar to arc.getLabelAnchor() 464 // there are some additions in the arc version though, mainly concerning 465 // "major" and "minor" arcs. but maybe these methods can be merged. 466 el.getLabelAnchor = function () { 467 var coords, vecx, vecy, len, 468 angle = Geometry.rad(this.point2, this.point1, this.point3), 469 dx = 13 / this.board.unitX, 470 dy = 13 / this.board.unitY, 471 p2c = this.point2.coords.usrCoords, 472 pmc = this.point1.coords.usrCoords, 473 bxminusax = p2c[1] - pmc[1], 474 byminusay = p2c[2] - pmc[2]; 475 476 // If this is uncommented, the angle label can not be dragged 477 //if (Type.exists(this.label)) { 478 // this.label.relativeCoords = new Coords(Const.COORDS_BY_SCREEN, [0, 0], this.board); 479 //} 480 481 if ((this.visProp.selection === 'minor' && angle > Math.PI) || 482 (this.visProp.selection === 'major' && angle < Math.PI)) { 483 angle = -(2 * Math.PI - angle); 484 } 485 486 coords = new Coords(Const.COORDS_BY_USER, [ 487 pmc[1] + Math.cos(angle * 0.5) * bxminusax - Math.sin(angle * 0.5) * byminusay, 488 pmc[2] + Math.sin(angle * 0.5) * bxminusax + Math.cos(angle * 0.5) * byminusay 489 ], this.board); 490 491 vecx = coords.usrCoords[1] - pmc[1]; 492 vecy = coords.usrCoords[2] - pmc[2]; 493 494 len = Math.sqrt(vecx * vecx + vecy * vecy); 495 vecx = vecx * (len + dx) / len; 496 vecy = vecy * (len + dy) / len; 497 498 return new Coords(Const.COORDS_BY_USER, [pmc[1] + vecx, pmc[2] + vecy], this.board); 499 }; 500 501 /** 502 * Overwrite the Radius method of the sector. 503 * Used in {@link GeometryElement#setAttribute}. 504 * @param {Number, Function} value New radius. 505 */ 506 el.setRadius = function (value) { 507 el.Radius = function () { 508 return Type.evaluate(value); 509 }; 510 }; 511 512 /** 513 * deprecated 514 * @ignore 515 */ 516 el.getRadius = function () { 517 return this.Radius(); 518 }; 519 520 /** 521 * Moves the sector by the difference of two coordinates. 522 * @param {Number} method The type of coordinates used here. Possible values are {@link JXG.COORDS_BY_USER} and {@link JXG.COORDS_BY_SCREEN}. 523 * @param {Array} coords coordinates in screen/user units 524 * @param {Array} oldcoords previous coordinates in screen/user units 525 * @returns {JXG.Curve} this element 526 */ 527 if (type === '3points') { 528 el.setPositionDirectly = function (method, coords, oldcoords) { 529 var dc, t, i, len, 530 c = new Coords(method, coords, this.board), 531 oldc = new Coords(method, oldcoords, this.board); 532 533 if (!el.point1.draggable() || !el.point2.draggable() || !el.point3.draggable()) { 534 return this; 535 } 536 537 dc = Statistics.subtract(c.usrCoords, oldc.usrCoords); 538 t = this.board.create('transform', dc.slice(1), {type: 'translate'}); 539 t.applyOnce([el.point1, el.point2, el.point3]); 540 541 return this; 542 }; 543 } 544 545 el.prepareUpdate().update(); 546 547 return el; 548 }; 549 550 JXG.registerElement('sector', JXG.createSector); 551 552 553 /** 554 * @class A circumcircle sector is different from a {@link Sector} mostly in the way the parent elements are interpreted. 555 * At first, the circum centre is determined from the three given points. Then the sector is drawn from <tt>p1</tt> through 556 * <tt>p2</tt> to <tt>p3</tt>. 557 * @pseudo 558 * @name CircumcircleSector 559 * @augments Sector 560 * @constructor 561 * @type Sector 562 * @throws {Error} If the element cannot be constructed with the given parent objects an exception is thrown. 563 * @param {JXG.Point_JXG.Point_JXG.Point} p1,p2,p1 A circumcircle sector is defined by the circumcircle which is determined 564 * by these three given points. The circumcircle sector is always drawn from <tt>p1</tt> through <tt>p2</tt> to <tt>p3</tt>. 565 * @example 566 * // Create an arc out of three free points 567 * var p1 = board.create('point', [1.5, 5.0]), 568 * p2 = board.create('point', [1.0, 0.5]), 569 * p3 = board.create('point', [5.0, 3.0]), 570 * 571 * a = board.create('circumcirclesector', [p1, p2, p3]); 572 * </pre><div id="695cf0d6-6d7a-4d4d-bfc9-34c6aa28cd04" style="width: 300px; height: 300px;"></div> 573 * <script type="text/javascript"> 574 * (function () { 575 * var board = JXG.JSXGraph.initBoard('695cf0d6-6d7a-4d4d-bfc9-34c6aa28cd04', {boundingbox: [-1, 7, 7, -1], axis: true, showcopyright: false, shownavigation: false}), 576 * p1 = board.create('point', [1.5, 5.0]), 577 * p2 = board.create('point', [1.0, 0.5]), 578 * p3 = board.create('point', [5.0, 3.0]), 579 * 580 * a = board.create('circumcirclesector', [p1, p2, p3]); 581 * })(); 582 * </script><pre> 583 */ 584 JXG.createCircumcircleSector = function (board, parents, attributes) { 585 var el, mp, attr, points, i; 586 587 points = Type.providePoints(board, parents, attributes, 'point'); 588 if (points === false) { 589 throw new Error("JSXGraph: Can't create circumcircle sector with parent types '" + 590 (typeof parents[0]) + "' and '" + (typeof parents[1]) + "' and '" + (typeof parents[2]) + "'."); 591 } 592 593 mp = board.create('circumcenter', points.slice(0, 3), attr); 594 mp.dump = false; 595 596 attr = Type.copyAttributes(attributes, board.options, 'circumcirclesector'); 597 el = board.create('sector', [mp, points[0], points[2], points[1]], attr); 598 599 el.elType = 'circumcirclesector'; 600 el.parents = [points[0].id, points[1].id, points[2].id]; 601 602 /** 603 * Center of the circumcirclesector 604 * @memberOf CircumcircleSector.prototype 605 * @name center 606 * @type Circumcenter 607 */ 608 el.center = mp; 609 el.subs = { 610 center: mp 611 }; 612 613 return el; 614 }; 615 616 JXG.registerElement('circumcirclesector', JXG.createCircumcircleSector); 617 618 /** 619 * @class A minor sector is a sector of a circle having measure less than or equal to 620 * 180 degrees (pi radians). It is defined by a center, one point that 621 * defines the radius, and a third point that defines the angle of the sector. 622 * @pseudo 623 * @name MinorSector 624 * @augments Curve 625 * @constructor 626 * @type JXG.Curve 627 * @throws {Error} If the element cannot be constructed with the given parent objects an exception is thrown. 628 * @param {JXG.Point_JXG.Point_JXG.Point} p1,p2,p3 . Minor sector is a sector of a circle around p1 having measure less than or equal to 629 * 180 degrees (pi radians) and starts at p2. The radius is determined by p2, the angle by p3. 630 * @example 631 * // Create sector out of three free points 632 * var p1 = board.create('point', [2.0, 2.0]); 633 * var p2 = board.create('point', [1.0, 0.5]); 634 * var p3 = board.create('point', [3.5, 1.0]); 635 * 636 * var a = board.create('minorsector', [p1, p2, p3]); 637 * </pre><div id="af27ddcc-265f-428f-90dd-d31ace945800" style="width: 300px; height: 300px;"></div> 638 * <script type="text/javascript"> 639 * (function () { 640 * var board = JXG.JSXGraph.initBoard('af27ddcc-265f-428f-90dd-d31ace945800', {boundingbox: [-1, 7, 7, -1], axis: true, showcopyright: false, shownavigation: false}), 641 * p1 = board.create('point', [2.0, 2.0]), 642 * p2 = board.create('point', [1.0, 0.5]), 643 * p3 = board.create('point', [3.5, 1.0]), 644 * 645 * a = board.create('minorsector', [p1, p2, p3]); 646 * })(); 647 * </script><pre> 648 */ 649 JXG.createMinorSector = function (board, parents, attributes) { 650 attributes.selection = 'minor'; 651 return JXG.createSector(board, parents, attributes); 652 }; 653 654 JXG.registerElement('minorsector', JXG.createMinorSector); 655 656 /** 657 * @class A major sector is a sector of a circle having measure greater than or equal to 658 * 180 degrees (pi radians). It is defined by a center, one point that 659 * defines the radius, and a third point that defines the angle of the sector. 660 * @pseudo 661 * @name MajorSector 662 * @augments Curve 663 * @constructor 664 * @type JXG.Curve 665 * @throws {Error} If the element cannot be constructed with the given parent objects an exception is thrown. 666 * @param {JXG.Point_JXG.Point_JXG.Point} p1,p2,p3 . Major sector is a sector of a circle around p1 having measure greater than or equal to 667 * 180 degrees (pi radians) and starts at p2. The radius is determined by p2, the angle by p3. 668 * @example 669 * // Create an arc out of three free points 670 * var p1 = board.create('point', [2.0, 2.0]); 671 * var p2 = board.create('point', [1.0, 0.5]); 672 * var p3 = board.create('point', [3.5, 1.0]); 673 * 674 * var a = board.create('majorsector', [p1, p2, p3]); 675 * </pre><div id="83c6561f-7561-4047-b98d-036248a00932" style="width: 300px; height: 300px;"></div> 676 * <script type="text/javascript"> 677 * (function () { 678 * var board = JXG.JSXGraph.initBoard('83c6561f-7561-4047-b98d-036248a00932', {boundingbox: [-1, 7, 7, -1], axis: true, showcopyright: false, shownavigation: false}), 679 * p1 = board.create('point', [2.0, 2.0]), 680 * p2 = board.create('point', [1.0, 0.5]), 681 * p3 = board.create('point', [3.5, 1.0]), 682 * 683 * a = board.create('majorsector', [p1, p2, p3]); 684 * })(); 685 * </script><pre> 686 */ 687 JXG.createMajorSector = function (board, parents, attributes) { 688 attributes.selection = 'major'; 689 return JXG.createSector(board, parents, attributes); 690 }; 691 692 JXG.registerElement('majorsector', JXG.createMajorSector); 693 694 /** 695 * @class The angle element is used to denote an angle defined by three points. Visually it is just a {@link Sector} 696 * element with a radius not defined by the parent elements but by an attribute <tt>radius</tt>. As opposed to the sector, 697 * an angle has two angle points and no radius point. 698 * Sector is displayed if type=="sector". 699 * If type=="square", instead of a sector a parallelogram is displayed. 700 * In case of type=="auto", a square is displayed if the angle is near orthogonal. 701 * If no name is provided the angle label is automatically set to a lower greek letter. 702 * @pseudo 703 * @name Angle 704 * @augments Sector 705 * @constructor 706 * @type Sector 707 * @throws {Error} If the element cannot be constructed with the given parent objects an exception is thrown. 708 * First possiblity of input parameters are: 709 * @param {JXG.Point_JXG.Point_JXG.Point} p1,p2,p1 An angle is always drawn counterclockwise from <tt>p1</tt> to 710 * <tt>p3</tt> around <tt>p2</tt>. 711 * 712 * Second possibility of input parameters are: 713 * @param {JXG.Line_JXG.Line_array|number_array|number} line, line2, coords1 or direction1, coords2 or direction2, radius The angle is defined by two lines. 714 * The two legs which define the angle are given by two coordinate arrays. 715 * The points given by these coordinate arrays are projected initially (i.e. only once) onto the two lines. 716 * The other possibility is to supply directions (+/- 1). 717 * 718 * @example 719 * // Create an angle out of three free points 720 * var p1 = board.create('point', [5.0, 3.0]), 721 * p2 = board.create('point', [1.0, 0.5]), 722 * p3 = board.create('point', [1.5, 5.0]), 723 * 724 * a = board.create('angle', [p1, p2, p3]); 725 * </pre><div id="a34151f9-bb26-480a-8d6e-9b8cbf789ae5" style="width: 300px; height: 300px;"></div> 726 * <script type="text/javascript"> 727 * (function () { 728 * var board = JXG.JSXGraph.initBoard('a34151f9-bb26-480a-8d6e-9b8cbf789ae5', {boundingbox: [-1, 7, 7, -1], axis: true, showcopyright: false, shownavigation: false}), 729 * p1 = board.create('point', [5.0, 3.0]), 730 * p2 = board.create('point', [1.0, 0.5]), 731 * p3 = board.create('point', [1.5, 5.0]), 732 * 733 * a = board.create('angle', [p1, p2, p3]); 734 * })(); 735 * </script><pre> 736 * 737 * @example 738 * // Create an angle out of two lines and two directions 739 * var p1 = board.create('point', [-1, 4]), 740 * p2 = board.create('point', [4, 1]), 741 * q1 = board.create('point', [-2, -3]), 742 * q2 = board.create('point', [4,3]), 743 * 744 * li1 = board.create('line', [p1,p2], {strokeColor:'black', lastArrow:true}), 745 * li2 = board.create('line', [q1,q2], {lastArrow:true}), 746 * 747 * a1 = board.create('angle', [li1, li2, [5.5, 0], [4, 3]], { radius:1 }), 748 * a2 = board.create('angle', [li1, li2, 1, -1], { radius:2 }); 749 * 750 * </pre><div id="3a667ddd-63dc-4594-b5f1-afac969b371f" style="width: 300px; height: 300px;"></div> 751 * <script type="text/javascript"> 752 * (function () { 753 * var board = JXG.JSXGraph.initBoard('3a667ddd-63dc-4594-b5f1-afac969b371f', {boundingbox: [-1, 7, 7, -1], axis: true, showcopyright: false, shownavigation: false}), 754 * p1 = board.create('point', [-1, 4]), 755 * p2 = board.create('point', [4, 1]), 756 * q1 = board.create('point', [-2, -3]), 757 * q2 = board.create('point', [4,3]), 758 * 759 * li1 = board.create('line', [p1,p2], {strokeColor:'black', lastArrow:true}), 760 * li2 = board.create('line', [q1,q2], {lastArrow:true}), 761 * 762 * a1 = board.create('angle', [li1, li2, [5.5, 0], [4, 3]], { radius:1 }), 763 * a2 = board.create('angle', [li1, li2, 1, -1], { radius:2 }); 764 * })(); 765 * </script><pre> 766 */ 767 JXG.createAngle = function (board, parents, attributes) { 768 var el, radius, text, attr, attrsub, 769 i, dot, points, 770 type = 'invalid'; 771 772 // Two lines or three points? 773 if (parents[0].elementClass === Const.OBJECT_CLASS_LINE && 774 parents[1].elementClass === Const.OBJECT_CLASS_LINE && 775 (Type.isArray(parents[2]) || Type.isNumber(parents[2])) && 776 (Type.isArray(parents[3]) || Type.isNumber(parents[3]))) { 777 778 type = '2lines'; 779 } else { 780 points = Type.providePoints(board, parents, attributes, 'point'); 781 if (points === false) { 782 throw new Error("JSXGraph: Can't create angle with parent types '" + 783 (typeof parents[0]) + "' and '" + (typeof parents[1]) + "' and '" + (typeof parents[2]) + "'."); 784 } 785 type = '3points'; 786 } 787 788 attr = Type.copyAttributes(attributes, board.options, 'angle'); 789 790 // If empty, create a new name 791 if (!Type.exists(attr.name) || attr.name === '') { 792 attr.name = board.generateName({type: Const.OBJECT_TYPE_ANGLE}); 793 } 794 795 if (Type.exists(attr.radius)) { 796 radius = attr.radius; 797 } else { 798 radius = 0; 799 } 800 801 if (type === '2lines') { 802 parents.push(radius); 803 el = board.create('sector', parents, attr); 804 el.updateDataArraySector = el.updateDataArray; 805 806 // TODO 807 el.setAngle = function (val) {}; 808 el.free = function (val) {}; 809 810 } else { 811 el = board.create('sector', [points[1], points[0], points[2]], attr); 812 el.arc.visProp.priv = true; 813 814 /** 815 * The point defining the radius of the angle element. Alias for {@link Angle.prototype#radiuspoint}. 816 * @type JXG.Point 817 * @name point 818 * @memberOf Angle.prototype 819 */ 820 el.point = el.point2 = el.radiuspoint = points[0]; 821 822 /** 823 * Helper point for angles of type 'square'. 824 * @type JXG.Point 825 * @name pointsquare 826 * @memberOf Angle.prototype 827 */ 828 el.pointsquare = el.point3 = el.anglepoint = points[2]; 829 830 el.Radius = function () { 831 return Type.evaluate(radius); 832 }; 833 834 el.updateDataArraySector = function () { 835 var A = this.point2, 836 B = this.point1, 837 C = this.point3, 838 r = this.Radius(), 839 d = B.Dist(A), 840 ar, 841 phi, 842 sgn = 1; 843 844 phi = Geometry.rad(A, B, C); 845 if ((this.visProp.selection === 'minor' && phi > Math.PI) || 846 (this.visProp.selection === 'major' && phi < Math.PI)) { 847 sgn = -1; 848 } 849 850 A = A.coords.usrCoords; 851 B = B.coords.usrCoords; 852 C = C.coords.usrCoords; 853 854 A = [1, B[1] + (A[1] - B[1]) * r / d, B[2] + (A[2] - B[2]) * r / d]; 855 C = [1, B[1] + (C[1] - B[1]) * r / d, B[2] + (C[2] - B[2]) * r / d]; 856 857 ar = Geometry.bezierArc(A, B, C, true, sgn); 858 859 this.dataX = ar[0]; 860 this.dataY = ar[1]; 861 this.bezierDegree = 3; 862 }; 863 864 /** 865 * Set an angle to a prescribed value given in radians. This is only possible if the third point of the angle, i.e. 866 * the anglepoint is a free point. 867 * @name setAngle 868 * @function 869 * @param {Number|Function} val Number or Function which returns the size of the angle in Radians 870 * @returns {Object} Pointer to the angle element.. 871 * @memberOf Angle.prototype 872 */ 873 el.setAngle = function (val) { 874 var t, 875 p = this.anglepoint, 876 q = this.radiuspoint; 877 878 if (p.draggable()) { 879 t = this.board.create('transform', [val, this.center], {type: 'rotate'}); 880 p.addTransform(q, t); 881 p.isDraggable = false; 882 p.parents = [q]; 883 } 884 return this; 885 }; 886 887 /** 888 * Frees an angle from a prescribed value. This is only relevant if the angle size has been set by 889 * setAngle() previously. The anglepoint is set to a free point. 890 * @name free 891 * @function 892 * @returns {Object} Pointer to the angle element.. 893 * @memberOf Angle.prototype 894 */ 895 el.free = function () { 896 var p = this.anglepoint; 897 if (p.transformations.length > 0) { 898 p.transformations.pop(); 899 p.isDraggable = true; 900 p.parents = []; 901 } 902 return this; 903 }; 904 905 el.parents = [points[0].id, points[1].id, points[2].id]; // Important: This overwrites the parents order in underlying sector 906 907 } // end '3points' 908 909 // GEONExT compatible labels. 910 if (JXG.exists(el.visProp.text)) { 911 el.label.setText(el.visProp.text); 912 } 913 914 el.elType = 'angle'; 915 el.type = Const.OBJECT_TYPE_ANGLE; 916 // el.parents = [points[0].id, points[1].id, points[2].id]; 917 el.subs = {}; 918 919 el.updateDataArraySquare = function () { 920 var A, B, C, 921 r = this.Radius(), 922 d1, d2, 923 v, l1, l2; 924 925 926 if (type === '2lines') { 927 // This is necessary to update this.point1, this.point2, this.point3. 928 this.updateDataArraySector(); 929 } 930 931 A = this.point2; 932 B = this.point1; 933 C = this.point3; 934 935 A = A.coords.usrCoords; 936 B = B.coords.usrCoords; 937 C = C.coords.usrCoords; 938 939 d1 = Geometry.distance(A, B, 3); 940 d2 = Geometry.distance(C, B, 3); 941 942 // In case of type=='2lines' this is redundant, because r == d1 == d2 943 A = [1, B[1] + (A[1] - B[1]) * r / d1, B[2] + (A[2] - B[2]) * r / d1]; 944 C = [1, B[1] + (C[1] - B[1]) * r / d2, B[2] + (C[2] - B[2]) * r / d2]; 945 946 v = Mat.crossProduct(C, B); 947 l1 = [-A[1] * v[1] - A[2] * v[2], A[0] * v[1], A[0] * v[2]]; 948 v = Mat.crossProduct(A, B); 949 l2 = [-C[1] * v[1] - C[2] * v[2], C[0] * v[1], C[0] * v[2]]; 950 951 v = Mat.crossProduct(l1, l2); 952 v[1] /= v[0]; 953 v[2] /= v[0]; 954 955 this.dataX = [B[1], A[1], v[1], C[1], B[1]]; 956 this.dataY = [B[2], A[2], v[2], C[2], B[2]]; 957 958 this.bezierDegree = 1; 959 }; 960 961 el.updateDataArrayNone = function () { 962 this.dataX = [NaN]; 963 this.dataY = [NaN]; 964 this.bezierDegree = 1; 965 }; 966 967 el.updateDataArray = function () { 968 var type = this.visProp.type, 969 deg = Geometry.trueAngle(this.point2, this.point1, this.point3); 970 971 if ((this.visProp.selection === 'minor' && deg > 180.0) || 972 (this.visProp.selection === 'major' && deg < 180.0)) { 973 deg = 360.0 - deg; 974 } 975 976 if (Math.abs(deg - 90.0) < this.visProp.orthosensitivity + Mat.eps) { 977 type = this.visProp.orthotype; 978 } 979 980 if (type === 'none') { 981 this.updateDataArrayNone(); 982 } else if (type === 'square') { 983 this.updateDataArraySquare(); 984 } else if (type === 'sector') { 985 this.updateDataArraySector(); 986 } else if (type === 'sectordot') { 987 this.updateDataArraySector(); 988 if (!this.dot.visProp.visible) { 989 this.dot.setAttribute({visible: true}); 990 } 991 } 992 993 if (!this.visProp.visible || (type !== 'sectordot' && this.dot.visProp.visible)) { 994 this.dot.setAttribute({visible: false}); 995 } 996 }; 997 998 /** 999 * Indicates a right angle. Invisible by default, use <tt>dot.visible: true</tt> to show. 1000 * Though this dot indicates a right angle, it can be visible even if the angle is not a right 1001 * one. 1002 * @type JXG.Point 1003 * @name dot 1004 * @memberOf Angle.prototype 1005 */ 1006 attrsub = Type.copyAttributes(attributes, board.options, 'angle', 'dot'); 1007 el.dot = board.create('point', [function () { 1008 var A, B, r, d, a2, co, si, mat, 1009 point1, point2, point3; 1010 1011 if (Type.exists(el.dot) && !el.dot.visProp.visible) { 1012 return [0, 0]; 1013 } 1014 1015 A = el.point2.coords.usrCoords; 1016 B = el.point1.coords.usrCoords; 1017 r = el.Radius(); 1018 d = Geometry.distance(A, B, 3); 1019 a2 = Geometry.rad(el.point2, el.point1, el.point3); 1020 1021 if ((el.visProp.selection === 'minor' && a2 > Math.PI) || 1022 (el.visProp.selection === 'major' && a2 < Math.PI)) { 1023 a2 = -(2 * Math.PI - a2); 1024 } 1025 a2 *= 0.5; 1026 1027 co = Math.cos(a2); 1028 si = Math.sin(a2); 1029 1030 A = [1, B[1] + (A[1] - B[1]) * r / d, B[2] + (A[2] - B[2]) * r / d]; 1031 1032 mat = [ 1033 [1, 0, 0], 1034 [B[1] - 0.5 * B[1] * co + 0.5 * B[2] * si, co * 0.5, -si * 0.5], 1035 [B[2] - 0.5 * B[1] * si - 0.5 * B[2] * co, si * 0.5, co * 0.5] 1036 ]; 1037 return Mat.matVecMult(mat, A); 1038 }], attrsub); 1039 1040 el.dot.dump = false; 1041 el.subs.dot = el.dot; 1042 1043 if (type === '2lines') { 1044 for (i = 0; i < 2; i++) { 1045 board.select(parents[i]).addChild(el.dot); 1046 } 1047 } else { 1048 for (i = 0; i < 3; i++) { 1049 board.select(points[i]).addChild(el.dot); 1050 } 1051 } 1052 1053 // documented in GeometryElement 1054 el.getLabelAnchor = function () { 1055 var vec, dx = 12, dy = 12, 1056 A, B, r, d, a2, co, si, mat; 1057 1058 // If this is uncommented, the angle label can not be dragged 1059 //if (Type.exists(this.label)) { 1060 // this.label.relativeCoords = new Coords(Const.COORDS_BY_SCREEN, [0, 0], this.board); 1061 //} 1062 1063 if (Type.exists(this.label.visProp.fontSize)) { 1064 dx = this.label.visProp.fontSize; 1065 dy = this.label.visProp.fontSize; 1066 } 1067 dx /= this.board.unitX; 1068 dy /= this.board.unitY; 1069 1070 A = el.point2.coords.usrCoords; 1071 B = el.point1.coords.usrCoords; 1072 r = el.Radius(); 1073 d = Geometry.distance(A, B, 3); 1074 a2 = Geometry.rad(el.point2, el.point1, el.point3); 1075 if ((el.visProp.selection === 'minor' && a2 > Math.PI) || 1076 (el.visProp.selection === 'major' && a2 < Math.PI)) { 1077 a2 = -(2 * Math.PI - a2); 1078 } 1079 a2 *= 0.5; 1080 co = Math.cos(a2); 1081 si = Math.sin(a2); 1082 1083 A = [1, B[1] + (A[1] - B[1]) * r / d, B[2] + (A[2] - B[2]) * r / d]; 1084 1085 mat = [ 1086 [1, 0, 0], 1087 [B[1] - 0.5 * B[1] * co + 0.5 * B[2] * si, co * 0.5, -si * 0.5], 1088 [B[2] - 0.5 * B[1] * si - 0.5 * B[2] * co, si * 0.5, co * 0.5] 1089 ]; 1090 vec = Mat.matVecMult(mat, A); 1091 vec[1] /= vec[0]; 1092 vec[2] /= vec[0]; 1093 vec[0] /= vec[0]; 1094 1095 d = Geometry.distance(vec, B, 3); 1096 vec = [vec[0], B[1] + (vec[1] - B[1]) * (r + dx) / d, B[2] + (vec[2] - B[2]) * (r + dx) / d]; 1097 1098 return new Coords(Const.COORDS_BY_USER, vec, this.board); 1099 }; 1100 1101 el.Value = function () { 1102 return Geometry.rad(this.point2, this.point1, this.point3); 1103 }; 1104 1105 el.methodMap = Type.deepCopy(el.methodMap, { 1106 Value: 'Value', 1107 setAngle: 'setAngle', 1108 free: 'free' 1109 }); 1110 1111 return el; 1112 }; 1113 1114 JXG.registerElement('angle', JXG.createAngle); 1115 1116 /** 1117 * @class A non-reflex angle is the acute or obtuse instance of an angle. 1118 * It is defined by a center, one point that 1119 * defines the radius, and a third point that defines the angle of the sector. 1120 * @pseudo 1121 * @name NonReflexAngle 1122 * @augments Sector 1123 * @constructor 1124 * @type Sector 1125 * @throws {Error} If the element cannot be constructed with the given parent objects an exception is thrown. 1126 * @param {JXG.Point_JXG.Point_JXG.Point} p1,p2,p3 . Minor sector is a sector of a circle around p1 having measure less than or equal to 1127 * 180 degrees (pi radians) and starts at p2. The radius is determined by p2, the angle by p3. 1128 * @example 1129 * // Create a non-reflex angle out of three free points 1130 * var p1 = board.create('point', [5.0, 3.0]), 1131 * p2 = board.create('point', [1.0, 0.5]), 1132 * p3 = board.create('point', [1.5, 5.0]), 1133 * 1134 * a = board.create('nonreflexangle', [p1, p2, p3], {radius: 2}); 1135 * </pre><div id="d0ab6d6b-63a7-48b2-8749-b02bb5e744f9" style="width: 300px; height: 300px;"></div> 1136 * <script type="text/javascript"> 1137 * (function () { 1138 * var board = JXG.JSXGraph.initBoard('d0ab6d6b-63a7-48b2-8749-b02bb5e744f9', {boundingbox: [-1, 7, 7, -1], axis: true, showcopyright: false, shownavigation: false}), 1139 * p1 = board.create('point', [5.0, 3.0]), 1140 * p2 = board.create('point', [1.0, 0.5]), 1141 * p3 = board.create('point', [1.5, 5.0]), 1142 * 1143 * a = board.create('nonreflexangle', [p1, p2, p3], {radius: 2}); 1144 * })(); 1145 * </script><pre> 1146 */ 1147 JXG.createNonreflexAngle = function (board, parents, attributes) { 1148 attributes.selection = 'minor'; 1149 return JXG.createAngle(board, parents, attributes); 1150 }; 1151 1152 JXG.registerElement('nonreflexangle', JXG.createNonreflexAngle); 1153 1154 /** 1155 * @class A reflex angle is the neither acute nor obtuse instance of an angle. 1156 * It is defined by a center, one point that 1157 * defines the radius, and a third point that defines the angle of the sector. 1158 * @pseudo 1159 * @name ReflexAngle 1160 * @augments Sector 1161 * @constructor 1162 * @type Sector 1163 * @throws {Error} If the element cannot be constructed with the given parent objects an exception is thrown. 1164 * @param {JXG.Point_JXG.Point_JXG.Point} p1,p2,p3 . Minor sector is a sector of a circle around p1 having measure less than or equal to 1165 * 180 degrees (pi radians) and starts at p2. The radius is determined by p2, the angle by p3. 1166 * @example 1167 * // Create a non-reflex angle out of three free points 1168 * var p1 = board.create('point', [5.0, 3.0]), 1169 * p2 = board.create('point', [1.0, 0.5]), 1170 * p3 = board.create('point', [1.5, 5.0]), 1171 * 1172 * a = board.create('reflexangle', [p1, p2, p3], {radius: 2}); 1173 * </pre><div id="f2a577f2-553d-4f9f-a895-2d6d4b8c60e8" style="width: 300px; height: 300px;"></div> 1174 * <script type="text/javascript"> 1175 * (function () { 1176 * var board = JXG.JSXGraph.initBoard('f2a577f2-553d-4f9f-a895-2d6d4b8c60e8', {boundingbox: [-1, 7, 7, -1], axis: true, showcopyright: false, shownavigation: false}), 1177 * p1 = board.create('point', [5.0, 3.0]), 1178 * p2 = board.create('point', [1.0, 0.5]), 1179 * p3 = board.create('point', [1.5, 5.0]), 1180 * 1181 * a = board.create('reflexangle', [p1, p2, p3], {radius: 2}); 1182 * })(); 1183 * </script><pre> 1184 */ 1185 JXG.createReflexAngle = function (board, parents, attributes) { 1186 attributes.selection = 'major'; 1187 return JXG.createAngle(board, parents, attributes); 1188 }; 1189 1190 JXG.registerElement('reflexangle', JXG.createReflexAngle); 1191 1192 1193 return { 1194 createSector: JXG.createSector, 1195 createCircumcircleSector: JXG.createCircumcircleSector, 1196 createMinorSector: JXG.createMinorSector, 1197 createMajorSector: JXG.createMajorSector, 1198 createAngle: JXG.createAngle, 1199 createReflexAngle: JXG.createReflexAngle, 1200 createNonreflexAngle: JXG.createNonreflexAngle 1201 }; 1202 }); 1203