1 /*
  2     Copyright 2008-2021
  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  base/element
 39  base/constants
 40  base/coords
 41  parser/geonext
 42  math/geometry
 43  math/statistics
 44  utils/type
 45   elements:
 46    transform
 47    point
 48  */
 49 
 50 /**
 51  * @fileoverview The geometry object Circle is defined in this file. Circle stores all
 52  * style and functional properties that are required to draw and move a circle on
 53  * a board.
 54  */
 55 
 56 define([
 57     'jxg', 'base/element', 'base/coords', 'base/constants', 'element/conic', 'parser/geonext', 'utils/type'
 58 ], function (JXG, GeometryElement, Coords, Const, Conic, GeonextParser, Type) {
 59 
 60     "use strict";
 61 
 62     /**
 63      * A circle consists of all points with a given distance from one point. This point is called center, the distance is called radius.
 64      * A circle can be constructed by providing a center and a point on the circle or a center and a radius (given as a number, function,
 65      * line, or circle).
 66      * @class Creates a new circle object. Do not use this constructor to create a circle. Use {@link JXG.Board#create} with
 67      * type {@link Circle} instead.
 68      * @constructor
 69      * @augments JXG.GeometryElement
 70      * @param {JXG.Board} board The board the new circle is drawn on.
 71      * @param {String} method Can be
 72      * <ul><li> <b>'twoPoints'</b> which means the circle is defined by its center and a point on the circle.</li>
 73      * <li><b>'pointRadius'</b> which means the circle is defined by its center and its radius in user units</li>
 74      * <li><b>'pointLine'</b> which means the circle is defined by its center and its radius given by the distance from the startpoint and the endpoint of the line</li>
 75      * <li><b>'pointCircle'</b> which means the circle is defined by its center and its radius given by the radius of another circle</li></ul>
 76      * The parameters p1, p2 and radius must be set according to this method parameter.
 77      * @param {JXG.Point} par1 center of the circle.
 78      * @param {JXG.Point|JXG.Line|JXG.Circle} par2 Can be
 79      * <ul><li>a point on the circle if method is 'twoPoints'</li>
 80      * <li>a line if the method is 'pointLine'</li>
 81      * <li>a circle if the method is 'pointCircle'</li></ul>
 82      * @param {Object} attributes
 83      * @see JXG.Board#generateName
 84      */
 85     JXG.Circle = function (board, method, par1, par2, attributes) {
 86         // Call the constructor of GeometryElement
 87         this.constructor(board, attributes, Const.OBJECT_TYPE_CIRCLE, Const.OBJECT_CLASS_CIRCLE);
 88 
 89         /**
 90          * Stores the given method.
 91          * Can be
 92          * <ul><li><b>'twoPoints'</b> which means the circle is defined by its center and a point on the circle.</li>
 93          * <li><b>'pointRadius'</b> which means the circle is defined by its center and its radius given in user units or as term.</li>
 94          * <li><b>'pointLine'</b> which means the circle is defined by its center and its radius given by the distance from the startpoint and the endpoint of the line.</li>
 95          * <li><b>'pointCircle'</b> which means the circle is defined by its center and its radius given by the radius of another circle.</li></ul>
 96          * @type String
 97          * @see #center
 98          * @see #point2
 99          * @see #radius
100          * @see #line
101          * @see #circle
102          */
103         this.method = method;
104 
105         // this is kept so existing code won't ne broken
106         this.midpoint = this.board.select(par1);
107 
108         /**
109          * The circles center. Do not set this parameter directly as it will break JSXGraph's update system.
110          * @type JXG.Point
111          */
112         this.center = this.board.select(par1);
113 
114         /** Point on the circle only set if method equals 'twoPoints'. Do not set this parameter directly as it will break JSXGraph's update system.
115          * @type JXG.Point
116          * @see #method
117          */
118         this.point2 = null;
119 
120         /** Radius of the circle
121          * only set if method equals 'pointRadius'
122          * @type Number
123          * @default null
124          * @see #method
125          */
126         this.radius = 0;
127 
128         /** Line defining the radius of the circle given by the distance from the startpoint and the endpoint of the line
129          * only set if method equals 'pointLine'. Do not set this parameter directly as it will break JSXGraph's update system.
130          * @type JXG.Line
131          * @default null
132          * @see #method
133          */
134         this.line = null;
135 
136         /** Circle defining the radius of the circle given by the radius of the other circle
137          * only set if method equals 'pointLine'. Do not set this parameter directly as it will break JSXGraph's update system.
138          * @type JXG.Circle
139          * @default null
140          * @see #method
141          */
142         this.circle = null;
143 
144         if (method === 'twoPoints') {
145             this.point2 = board.select(par2);
146             this.radius = this.Radius();
147         } else if (method === 'pointRadius') {
148             this.gxtterm = par2;
149             // Converts GEONExT syntax into JavaScript syntax and generally ensures that the radius is a function
150             this.updateRadius = Type.createFunction(par2, this.board, null, true);
151             // First evaluation of the radius function
152             this.updateRadius();
153         } else if (method === 'pointLine') {
154             // dann ist p2 die Id eines Objekts vom Typ Line!
155             this.line = board.select(par2);
156             this.radius = this.line.point1.coords.distance(Const.COORDS_BY_USER, this.line.point2.coords);
157         } else if (method === 'pointCircle') {
158             // dann ist p2 die Id eines Objekts vom Typ Circle!
159             this.circle = board.select(par2);
160             this.radius = this.circle.Radius();
161         }
162 
163         // create Label
164         this.id = this.board.setId(this, 'C');
165         this.board.renderer.drawEllipse(this);
166         this.board.finalizeAdding(this);
167 
168         this.createGradient();
169         this.elType = 'circle';
170         this.createLabel();
171 
172         this.center.addChild(this);
173 
174         if (method === 'pointRadius') {
175             this.notifyParents(par2);
176         } else if (method === 'pointLine') {
177             this.line.addChild(this);
178         } else if (method === 'pointCircle') {
179             this.circle.addChild(this);
180         } else if (method === 'twoPoints') {
181             this.point2.addChild(this);
182         }
183 
184         this.methodMap = Type.deepCopy(this.methodMap, {
185             setRadius: 'setRadius',
186             getRadius: 'getRadius',
187             Area: 'Area',
188             area: 'Area',
189             radius: 'Radius',
190             center: 'center',
191             line: 'line',
192             point2: 'point2'
193         });
194     };
195 
196     JXG.Circle.prototype = new GeometryElement();
197 
198     JXG.extend(JXG.Circle.prototype, /** @lends JXG.Circle.prototype */ {
199         /**
200          * Checks whether (x,y) is near the circle line or inside of the ellipse
201          * (in case JXG.Options.conic#hasInnerPoints is true).
202          * @param {Number} x Coordinate in x direction, screen coordinates.
203          * @param {Number} y Coordinate in y direction, screen coordinates.
204          * @returns {Boolean} True if (x,y) is near the circle, False otherwise.
205          * @private
206          */
207         hasPoint: function (x, y) {
208             var prec, type,
209                 mp = this.center.coords.usrCoords,
210                 p = new Coords(Const.COORDS_BY_SCREEN, [x, y], this.board),
211                 r = this.Radius(),
212                 dx, dy, dist;
213 
214 
215             if (Type.isObject(Type.evaluate(this.visProp.precision))) {
216                 type = this.board._inputDevice;
217                 prec = Type.evaluate(this.visProp.precision[type]);
218             } else {
219                 // 'inherit'
220                 prec = this.board.options.precision.hasPoint;
221             }
222             dx = mp[1] - p.usrCoords[1];
223             dy = mp[2] - p.usrCoords[2];
224             dist = Math.sqrt(dx * dx + dy * dy);
225             // We have to use usrCoords, since Radius is available in usrCoords only.
226             prec += Type.evaluate(this.visProp.strokewidth) * 0.5;
227             prec /= Math.sqrt(this.board.unitX * this.board.unitY);
228 
229             if (Type.evaluate(this.visProp.hasinnerpoints)) {
230                 return (dist < r + prec);
231             }
232 
233             return (Math.abs(dist - r) < prec);
234         },
235 
236         /**
237          * Used to generate a polynomial for a point p that lies on this circle.
238          * @param {JXG.Point} p The point for which the polynomial is generated.
239          * @returns {Array} An array containing the generated polynomial.
240          * @private
241          */
242         generatePolynomial: function (p) {
243             /*
244              * We have four methods to construct a circle:
245              *   (a) Two points
246              *   (b) center and radius
247              *   (c) center and radius given by length of a segment
248              *   (d) center and radius given by another circle
249              *
250              * In case (b) we have to distinguish two cases:
251              *  (i)  radius is given as a number
252              *  (ii) radius is given as a function
253              * In the latter case there's no guarantee the radius depends on other geometry elements
254              * in a polynomial way so this case has to be omitted.
255              *
256              * Another tricky case is case (d):
257              * The radius depends on another circle so we have to cycle through the ancestors of each circle
258              * until we reach one that's radius does not depend on another circles radius.
259              *
260              *
261              * All cases (a) to (d) vary only in calculation of the radius. So the basic formulae for
262              * a glider G (g1,g2) on a circle with center M (m1,m2) and radius r is just:
263              *
264              *     (g1-m1)^2 + (g2-m2)^2 - r^2 = 0
265              *
266              * So the easiest case is (b) with a fixed radius given as a number. The other two cases (a)
267              * and (c) are quite the same: Euclidean distance between two points A (a1,a2) and B (b1,b2),
268              * squared:
269              *
270              *     r^2 = (a1-b1)^2 + (a2-b2)^2
271              *
272              * For case (d) we have to cycle recursively through all defining circles and finally return the
273              * formulae for calculating r^2. For that we use JXG.Circle.symbolic.generateRadiusSquared().
274              */
275             var m1 = this.center.symbolic.x,
276                 m2 = this.center.symbolic.y,
277                 g1 = p.symbolic.x,
278                 g2 = p.symbolic.y,
279                 rsq = this.generateRadiusSquared();
280 
281             /* No radius can be calculated (Case b.ii) */
282             if (rsq === '') {
283                 return [];
284             }
285 
286             return ['((' + g1 + ')-(' + m1 + '))^2 + ((' + g2 + ')-(' + m2 + '))^2 - (' + rsq + ')'];
287         },
288 
289         /**
290          * Generate symbolic radius calculation for loci determination with Groebner-Basis algorithm.
291          * @returns {String} String containing symbolic calculation of the circle's radius or an empty string
292          * if the radius can't be expressed in a polynomial equation.
293          * @private
294          */
295         generateRadiusSquared: function () {
296             /*
297              * Four cases:
298              *
299              *   (a) Two points
300              *   (b) center and radius
301              *   (c) center and radius given by length of a segment
302              *   (d) center and radius given by another circle
303              */
304             var m1, m2, p1, p2, q1, q2,
305                 rsq = '';
306 
307             if (this.method === "twoPoints") {
308                 m1 = this.center.symbolic.x;
309                 m2 = this.center.symbolic.y;
310                 p1 = this.point2.symbolic.x;
311                 p2 = this.point2.symbolic.y;
312 
313                 rsq = '((' + p1 + ')-(' + m1 + '))^2 + ((' + p2 + ')-(' + m2 + '))^2';
314             } else if (this.method === "pointRadius") {
315                 if (Type.isNumber(this.radius)) {
316                     rsq = (this.radius * this.radius).toString();
317                 }
318             } else if (this.method === "pointLine") {
319                 p1 = this.line.point1.symbolic.x;
320                 p2 = this.line.point1.symbolic.y;
321 
322                 q1 = this.line.point2.symbolic.x;
323                 q2 = this.line.point2.symbolic.y;
324 
325                 rsq = '((' + p1 + ')-(' + q1 + '))^2 + ((' + p2 + ')-(' + q2 + '))^2';
326             } else if (this.method === "pointCircle") {
327                 rsq = this.circle.Radius();
328             }
329 
330             return rsq;
331         },
332 
333         /**
334          * Uses the boards renderer to update the circle.
335          */
336         update: function () {
337             if (this.needsUpdate) {
338                 if (Type.evaluate(this.visProp.trace)) {
339                     this.cloneToBackground(true);
340                 }
341 
342                 if (this.method === 'pointLine') {
343                     this.radius = this.line.point1.coords.distance(Const.COORDS_BY_USER, this.line.point2.coords);
344                 } else if (this.method === 'pointCircle') {
345                     this.radius = this.circle.Radius();
346                 } else if (this.method === 'pointRadius') {
347                     this.radius = this.updateRadius();
348                 }
349 
350                 this.updateStdform();
351                 this.updateQuadraticform();
352             }
353 
354             return this;
355         },
356 
357         /**
358          * Updates this circle's {@link JXG.Circle#quadraticform}.
359          * @private
360          */
361         updateQuadraticform: function () {
362             var m = this.center,
363                 mX = m.X(),
364                 mY = m.Y(),
365                 r = this.Radius();
366 
367             this.quadraticform = [
368                 [mX * mX + mY * mY - r * r, -mX, -mY],
369                 [-mX, 1, 0],
370                 [-mY, 0, 1]
371             ];
372         },
373 
374         /**
375          * Updates the stdform derived from the position of the center and the circle's radius.
376          * @private
377          */
378         updateStdform: function () {
379             this.stdform[3] = 0.5;
380             this.stdform[4] = this.Radius();
381             this.stdform[1] = -this.center.coords.usrCoords[1];
382             this.stdform[2] = -this.center.coords.usrCoords[2];
383             if (!isFinite(this.stdform[4])) {
384                 this.stdform[0] = Type.exists(this.point2) ? -(
385                     this.stdform[1] * this.point2.coords.usrCoords[1] +
386                     this.stdform[2] * this.point2.coords.usrCoords[2]
387                 ) : 0;
388             }
389             this.normalize();
390         },
391 
392         /**
393          * Uses the boards renderer to update the circle.
394          * @private
395          */
396         updateRenderer: function () {
397             // var wasReal;
398 
399             if (!this.needsUpdate) {
400                 return this;
401             }
402 
403             if (this.visPropCalc.visible) {
404                 // wasReal = this.isReal;
405                 this.isReal = (!isNaN(this.center.coords.usrCoords[1] + this.center.coords.usrCoords[2] + this.Radius())) && this.center.isReal;
406 
407                 if (//wasReal &&
408                     !this.isReal) {
409                     this.updateVisibility(false);
410                 }
411             }
412 
413             // Update the position
414             if (this.visPropCalc.visible) {
415                 this.board.renderer.updateEllipse(this);
416             }
417 
418             // Update the label if visible.
419             if (this.hasLabel && this.visPropCalc.visible && this.label &&
420                 this.label.visPropCalc.visible && this.isReal) {
421 
422                 this.label.update();
423                 this.board.renderer.updateText(this.label);
424             }
425 
426             // Update rendNode display
427             this.setDisplayRendNode();
428             // if (this.visPropCalc.visible !== this.visPropOld.visible) {
429             //     this.board.renderer.display(this, this.visPropCalc.visible);
430             //     this.visPropOld.visible = this.visPropCalc.visible;
431             //
432             //     if (this.hasLabel) {
433             //         this.board.renderer.display(this.label, this.label.visPropCalc.visible);
434             //     }
435             // }
436 
437             this.needsUpdate = false;
438             return this;
439         },
440 
441         /**
442          * Finds dependencies in a given term and resolves them by adding the elements referenced in this
443          * string to the circle's list of ancestors.
444          * @param {String} contentStr
445          * @private
446          */
447         notifyParents: function (contentStr) {
448             if (Type.isString(contentStr)) {
449                 GeonextParser.findDependencies(this, contentStr, this.board);
450             }
451         },
452 
453         /**
454          * Set a new radius, then update the board.
455          * @param {String|Number|function} r A string, function or number describing the new radius.
456          * @returns {JXG.Circle} Reference to this circle
457          */
458         setRadius: function (r) {
459             this.updateRadius = Type.createFunction(r, this.board, null, true);
460             this.board.update();
461 
462             return this;
463         },
464 
465         /**
466          * Calculates the radius of the circle.
467          * @param {String|Number|function} [value] Set new radius
468          * @returns {Number} The radius of the circle
469          */
470         Radius: function (value) {
471             if (Type.exists(value)) {
472                 this.setRadius(value);
473                 return this.Radius();
474             }
475 
476             if (this.method === 'twoPoints') {
477                 if (Type.cmpArrays(this.point2.coords.usrCoords, [0, 0, 0]) ||
478                         Type.cmpArrays(this.center.coords.usrCoords, [0, 0, 0])) {
479 
480                     return NaN;
481                 }
482 
483                 return this.center.Dist(this.point2);
484             }
485 
486             if (this.method === 'pointLine' || this.method === 'pointCircle') {
487                 return this.radius;
488             }
489 
490             if (this.method === 'pointRadius') {
491                 return this.updateRadius();
492             }
493 
494             return NaN;
495         },
496 
497         /**
498          * Use {@link JXG.Circle#Radius}.
499          * @deprecated
500          */
501         getRadius: function () {
502             JXG.deprecated('Circle.getRadius()', 'Circle.Radius()');
503             return this.Radius();
504         },
505 
506         // documented in geometry element
507         getTextAnchor: function () {
508             return this.center.coords;
509         },
510 
511         // documented in geometry element
512         getLabelAnchor: function () {
513             var x, y,
514                 r = this.Radius(),
515                 c = this.center.coords.usrCoords;
516 
517             switch (Type.evaluate(this.visProp.label.position)) {
518             case 'lft':
519                 x = c[1] - r;
520                 y = c[2];
521                 break;
522             case 'llft':
523                 x = c[1] - Math.sqrt(0.5) * r;
524                 y = c[2] - Math.sqrt(0.5) * r;
525                 break;
526             case 'rt':
527                 x = c[1] + r;
528                 y = c[2];
529                 break;
530             case 'lrt':
531                 x = c[1] + Math.sqrt(0.5) * r;
532                 y = c[2] - Math.sqrt(0.5) * r;
533                 break;
534             case 'urt':
535                 x = c[1] + Math.sqrt(0.5) * r;
536                 y = c[2] + Math.sqrt(0.5) * r;
537                 break;
538             case 'top':
539                 x = c[1];
540                 y = c[2] + r;
541                 break;
542             case 'bot':
543                 x = c[1];
544                 y = c[2] - r;
545                 break;
546             default:
547                 // includes case 'ulft'
548                 x = c[1] - Math.sqrt(0.5) * r;
549                 y = c[2] + Math.sqrt(0.5) * r;
550                 break;
551             }
552 
553             return new Coords(Const.COORDS_BY_USER, [x, y], this.board);
554         },
555 
556         // documented in geometry element
557         cloneToBackground: function () {
558             var er,
559                 r = this.Radius(),
560                 copy = {
561                     id: this.id + 'T' + this.numTraces,
562                     elementClass: Const.OBJECT_CLASS_CIRCLE,
563                     center: {
564                         coords: this.center.coords
565                     },
566                     Radius: function () {
567                         return r;
568                     },
569                     getRadius: function () {
570                         return r;
571                     },
572                     board: this.board,
573                     visProp: Type.deepCopy(this.visProp, this.visProp.traceattributes, true)
574                 };
575 
576             copy.visProp.layer = this.board.options.layer.trace;
577 
578             this.numTraces++;
579             Type.clearVisPropOld(copy);
580             copy.visPropCalc = {
581                 visible: Type.evaluate(copy.visProp.visible)
582             };
583 
584             er = this.board.renderer.enhancedRendering;
585             this.board.renderer.enhancedRendering = true;
586             this.board.renderer.drawEllipse(copy);
587             this.board.renderer.enhancedRendering = er;
588             this.traces[copy.id] = copy.rendNode;
589 
590             return this;
591         },
592 
593         /**
594          * Add transformations to this circle.
595          * @param {JXG.Transformation|Array} transform Either one {@link JXG.Transformation} or an array of {@link JXG.Transformation}s.
596          * @returns {JXG.Circle} Reference to this circle object.
597          */
598         addTransform: function (transform) {
599             var i,
600                 list = Type.isArray(transform) ? transform : [transform],
601                 len = list.length;
602 
603             for (i = 0; i < len; i++) {
604                 this.center.transformations.push(list[i]);
605 
606                 if (this.method === 'twoPoints') {
607                     this.point2.transformations.push(list[i]);
608                 }
609             }
610 
611             return this;
612         },
613 
614         // see element.js
615         snapToGrid: function () {
616             var forceIt = Type.evaluate(this.visProp.snaptogrid);
617 
618             this.center.handleSnapToGrid(forceIt, true);
619             if (this.method === 'twoPoints') {
620                 this.point2.handleSnapToGrid(forceIt, true);
621             }
622 
623             return this;
624         },
625 
626         // see element.js
627         snapToPoints: function () {
628             var forceIt = Type.evaluate(this.visProp.snaptopoints);
629 
630             this.center.handleSnapToPoints(forceIt);
631             if (this.method === 'twoPoints') {
632                 this.point2.handleSnapToPoints(forceIt);
633             }
634 
635             return this;
636         },
637 
638         /**
639          * Treats the circle as parametric curve and calculates its X coordinate.
640          * @param {Number} t Number between 0 and 1.
641          * @returns {Number} <tt>X(t)= radius*cos(t)+centerX</tt>.
642          */
643         X: function (t) {
644             return this.Radius() * Math.cos(t * 2 * Math.PI) + this.center.coords.usrCoords[1];
645         },
646 
647         /**
648          * Treats the circle as parametric curve and calculates its Y coordinate.
649          * @param {Number} t Number between 0 and 1.
650          * @returns {Number} <tt>X(t)= radius*sin(t)+centerY</tt>.
651          */
652         Y: function (t) {
653             return this.Radius() * Math.sin(t * 2 * Math.PI) + this.center.coords.usrCoords[2];
654         },
655 
656         /**
657          * Treat the circle as parametric curve and calculates its Z coordinate.
658          * @param {Number} t ignored
659          * @returns {Number} 1.0
660          */
661         Z: function (t) {
662             return 1.0;
663         },
664 
665         /**
666          * Returns 0.
667          * @private
668          */
669         minX: function () {
670             return 0.0;
671         },
672 
673         /**
674          * Returns 1.
675          * @private
676          */
677         maxX: function () {
678             return 1.0;
679         },
680 
681         /**
682          * Circle area
683          * @returns {Number} area of the circle.
684          */
685         Area: function () {
686             var r = this.Radius();
687 
688             return r * r * Math.PI;
689         },
690 
691         /**
692          * Get bounding box of the circle.
693          * @returns {Array} [x1, y1, x2, y2]
694          */
695         bounds: function () {
696             var uc = this.center.coords.usrCoords,
697                 r = this.Radius();
698 
699             return [uc[1] - r, uc[2] + r, uc[1] + r, uc[2] - r];
700         },
701 
702         /**
703          * Get data to construct this element. Data consists of the parent elements
704          * and static data like radius.
705          * @returns {Array} data necessary to construct this element
706          */
707         getParents: function() {
708             if (this.parents.length === 1) {  // i.e. this.method === 'pointRadius'
709                 return this.parents.concat(this.radius);
710             }
711             return this.parents;
712         }
713     });
714 
715     /**
716      * @class This element is used to provide a constructor for a circle.
717      * @pseudo
718      * @description  A circle consists of all points with a given distance from one point. This point is called center, the distance is called radius.
719      * A circle can be constructed by providing a center and a point on the circle or a center and a radius (given as a number, function,
720      * line, or circle).
721      * @name Circle
722      * @augments JXG.Circle
723      * @constructor
724      * @type JXG.Circle
725      * @throws {Exception} If the element cannot be constructed with the given parent objects an exception is thrown.
726      * @param {JXG.Point_number,JXG.Point,JXG.Line,JXG.Circle} center,radius The center must be given as a {@link JXG.Point}, see {@link JXG.providePoints}, but the radius can be given
727      * as a number (which will create a circle with a fixed radius), another {@link JXG.Point}, a {@link JXG.Line} (the distance of start and end point of the
728      * line will determine the radius), or another {@link JXG.Circle}.
729      * @example
730      * // Create a circle providing two points
731      * var p1 = board.create('point', [2.0, 2.0]),
732      *     p2 = board.create('point', [2.0, 0.0]),
733      *     c1 = board.create('circle', [p1, p2]);
734      *
735      * // Create another circle using the above circle
736      * var p3 = board.create('point', [3.0, 2.0]),
737      *     c2 = board.create('circle', [p3, c1]);
738      * </pre><div class="jxgbox" id="JXG5f304d31-ef20-4a8e-9c0e-ea1a2b6c79e0" style="width: 400px; height: 400px;"></div>
739      * <script type="text/javascript">
740      * (function() {
741      *   var cex1_board = JXG.JSXGraph.initBoard('JXG5f304d31-ef20-4a8e-9c0e-ea1a2b6c79e0', {boundingbox: [-1, 9, 9, -1], axis: true, showcopyright: false, shownavigation: false});
742      *       cex1_p1 = cex1_board.create('point', [2.0, 2.0]),
743      *       cex1_p2 = cex1_board.create('point', [2.0, 0.0]),
744      *       cex1_c1 = cex1_board.create('circle', [cex1_p1, cex1_p2]),
745      *       cex1_p3 = cex1_board.create('point', [3.0, 2.0]),
746      *       cex1_c2 = cex1_board.create('circle', [cex1_p3, cex1_c1]);
747      * })();
748      * </script><pre>
749      * @example
750      * // Create a circle providing two points
751      * var p1 = board.create('point', [2.0, 2.0]),
752      *     c1 = board.create('circle', [p1, 3]);
753      *
754      * // Create another circle using the above circle
755      * var c2 = board.create('circle', [function() { return [p1.X(), p1.Y() + 1];}, function() { return c1.Radius(); }]);
756      * </pre><div class="jxgbox" id="JXG54165f60-93b9-441d-8979-ac5d0f193020" style="width: 400px; height: 400px;"></div>
757      * <script type="text/javascript">
758      * (function() {
759      * var board = JXG.JSXGraph.initBoard('JXG54165f60-93b9-441d-8979-ac5d0f193020', {boundingbox: [-1, 9, 9, -1], axis: true, showcopyright: false, shownavigation: false});
760      * var p1 = board.create('point', [2.0, 2.0]);
761      * var c1 = board.create('circle', [p1, 3]);
762      *
763      * // Create another circle using the above circle
764      * var c2 = board.create('circle', [function() { return [p1.X(), p1.Y() + 1];}, function() { return c1.Radius(); }]);
765      * })();
766      * </script><pre>
767      * @example
768      * var li = board.create('line', [1,1,1], {strokeColor: '#aaaaaa'});
769      * var reflect = board.create('transform', [li], {type: 'reflect'});
770      *
771      * var c1 = board.create('circle', [[-2,-2], [-2, -1]], {center: {visible:true}});
772      * var c2 = board.create('circle', [c1, reflect]);
773      *      * </pre><div id="JXGa2a5a870-5dbb-11e8-9fb9-901b0e1b8723" class="jxgbox" style="width: 300px; height: 300px;"></div>
774      * <script type="text/javascript">
775      *     (function() {
776      *         var board = JXG.JSXGraph.initBoard('JXGa2a5a870-5dbb-11e8-9fb9-901b0e1b8723',
777      *             {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false});
778      *             var li = board.create('line', [1,1,1], {strokeColor: '#aaaaaa'});
779      *             var reflect = board.create('transform', [li], {type: 'reflect'});
780      *
781      *             var c1 = board.create('circle', [[-2,-2], [-2, -1]], {center: {visible:true}});
782      *             var c2 = board.create('circle', [c1, reflect]);
783      *     })();
784      *
785      * </script><pre>
786      *
787      * @example
788      * var t = board.create('transform', [2, 1.5], {type: 'scale'});
789      * var c1 = board.create('circle', [[1.3, 1.3], [0, 1.3]], {strokeColor: 'black', center: {visible:true}});
790      * var c2 = board.create('circle', [c1, t], {strokeColor: 'black'});
791      *
792      * </pre><div id="JXG0686a222-6339-11e8-9fb9-901b0e1b8723" class="jxgbox" style="width: 300px; height: 300px;"></div>
793      * <script type="text/javascript">
794      *     (function() {
795      *         var board = JXG.JSXGraph.initBoard('JXG0686a222-6339-11e8-9fb9-901b0e1b8723',
796      *             {boundingbox: [-8, 8, 8,-8], axis: true, showcopyright: false, shownavigation: false});
797      *     var t = board.create('transform', [2, 1.5], {type: 'scale'});
798      *     var c1 = board.create('circle', [[1.3, 1.3], [0, 1.3]], {strokeColor: 'black', center: {visible:true}});
799      *     var c2 = board.create('circle', [c1, t], {strokeColor: 'black'});
800      *
801      *     })();
802      *
803      * </script><pre>
804      *
805      */
806     JXG.createCircle = function (board, parents, attributes) {
807         var el, p, i, attr, obj,
808             isDraggable = true;
809 
810         p = [];
811         obj = board.select(parents[0]);
812         if (Type.isObject(obj) && obj.elementClass === Const.OBJECT_CLASS_CIRCLE &&
813             Type.isTransformationOrArray(parents[1])) {
814 
815             attr = Type.copyAttributes(attributes, board.options, 'circle');
816             // if (!Type.exists(attr.type) || attr.type.toLowerCase() !== 'euclidean') {
817             //     // Create a circle element from a circle and a Euclidean transformation
818             //     el = JXG.createCircle(board, [obj.center, function() { return obj.Radius(); }], attr);
819             // } else {
820                 // Create a conic element from a circle and a projective transformation
821                 el = Conic.createEllipse(board, [obj.center, obj.center, function() { return 2 * obj.Radius(); }], attr);
822             // }
823             el.addTransform(parents[1]);
824             return el;
825 
826         }
827         // Circle defined by points
828         for (i = 0; i < parents.length; i++) {
829             if (Type.isPointType(board, parents[i])) {
830                 p = p.concat(Type.providePoints(board, [parents[i]], attributes, 'circle', ['center']));
831                 if (p[p.length - 1] === false) {
832                     throw new Error('JSXGraph: Can\'t create circle from this type. Please provide a point type.');
833                 }
834             } else {
835                 p.push(parents[i]);
836             }
837         }
838 
839         attr = Type.copyAttributes(attributes, board.options, 'circle');
840 
841         if (p.length === 2 && Type.isPoint(p[0]) && Type.isPoint(p[1])) {
842             // Point/Point
843             el = new JXG.Circle(board, 'twoPoints', p[0], p[1], attr);
844         } else if ((Type.isNumber(p[0]) || Type.isFunction(p[0]) || Type.isString(p[0])) &&
845                     Type.isPoint(p[1])) {
846             // Number/Point
847             el = new JXG.Circle(board, 'pointRadius', p[1], p[0], attr);
848         } else if ((Type.isNumber(p[1]) || Type.isFunction(p[1]) || Type.isString(p[1])) &&
849                 Type.isPoint(p[0])) {
850             // Point/Number
851             el = new JXG.Circle(board, 'pointRadius', p[0], p[1], attr);
852         } else if ((p[0].elementClass === Const.OBJECT_CLASS_CIRCLE) && Type.isPoint(p[1])) {
853             // Circle/Point
854             el = new JXG.Circle(board, 'pointCircle', p[1], p[0], attr);
855         } else if ((p[1].elementClass === Const.OBJECT_CLASS_CIRCLE) && Type.isPoint(p[0])) {
856             // Point/Circle
857             el = new JXG.Circle(board, 'pointCircle', p[0], p[1], attr);
858         } else if ((p[0].elementClass === Const.OBJECT_CLASS_LINE) && Type.isPoint(p[1])) {
859             // Line/Point
860             el = new JXG.Circle(board, 'pointLine', p[1], p[0], attr);
861         } else if ((p[1].elementClass === Const.OBJECT_CLASS_LINE) && Type.isPoint(p[0])) {
862             // Point/Line
863             el = new JXG.Circle(board, 'pointLine', p[0], p[1], attr);
864         } else if (parents.length === 3 && Type.isPoint(p[0]) && Type.isPoint(p[1]) && Type.isPoint(p[2])) {
865             // Circle through three points
866             // Check if circumcircle element is available
867             if (JXG.elements.circumcircle) {
868                 el = JXG.elements.circumcircle(board, p, attr);
869             } else {
870                 throw new Error('JSXGraph: Can\'t create circle with three points. Please include the circumcircle element (element/composition).');
871             }
872 
873         } else {
874             throw new Error("JSXGraph: Can't create circle with parent types '" +
875                 (typeof parents[0]) + "' and '" + (typeof parents[1]) + "'." +
876                 "\nPossible parent types: [point,point], [point,number], [point,function], [point,circle], [point,point,point], [circle,transformation]");
877         }
878 
879         el.isDraggable = isDraggable;
880         el.setParents(p);
881         el.elType = 'circle';
882         for (i = 0; i < p.length; i++) {
883             if (Type.isPoint(p[i])) {
884                 el.inherits.push(p[i]);
885             }
886         }
887         return el;
888     };
889 
890     JXG.registerElement('circle', JXG.createCircle);
891 
892     return {
893         Circle: JXG.Circle,
894         createCircle: JXG.createCircle
895     };
896 });
897