packages/cafeteria/app/external/sylvester/scripts/sylvester-0.1.3.js
function(obj) {
    if (obj.normal) { return obj.distanceFrom(this); }
    if (obj.direction) {
      // obj is a line
      if (this.isParallelTo(obj)) { return this.distanceFrom(obj.anchor); }
      var N = this.direction.cross(obj.direction).toUnitVector().elements;
      var A = this.anchor.elements, B = obj.anchor.elements;
      return Math.abs((A[0] - B[0]) * N[0] + (A[1] - B[1]) * N[1] + (A[2] - B[2]) * N[2]);
    } else {
      // obj is a point
      var P = obj.elements || obj;
      var A = this.anchor.elements, D = this.direction.elements;
      var PA1 = P[0] - A[0], PA2 = P[1] - A[1], PA3 = (P[2] || 0) - A[2];
      var modPA = Math.sqrt(PA1*PA1 + PA2*PA2 + PA3*PA3);
      if (modPA === 0) return 0;
      // Assumes direction vector is normalized
      var cosTheta = (PA1 * D[0] + PA2 * D[1] + PA3 * D[2]) / modPA;
      var sin2 = 1 - cosTheta*cosTheta;
      return Math.abs(modPA * Math.sqrt(sin2 < 0 ? 0 : sin2));
    }
  }
function(obj) {
    if (obj.normal) { return obj.distanceFrom(this); }
    if (obj.direction) {
      // obj is a line
      if (this.isParallelTo(obj)) { return this.distanceFrom(obj.anchor); }
      var N = this.direction.cross(obj.direction).toUnitVector().elements;
      var A = this.anchor.elements, B = obj.anchor.elements;
      return Math.abs((A[0] - B[0]) * N[0] + (A[1] - B[1]) * N[1] + (A[2] - B[2]) * N[2]);
    } else {
      // obj is a point
      var P = obj.elements || obj;
      var A = this.anchor.elements, D = this.direction.elements;
      var PA1 = P[0] - A[0], PA2 = P[1] - A[1], PA3 = (P[2] || 0) - A[2];
      var modPA = Math.sqrt(PA1*PA1 + PA2*PA2 + PA3*PA3);
      if (modPA === 0) return 0;
      // Assumes direction vector is normalized
      var cosTheta = (PA1 * D[0] + PA2 * D[1] + PA3 * D[2]) / modPA;
      var sin2 = 1 - cosTheta*cosTheta;
      return Math.abs(modPA * Math.sqrt(sin2 < 0 ? 0 : sin2));
    }
  }
function ( obj ) { if ( obj . normal ) { return obj . distanceFrom ( this ) ; } if ( obj . direction ) { // obj is a line if ( this . isParallelTo ( obj ) ) { return this . distanceFrom ( obj . anchor ) ; } var N = this . direction . cross ( obj . direction ) . toUnitVector ( ) . elements ; var A = this . anchor . elements , B = obj . anchor . elements ; return Math . abs ( ( A [ 0 ] - B [ 0 ] ) * N [ 0 ] + ( A [ 1 ] - B [ 1 ] ) * N [ 1 ] + ( A [ 2 ] - B [ 2 ] ) * N [ 2 ] ) ; } else { // obj is a point var P = obj . elements || obj ; var A = this . anchor . elements , D = this . direction . elements ; var PA1 = P [ 0 ] - A [ 0 ] , PA2 = P [ 1 ] - A [ 1 ] , PA3 = ( P [ 2 ] || 0 ) - A [ 2 ] ; var modPA = Math . sqrt ( PA1 * PA1 + PA2 * PA2 + PA3 * PA3 ) ; if ( modPA === 0 ) return 0 ; // Assumes direction vector is normalized var cosTheta = ( PA1 * D [ 0 ] + PA2 * D [ 1 ] + PA3 * D [ 2 ] ) / modPA ; var sin2 = 1 - cosTheta * cosTheta ; return Math . abs ( modPA * Math . sqrt ( sin2 < 0 ? 0 : sin2 ) ) ; } }
Returns the line's perpendicular distance from the argument, which can be a point, a line or a plane
Returns the line s perpendicular distance from the argument which can be a point a line or a plane