gentable-atan2.py 688 B

1234567891011121314151617181920212223242526272829303132
  1. import math
  2. # Size of the LUT
  3. SIZE = 81
  4. # Center of the circle
  5. CX = 40
  6. CY = 40
  7. # Generate LUT
  8. lut = [[0 for _ in range(SIZE)] for _ in range(SIZE)]
  9. for i in range(SIZE):
  10. for j in range(SIZE):
  11. x = i - CX
  12. y = j - CY
  13. angle = int(math.degrees(math.atan2(y, x)) * 256 / 360)
  14. if angle < 0:
  15. angle += 256
  16. lut[i][j] = angle
  17. # Output C code
  18. print("const int16_t atan2_lut[{}][{}] = {{".format(SIZE, SIZE))
  19. for i in range(SIZE):
  20. print(" {", end="")
  21. for j in range(SIZE):
  22. print("{:5},".format(lut[i][j]), end="")
  23. if j % 10 == 9:
  24. print()
  25. print(" ", end="")
  26. print("},")
  27. print("};")