Decompose Matrix
copy const deltaTransformPoint = ( matrix, point) => { return { x: point.x * matrix.a + point.y * matrix.c , y: point.x * matrix.b + point.y * matrix.d } } const decomposeMatrix = matrix => { let px = deltaTransformPoint( matrix, { x: 0 , y: 1 } ) let py = deltaTransformPoint( matrix, { x: 1 , y: 0 } ) let skewX = FROM_RADS * Math .atan2 ( px.y , px.x ) - 90 let skewY = FROM_RADS * Math .atan2 ( py.y , py.x ) return { tx: matrix.e , ty: matrix.f , scaleX: Math .sqrt ( matrix.a * matrix.a + matrix.b * matrix.b ) , scaleY: Math .sqrt ( matrix.c * matrix.c + matrix.d * matrix.d ) , skewX: skewX, skewY: skewY, rotation: skewX } }
Get the scale, translation, rotationa and skew values from a matrix.
Great stackoverflow answer from user dave
Iterative Square Root
copy //------------------------------------------------------------------ float function_IterativeSquareRoot ( float x) { // http://en.wikipedia.org/wiki/Methods_of_computing_square_roots // Ancient Babylonian technology functionName = "Iterative (Heron's) Square Root" ; float y = 0.5 ; int n = 6 ; for ( int i= 0 ; i< n; i++ ) { y = ( y + x/ y) / 2.0 ; } return y; }
Was browsing some code by Golan Levin and stumbled on this…
There are some real gems in the repo – might port some stuff from there in the future…
Fibonacci Triangle Golfed
copy // by Arnauld - https://codegolf.stackexchange.com/users/58563/arnauld f= ( n, a= b= 1 , p) => n? '' .padEnd ( p) + a+ `+ ${ b} = ${ b+= a} `+ f( n- 1 , b- a, ( a+ "" ) .length - ~p) : '' console.log ( f( 20 ) )
Try it out…
Great golfed solution to this question at codegolf stackexchange by user Arnauld
Step Between Two Numbers
copy const stepBetweenA = ( min, max, steps) => Array ( steps) .fill ( 0 ) .reduce ( ( prev, curr, i) => { prev.push ( min + ( ( max - min) / ( steps - 1 ) ) * i) return prev } , [ ] ) const stepBetweenB = ( min, max, steps) => { steps -= 1 const diff = ( max - min) / steps const result = [ min] for ( let i = 0 ; i < steps; i++ ) { result.push ( min += diff) } return result } console.log ( 'a' , stepBetweenA( 10 , 110 , 4 ) ) console.log ( 'b' , stepBetweenB( 10 , 110 , 4 ) ) const ITER = 10000 console.time ( 'a' ) for ( let i = 0 ; i < ITER; i++ ) { stepBetweenA( 10 , 110 , 4 ) } console.timeEnd ( 'a' ) console.time ( 'b' ) for ( let i = 0 ; i < ITER; i++ ) { stepBetweenB( 10 , 110 , 4 ) } console.timeEnd ( 'b' )
Try it out…
Two messy implementations for stepping between two numbers… I am not sure it’s possible to make console.time
work well with the snippet zone quick editor – so if you want to see the times… open your console.
Hamming Distance in JavaScript
copy function hamming( a, b) { const leng = a.length let dist = 0 // strings need to be same length if ( leng != b.length ) return - 1 ; a = a.toLowerCase ( ) b = b.toLowerCase ( ) for ( let i = 0 ; i < leng; i++ ) if ( a[ i] !== b[ i] ) dist++ return dist } console.log ( hamming( 'zevan' , 'kevin' ) ) console.log ( hamming( 'joe' , 'joe' ) ) console.log ( hamming( 'john' , 'jake' ) )
Try it out…
The hamming distance between two strings…