demos/Collage/fabric.js
function(ex, ey, oCoords) {
      var b1, b2, a1, a2, xi, yi,
          xcount = 0,
          iLine;

      for (var lineKey in oCoords) {
        iLine = oCoords[lineKey];
        // optimisation 1: line below dot. no cross
        if ((iLine.o.y < ey) && (iLine.d.y < ey)) {
          continue;
        }
        // optimisation 2: line above dot. no cross
        if ((iLine.o.y >= ey) && (iLine.d.y >= ey)) {
          continue;
        }
        // optimisation 3: vertical line case
        if ((iLine.o.x == iLine.d.x) && (iLine.o.x >= ex)) {
          xi = iLine.o.x;
          yi = ey;
        }
        // calculate the intersection point
        else {
          b1 = 0;
          b2 = (iLine.d.y-iLine.o.y)/(iLine.d.x-iLine.o.x);
          a1 = ey-b1*ex;
          a2 = iLine.o.y-b2*iLine.o.x;

          xi = - (a1-a2)/(b1-b2);
          yi = a1+b1*xi;
        }
        // dont count xi < ex cases
        if (xi >= ex) {
          xcount += 1;
        }
        // optimisation 4: specific for square images
        if (xcount == 2) {
          break;
        }
      }
      return xcount;
    }
function(ex, ey, oCoords) {
      var b1, b2, a1, a2, xi, yi,
          xcount = 0,
          iLine;

      for (var lineKey in oCoords) {
        iLine = oCoords[lineKey];
        // optimisation 1: line below dot. no cross
        if ((iLine.o.y < ey) && (iLine.d.y < ey)) {
          continue;
        }
        // optimisation 2: line above dot. no cross
        if ((iLine.o.y >= ey) && (iLine.d.y >= ey)) {
          continue;
        }
        // optimisation 3: vertical line case
        if ((iLine.o.x == iLine.d.x) && (iLine.o.x >= ex)) {
          xi = iLine.o.x;
          yi = ey;
        }
        // calculate the intersection point
        else {
          b1 = 0;
          b2 = (iLine.d.y-iLine.o.y)/(iLine.d.x-iLine.o.x);
          a1 = ey-b1*ex;
          a2 = iLine.o.y-b2*iLine.o.x;

          xi = - (a1-a2)/(b1-b2);
          yi = a1+b1*xi;
        }
        // dont count xi < ex cases
        if (xi >= ex) {
          xcount += 1;
        }
        // optimisation 4: specific for square images
        if (xcount == 2) {
          break;
        }
      }
      return xcount;
    }
function ( ex , ey , oCoords ) { var b1 , b2 , a1 , a2 , xi , yi , xcount = 0 , iLine ; for ( var lineKey in oCoords ) { iLine = oCoords [ lineKey ] ; // optimisation 1: line below dot. no cross if ( ( iLine . o . y < ey ) && ( iLine . d . y < ey ) ) { continue ; } // optimisation 2: line above dot. no cross if ( ( iLine . o . y >= ey ) && ( iLine . d . y >= ey ) ) { continue ; } // optimisation 3: vertical line case if ( ( iLine . o . x == iLine . d . x ) && ( iLine . o . x >= ex ) ) { xi = iLine . o . x ; yi = ey ; } // calculate the intersection point else { b1 = 0 ; b2 = ( iLine . d . y - iLine . o . y ) / ( iLine . d . x - iLine . o . x ) ; a1 = ey - b1 * ex ; a2 = iLine . o . y - b2 * iLine . o . x ; xi = - ( a1 - a2 ) / ( b1 - b2 ) ; yi = a1 + b1 * xi ; } // dont count xi < ex cases if ( xi >= ex ) { xcount += 1 ; } // optimisation 4: specific for square images if ( xcount == 2 ) { break ; } } return xcount ; }
Helper method to determine how many cross points are between the 4 image edges
and the horizontal line determined by the position of our mouse when clicked on canvas
@method _findCrossPoints
@private
@param ex {Number} x coordinate of the mouse
@param ey {Number} y coordinate of the mouse
@param oCoords {Object} Coordinates of the image being evaluated
Helper method to determine how many cross points are between the 4 image edges and the horizontal line determined by the position of our mouse when clicked on canvas