import { Vector2 , sub , v2 , normal , dot , distance , set , } from "./v2" ; export type PointEdgeProjection = { distance : number ; similarity : number ; u : number ; projectedPoint : Vector2 ; edgeNormal : Vector2 ; } ; /**
 * Create a pre-made result object for tests.
 */ export function createPointEdgeProjectionResult ( ) { return { distance : 0 , similarity : 0 , u : 0 , projectedPoint : v2 ( ) , edgeNormal : v2 ( ) , } ; } const edgeDelta = v2 ( ) ; const perp = v2 ( ) ; export function projectPointEdge ( point , endpoint1 , endpoint2 , result ) { sub ( edgeDelta , endpoint2 , endpoint1 ) ; if ( edgeDelta . x === 0 && edgeDelta . y === 0 ) { throw new Error ( 'ZeroLengthEdge' ) ; } const u = ( ( point . x - endpoint1 . x ) * edgeDelta . x + ( point . y - endpoint1 . y ) * edgeDelta . y ) / ( edgeDelta . x * edgeDelta . x + edgeDelta . y * edgeDelta . y ) ; result . u = u ; const proj = set ( result . projectedPoint , endpoint1 . x + u * edgeDelta . x , endpoint1 . y + u * edgeDelta . y ) ; result . distance = distance ( proj , point ) ; const edgeNorm = normal ( result . edgeNormal , endpoint1 , endpoint2 ) ; sub ( perp , point , proj ) ; result . similarity = dot ( edgeNorm , perp ) ; }