/src/freetype/src/tools/cordic.py

https://bitbucket.org/cabalistic/ogredeps/ · Python · 79 lines · 53 code · 22 blank · 4 comment · 6 complexity · 0c4f1d0644b75e072e736515d883abcb MD5 · raw file

  1. # compute arctangent table for CORDIC computations in fttrigon.c
  2. import sys, math
  3. #units = 64*65536.0 # don't change !!
  4. units = 256
  5. scale = units/math.pi
  6. shrink = 1.0
  7. comma = ""
  8. def calc_val( x ):
  9. global units, shrink
  10. angle = math.atan(x)
  11. shrink = shrink * math.cos(angle)
  12. return angle/math.pi * units
  13. def print_val( n, x ):
  14. global comma
  15. lo = int(x)
  16. hi = lo + 1
  17. alo = math.atan(lo)
  18. ahi = math.atan(hi)
  19. ax = math.atan(2.0**n)
  20. errlo = abs( alo - ax )
  21. errhi = abs( ahi - ax )
  22. if ( errlo < errhi ):
  23. hi = lo
  24. sys.stdout.write( comma + repr( int(hi) ) )
  25. comma = ", "
  26. print ""
  27. print "table of arctan( 1/2^n ) for PI = " + repr(units/65536.0) + " units"
  28. # compute range of "i"
  29. r = [-1]
  30. r = r + range(32)
  31. for n in r:
  32. if n >= 0:
  33. x = 1.0/(2.0**n) # tangent value
  34. else:
  35. x = 2.0**(-n)
  36. angle = math.atan(x) # arctangent
  37. angle2 = angle*scale # arctangent in FT_Angle units
  38. # determine which integer value for angle gives the best tangent
  39. lo = int(angle2)
  40. hi = lo + 1
  41. tlo = math.tan(lo/scale)
  42. thi = math.tan(hi/scale)
  43. errlo = abs( tlo - x )
  44. errhi = abs( thi - x )
  45. angle2 = hi
  46. if errlo < errhi:
  47. angle2 = lo
  48. if angle2 <= 0:
  49. break
  50. sys.stdout.write( comma + repr( int(angle2) ) )
  51. comma = ", "
  52. shrink = shrink * math.cos( angle2/scale)
  53. print
  54. print "shrink factor = " + repr( shrink )
  55. print "shrink factor 2 = " + repr( shrink * (2.0**32) )
  56. print "expansion factor = " + repr(1/shrink)
  57. print ""