fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
a7d6bcc
GUI
1function math . gcd ( m , n ) 2-- greatest common divisor 3while m ~= 0 do 4m , n =n % m , m 5end 6return n 7end 8 9do 10local min =math . min 11local max =math . max 12 13function math . clamp ( num , min , max ) 14assert ( min <= max , "invalid clamp range" ) 15if num < min then 16num =min 17elseif num > max then 18num =max 19end 20return num 21end 22end 23 24do 25local pow =math . pow 26local floor =math . floor 27 28function math . round ( num ) 29return num + 0.5 - ( num + 0.5 ) % 1 30end 31 32function math . roundplaces ( num , palces ) 33local mult =pow ( 10 , ( palces or 0 )) 34return floor ( num * mult + 0.5 ) / mult 35end 36end 37 38do 39local random =math . random 40 41function math . randomFloat ( min , max ) 42return min + random () * ( max - min ) 43end 44 45require ( "extensions.table" ) 46 47local sum =table . sum 48 49function math . weightedRandom ( tbl ) 50local threshold =random ( 0 , sum ( tbl )) 51local last_choice 52for choice , weight in pairs ( tbl ) do 53threshold =threshold - weight 54if threshold <= 0 then return choice end 55last_choice =choice 56end 57return last_choice 58end 59end 60 61do 62local sin =math . sin 63 64function math . sinlerp ( min , max , t ) 65local h =( max - min ) / 2 66return min + h + sin ( t ) * h 67end 68 69local cos =math . cos 70 71function math . coslerp ( min , max , t ) 72local h =( max - min ) / 2 73return min + h + cos ( t ) * h 74end 75end 76 77function math . lerp ( a , b , t ) 78return ( 1 - t ) * a + t * b 79end 80 81function math . timeunits ( seconds ) 82return { 83years =seconds / 3.154e7 , 84months =seconds / 2.628e6 , 85weeks =seconds / 604800 , 86days =seconds / 86400 , 87hours =seconds / 3600 , 88minutes =seconds / 60 , 89seconds =seconds , 90milliseconds =seconds * 1000 , 91} 92end 93 94do 95local format =string . format 96 97function math . toMinutesSeconds ( num , fmt ) 98local m =num / 60 99local s =num % 60 100return format ( fmt or "%02d:%02d" , m , s ) 101end 102 103function math . toMinSecMS ( num , fmt ) 104local m =num / 60 105local s =num % 60 106local ms =( num % 1 ) * 100 107return format ( fmt or "%02d:%02d:%02d" , m , s , ms % 100 ) 108end 109end