10 - Adding the Walls/script.js
//returns with the closest point on a line segment to a given point

returns with the closest point on a line segment to a given point
returns with the closest point on a line segment to a given point
function closestPointBW(b1, w1){
    let ballToWallStart = w1.start.subtr(b1.pos);
    if(Vector.dot(w1.wallUnit(), ballToWallStart) > 0){
        return w1.start;
    }
    let wallEndToBall = b1.pos.subtr(w1.end);
    if(Vector.dot(w1.wallUnit(), wallEndToBall) > 0){
        return w1.end;
    }
    let closestDist = Vector.dot(w1.wallUnit(), ballToWallStart);
    let closestVect = w1.wallUnit().mult(closestDist);
    return w1.start.subtr(closestVect);
}
function closestPointBW ( b1 , w1 ) { let ballToWallStart = w1 . start . subtr ( b1 . pos ) ; if ( Vector . dot ( w1 . wallUnit ( ) , ballToWallStart ) > 0 ) { return w1 . start ; } let wallEndToBall = b1 . pos . subtr ( w1 . end ) ; if ( Vector . dot ( w1 . wallUnit ( ) , wallEndToBall ) > 0 ) { return w1 . end ; } let closestDist = Vector . dot ( w1 . wallUnit ( ) , ballToWallStart ) ; let closestVect = w1 . wallUnit ( ) . mult ( closestDist ) ; return w1 . start . subtr ( closestVect ) ; }
returns with the closest point on a line segment to a given point
returns with the closest point on a line segment to a given point