misc/libfreetype/src/tools/cordic.py
author nemo
Sun, 11 Sep 2011 10:46:53 -0400
changeset 5856 ed97138dc414
parent 5172 88f2e05288ba
permissions -rw-r--r--
Should prevent a crasher when drowning while firing
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5172
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     1
# compute arctangent table for CORDIC computations in fttrigon.c
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     2
import sys, math
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     3
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     4
#units  = 64*65536.0   # don't change !!
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     5
units  = 256
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     6
scale  = units/math.pi
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     7
shrink = 1.0
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     8
comma  = ""
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     9
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    10
def calc_val( x ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    11
    global units, shrink
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    12
    angle  = math.atan(x)
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    13
    shrink = shrink * math.cos(angle)
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    14
    return angle/math.pi * units
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    15
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    16
def  print_val( n, x ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    17
    global comma
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    18
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    19
    lo  = int(x)
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    20
    hi  = lo + 1
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    21
    alo = math.atan(lo)
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    22
    ahi = math.atan(hi)
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    23
    ax  = math.atan(2.0**n)
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    24
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    25
    errlo = abs( alo - ax )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    26
    errhi = abs( ahi - ax )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    27
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    28
    if ( errlo < errhi ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    29
      hi = lo
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    30
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    31
    sys.stdout.write( comma + repr( int(hi) ) )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    32
    comma = ", "
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    33
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    34
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    35
print ""
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    36
print "table of arctan( 1/2^n ) for PI = " + repr(units/65536.0) + " units"
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    37
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    38
# compute range of "i"
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    39
r = [-1]
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    40
r = r + range(32)
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    41
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    42
for n in r:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    43
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    44
    if n >= 0:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    45
        x = 1.0/(2.0**n)    # tangent value
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    46
    else:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    47
        x = 2.0**(-n)
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    48
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    49
    angle  = math.atan(x)    # arctangent
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    50
    angle2 = angle*scale     # arctangent in FT_Angle units
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    51
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    52
    # determine which integer value for angle gives the best tangent
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    53
    lo  = int(angle2)
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    54
    hi  = lo + 1
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    55
    tlo = math.tan(lo/scale)
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    56
    thi = math.tan(hi/scale)
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    57
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    58
    errlo = abs( tlo - x )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    59
    errhi = abs( thi - x )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    60
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    61
    angle2 = hi
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    62
    if errlo < errhi:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    63
        angle2 = lo
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    64
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    65
    if angle2 <= 0:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    66
        break
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    67
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    68
    sys.stdout.write( comma + repr( int(angle2) ) )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    69
    comma = ", "
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    70
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    71
    shrink = shrink * math.cos( angle2/scale)
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    72
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    73
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    74
print
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    75
print "shrink factor    = " + repr( shrink )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    76
print "shrink factor 2  = " + repr( shrink * (2.0**32) )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    77
print "expansion factor = " + repr(1/shrink)
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    78
print ""
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    79