1 /*
  2     Copyright 2012
  3         Matthias Ehmann,
  4         Michael Gerhaeuser,
  5         Carsten Miller,
  6         Alfred Wassermann
  7 
  8     This file is part of JSXGraph.
  9 
 10     JSXGraph is free software: you can redistribute it and/or modify
 11     it under the terms of the GNU Lesser General Public License as published by
 12     the Free Software Foundation, either version 3 of the License, or
 13     (at your option) any later version.
 14 
 15     JSXGraph is distributed in the hope that it will be useful,
 16     but WITHOUT ANY WARRANTY; without even the implied warranty of
 17     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 18     GNU Lesser General Public License for more details.
 19 
 20     You should have received a copy of the GNU Lesser General Public License
 21     along with JSXGraph.  If not, see <http://www.gnu.org/licenses/>.
 22 */
 23 
 24 JXG.extend(JXG.Board.prototype, /** @lends JXG.Board.prototype */ {
 25     intersection: function(el1, el2, i, j, pointObj){ 
 26         var p;
 27         
 28         el1 = JXG.getReference(this,el1);
 29         el2 = JXG.getReference(this,el2);
 30         
 31         // Get access to the intersection point object
 32         // This is necessary to read the property alwaysIntersect
 33         // see JXG.createIntersectionPoint() (Point.js)
 34         if (pointObj!=null) {
 35             p = pointObj.point;
 36         }
 37         
 38         if (el1.elementClass==JXG.OBJECT_CLASS_CURVE 
 39             && el2.elementClass==JXG.OBJECT_CLASS_CURVE
 40             && (el1.type!=JXG.OBJECT_TYPE_ARC 
 41                 || el2.type!=JXG.OBJECT_TYPE_ARC) ) {
 42                     
 43             // curve - curve, but not both are arcs 
 44             // TEMPORARY FIX!!!
 45             return function(){
 46                         return JXG.Math.Geometry.meetCurveCurve(el1,el2,i,j,el1.board); 
 47                     };
 48             
 49         } else if ( (el1.type==JXG.OBJECT_TYPE_ARC 
 50                       && el2.elementClass==JXG.OBJECT_CLASS_LINE) 
 51                     ||
 52                      (el2.type==JXG.OBJECT_TYPE_ARC 
 53                       && el1.elementClass==JXG.OBJECT_CLASS_LINE)) {
 54                           
 55             // arc - line   (arcs are of class curve, but are intersected like circles)
 56             // TEMPORARY FIX!!!
 57             return function(){
 58                         return JXG.Math.Geometry.meet(el1.stdform,el2.stdform,i,el1.board); 
 59                     };
 60                     
 61         } else if ( (el1.elementClass==JXG.OBJECT_CLASS_CURVE 
 62                       && el2.elementClass==JXG.OBJECT_CLASS_LINE)
 63                     ||
 64                      (el2.elementClass==JXG.OBJECT_CLASS_CURVE 
 65                       && el1.elementClass==JXG.OBJECT_CLASS_LINE)) {
 66                           
 67             // curve - line (this includes intersections between conic sections and lines
 68             return function(){
 69                         return JXG.Math.Geometry.meetCurveLine(el1,el2,i,el1.board, pointObj); 
 70                     };
 71                     
 72         } else if (el1.elementClass==JXG.OBJECT_CLASS_LINE 
 73                     && el2.elementClass==JXG.OBJECT_CLASS_LINE
 74                    )  {
 75                      
 76             // line - line, lines may also be segments.
 77             return function(){ 
 78                 var res, c,
 79                     first1 = el1.visProp.straightfirst;
 80                     first2 = el2.visProp.straightfirst;
 81                     last1 = el1.visProp.straightlast;
 82                     last2 = el2.visProp.straightlast;
 83 
 84                 /** 
 85                  * If one of the lines is a segment or ray and
 86                  * the the intersection point shpould disappear if outside 
 87                  * of the segment or ray we call
 88                  * meetSegmentSegment 
 89                  */
 90                 if (JXG.exists(p) && !p.visProp.alwaysintersect
 91                     && (first1==false
 92                         || last1==false
 93                         || first2==false
 94                         || last2==false)
 95                     ) {
 96 
 97                     res = JXG.Math.Geometry.meetSegmentSegment(
 98                         el1.point1.coords.usrCoords, el1.point2.coords.usrCoords,
 99                         el2.point1.coords.usrCoords, el2.point2.coords.usrCoords, 
100                         el1.board); 
101                         
102                     if ( (!first1 && res[1]<0)
103                          || (!last1 && res[1]>1)
104                          || (!first2 && res[2]<0)
105                          || (!last2 && res[2]>1) ) {
106                         c = [0,NaN,NaN];  // Non-existent
107                     } else {
108                         c = res[0];
109                     }
110                     return (new JXG.Coords(JXG.COORDS_BY_USER, c, el1.board));
111                 } else {
112                     return JXG.Math.Geometry.meet(el1.stdform,el2.stdform,i,el1.board);
113                 }
114             };
115         // All other combinations of circles and lines
116         } else {
117             return function(){return JXG.Math.Geometry.meet(el1.stdform,el2.stdform,i,el1.board); };
118         }
119     }, //returns a single point of intersection
120     intersectionFunc: function(el1,el2,i,j){ return this.intersection(el1,el2,i,j); },
121 
122     /**
123     * Intersection of circles and line
124     */ 
125     otherIntersection: function(el1,el2,el){ 
126         el1 = JXG.getReference(this,el1);
127         el2 = JXG.getReference(this,el2);
128         return function(){
129             var c = JXG.Math.Geometry.meet(el1.stdform,el2.stdform,0,el1.board);
130             if (Math.abs(el.X()-c.usrCoords[1])>JXG.Math.eps ||
131                 Math.abs(el.Y()-c.usrCoords[2])>JXG.Math.eps ||
132                 Math.abs(el.Z()-c.usrCoords[0])>JXG.Math.eps) {
133                 return c;
134             } else {
135                 return JXG.Math.Geometry.meet(el1.stdform,el2.stdform,1,el1.board);
136             }
137         };
138     }, //returns a single point of intersection
139 });
140