import { Point2D } from "./types/sol" ; export const add = ( [ x1 , y1 ] , [ x2 , y2 ] ) : Point2D => [ x1 + x2 , y1 + y2 , ] ; export const subtract = ( [ x1 , y1 ] , [ x2 , y2 ] ) : Point2D => [ x1 - x2 , y1 - y2 , ] ; export const magnitude = ( [ x , y ] ) : number => Math . sqrt ( x ** 2 + y ** 2 ) ; export const distance = ( a , b ) : number => magnitude ( subtract ( a , b ) ) ; export const rotate = ( [ x , y ] , a ) : Point2D => [ x * Math . cos ( a ) - y * Math . sin ( a ) , x * Math . sin ( a ) + y * Math . cos ( a ) , ] ; export const normalise = ( p ) : Point2D => { const m = magnitude ( p ) ; return [ p [ 0 ] / m , p [ 1 ] / m ] ; } ; export const scale = ( [ x , y ] , scale ) : Point2D => [ scale * x , scale * y , ] ; export const polarToCartesian = ( [ x , y ] , radius , angle ) : Point2D => [ x + radius * Math . cos ( angle ) , y + radius * Math . sin ( angle ) ] ; export const pointAlong = ( a , b , proportion = 0.5 ) : Point2D => { return add ( a , scale ( subtract ( b , a ) , proportion ) ) ; } ; export const dot = ( [ x1 , y1 ] , [ x2 , y2 ] ) : number => x1 * x2 + y1 * y2 ; export default { add , subtract , magnitude , rotate , normalise , scale , polarToCartesian , pointAlong , dot , distance , } ;