import type { ReadonlyVec , Vec } from "@thi.ng/vectors" ; import { cornerBisector } from "@thi.ng/vectors/bisect" ; import { corner2 } from "@thi.ng/vectors/clockwise" ; import { direction } from "@thi.ng/vectors/direction" ; import { dist } from "@thi.ng/vectors/dist" ; import { maddN } from "@thi.ng/vectors/maddn" ; import { mulN } from "@thi.ng/vectors/muln" ; import { perpendicularCW } from "@thi.ng/vectors/perpendicular" ; import { set } from "@thi.ng/vectors/set" ; const buildSegments = ( tangents , t , uniform ) => { const res = [ ] ; for ( let i = 0 , num = tangents . length - 1 ; i < num ; i ++ ) { const [ a , na ] = tangents [ i ] ; const [ b , nb ] = tangents [ i + 1 ] ; const d = uniform ? t : t * dist ( a , b ) ; res . push ( [ a , maddN ( [ ] , na , d , a ) , maddN ( [ ] , nb , - d , b ) , b ] ) ; } return res ; } ; export const closedCubicFromBreakPoints = ( points , t = 1 / 3 , uniform = false ) => { const tangents = [ ] ; for ( let num = points . length , i = num - 1 , j = 0 ; j < num ; i = j , j ++ ) { const a = points [ i ] ; const b = points [ j ] ; const c = points [ ( j + 1 ) % num ] ; const n = mulN ( null , perpendicularCW ( null , cornerBisector ( [ ] , a , b , c ) ) , corner2 ( a , b , c ) ) ; tangents . push ( [ set ( [ ] , b ) , n ] ) ; } tangents . push ( tangents [ 0 ] ) ; return buildSegments ( tangents , t , uniform ) ; } ; export const openCubicFromBreakPoints = ( points , t = 1 / 3 , uniform = false ) => { const tangents = [ [ points [ 0 ] , direction ( [ ] , points [ 0 ] , points [ 1 ] ) ] , ] ; const num = points . length - 1 ; for ( let i = 1 ; i < num ; i ++ ) { const a = points [ i - 1 ] ; const b = points [ i ] ; const c = points [ i + 1 ] ; const n = mulN ( null , perpendicularCW ( null , cornerBisector ( [ ] , a , b , c ) ) , corner2 ( a , b , c ) ) ; tangents . push ( [ set ( [ ] , b ) , n ] ) ; } tangents . push ( [ points [ num ] , direction ( [ ] , points [ num - 1 ] , points [ num ] ) ] ) ; return buildSegments ( tangents , t , uniform ) ; } ;