Package gameobjects :: Module util
[hide private]
[frames] | no frames]

Source Code for Module gameobjects.util

 1  from math import * 
 2   
3 -def format_number(n, accuracy=6):
4 """Formats a number in a friendly manner 5 (removes trailing zeros and unneccesary point.""" 6 7 fs = "%."+str(accuracy)+"f" 8 str_n = fs%float(n) 9 if '.' in str_n: 10 str_n = str_n.rstrip('0').rstrip('.') 11 if str_n == "-0": 12 str_n = "0" 13 #str_n = str_n.replace("-0", "0") 14 return str_n
15 16
17 -def lerp(a, b, i):
18 """Linear enterpolate from a to b.""" 19 return a+(b-a)*i
20 21
22 -def range2d(range_x, range_y):
23 24 """Creates a 2D range.""" 25 26 range_x = list(range_x) 27 return [ (x, y) for y in range_y for x in range_x ]
28 29
30 -def xrange2d(range_x, range_y):
31 32 """Iterates over a 2D range.""" 33 34 range_x = list(range_x) 35 for y in range_y: 36 for x in range_x: 37 yield (x, y)
38 39
40 -def saturate(value, low, high):
41 return min(max(value, low), high)
42 43
44 -def is_power_of_2(n):
45 """Returns True if a value is a power of 2.""" 46 return log(n, 2) % 1.0 == 0.0
47 48
49 -def next_power_of_2(n):
50 """Returns the next power of 2 that is >= n""" 51 return int(2 ** ceil(log(n, 2)))
52 53 if __name__ == "__main__": 54 55 print list( xrange2d(xrange(3), xrange(3)) ) 56 print range2d(xrange(3), xrange(3)) 57 print is_power_of_2(7) 58 print is_power_of_2(8) 59 print is_power_of_2(9) 60 61 print next_power_of_2(7) 62