Gerard-Meier

Line class

Feb 10th, 2011
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Line(placePoint, endPoint) {
  2.     this.placePoint = placePoint;
  3.     this.endPoint   = endPoint;
  4.    
  5.     this.testWith   = function(lineB) {
  6.         var lambdaA = this.getLambda(lineB);
  7.        
  8.         if(lambdaA >= 0 && lambdaA <= 1) {
  9.             var lambdaB = lineB.getLambda(this);
  10.             if(lambdaB >= 0 && lambdaB <= 1) {
  11.                 // return the point of intersection:
  12.                 return lineB.getDirectionPoint().multiply(lambdaA).add(placePoint);
  13.             }
  14.         }
  15.        
  16.         return null;
  17.     }
  18.    
  19.     this.getLambda = function(lineB) {
  20.         var rn = this.getDirectionPoint().dotProduct(lineB.getNormalized());
  21.         var sn = placePoint.dotProduct(lineB.getNormalized());
  22.        
  23.         return (lineB.getConstante() - sn) / rn;
  24.     }
  25.    
  26.     this.getDirectionPoint = function() {
  27.         // Vector AB = B-A
  28.         return endPoint.substract(placePoint);
  29.     }
  30.    
  31.     this.getNormalized = function(lineB) {
  32.         return this.getDirectionPoint().perp().normalize();
  33.     }
  34.    
  35.     this.getConstante = function() {
  36.         return placePoint.dotProduct(this.getNormalized());
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment