Path: blob/main/plugins/default-human-emulator/curveLength.ts
1030 views
import IPoint from '@secret-agent/interfaces/IPoint';12// Legendre-Gauss abscissae with n=24 (x_i values, defined at i=n as the roots of the nth order Legendre polynomial Pn(x))3const Tvalues = [4-0.0640568928626056260850430826247450385909,50.0640568928626056260850430826247450385909,6-0.1911188674736163091586398207570696318404,70.1911188674736163091586398207570696318404,8-0.3150426796961633743867932913198102407864,90.3150426796961633743867932913198102407864,10-0.4337935076260451384870842319133497124524,110.4337935076260451384870842319133497124524,12-0.5454214713888395356583756172183723700107,130.5454214713888395356583756172183723700107,14-0.6480936519369755692524957869107476266696,150.6480936519369755692524957869107476266696,16-0.7401241915785543642438281030999784255232,170.7401241915785543642438281030999784255232,18-0.8200019859739029219539498726697452080761,190.8200019859739029219539498726697452080761,20-0.8864155270044010342131543419821967550873,210.8864155270044010342131543419821967550873,22-0.9382745520027327585236490017087214496548,230.9382745520027327585236490017087214496548,24-0.9747285559713094981983919930081690617411,250.9747285559713094981983919930081690617411,26-0.9951872199970213601799974097007368118745,270.9951872199970213601799974097007368118745,28];2930// Legendre-Gauss weights with n=24 (w_i values, defined by a function linked to in the Bezier primer article)31const Cvalues = [320.1279381953467521569740561652246953718517,330.1279381953467521569740561652246953718517,340.1258374563468282961213753825111836887264,350.1258374563468282961213753825111836887264,360.121670472927803391204463153476262425607,370.121670472927803391204463153476262425607,380.1155056680537256013533444839067835598622,390.1155056680537256013533444839067835598622,400.1074442701159656347825773424466062227946,410.1074442701159656347825773424466062227946,420.0976186521041138882698806644642471544279,430.0976186521041138882698806644642471544279,440.086190161531953275917185202983742667185,450.086190161531953275917185202983742667185,460.0733464814110803057340336152531165181193,470.0733464814110803057340336152531165181193,480.0592985849154367807463677585001085845412,490.0592985849154367807463677585001085845412,500.0442774388174198061686027482113382288593,510.0442774388174198061686027482113382288593,520.0285313886289336631813078159518782864491,530.0285313886289336631813078159518782864491,540.0123412297999871995468056670700372915759,550.0123412297999871995468056670700372915759,56];5758export default function curveLength(derivativePoint: (t: number) => IPoint) {59const z = 0.5;6061let sum = 0;62for (let i = 0; i < Tvalues.length; i += 1) {63const t = z * Tvalues[i] + z;64const { x, y } = derivativePoint(t);65const arc = Math.sqrt(x * x + y * y);6667sum += Cvalues[i] * arc;68}69return z * sum;70}717273