A Flash Developer Resource Site

Results 1 to 13 of 13

Thread: sin() cos() lookup tables how

  1. #1
    Senior Member
    Join Date
    Nov 2003
    Posts
    524

    sin() cos() lookup tables how

    Hi,
    Ok I am stumped. I have read heaps of tuts for this in C/C++. But I
    must be thick as a brick because I still can't get it all working.

    This is the script as it stands but I want to optimize by using lookup tables. (or at least see if I can get some speed improvements).
    Code:
    //variable holding increment for a y rotation (radians).
    var angle_inc = 0.035;
    //variable to add increments to
    var angle_y=0;
    //final sin of angle_y
    var sinY=0;
    //increment angle_y
    angle_y+=angle_inc; (can also be angle_y-=angle_inc;) 
    
    for(j=0;j=aVeryBigNumber;j++){
        siny=Math.sin (angle_y);
        //use siny................
    }
    How do I replace this with lookUp tables. ie:
    Code:
    sinTbl=new Array();
    cosTbl=new Array();
    //increment angle_y
    angle_y+=angle_inc; (can also be angle_y-=angle_inc;)  
    
    //use tables 
    for(j=0;j=aVeryBigNumber;j++){
        siny=sinTbl[angle_y];
        //use siny................
    }
    How do I fill the arrays with the correct increments?
    How do I know where to index into the array?
    How do I stop indexing beyond the array limits?
    angle_inc can also be -angle_inc.

    Shipstern

  2. #2
    Junior Member
    Join Date
    Jul 2003
    Location
    London, UK
    Posts
    18
    Have you got C code to do the job, but just having trouble translating it into AS?

    If so, post the C code as a text file and I'll try and help you do it.

  3. #3
    Senior Member
    Join Date
    Nov 2003
    Location
    Nevada
    Posts
    138
    //try something like this, using lookup angle of 89.2 degreees, for example.


    var inputAngle=89.2;

    var tableSize=720;
    var splineRatio=360/tableSize;
    var sine = new Array();

    for (i=0;i<tableSize;i++) {
    sine.push(Math.sin(i*2*Math.PI*splineRatio/360));
    }

    output =sine[Math.floor(angle/splineRatio)];

    //outputs: 0.999847695156391

  4. #4
    Senior Member
    Join Date
    Nov 2003
    Location
    Nevada
    Posts
    138

    correction..

    correction, i did not realize you had to deal with + and negative angle inputs.... here is the correct code for that sitch.

    +++++++++++++++++++++++++++++++++++++++++++++++

    var inputAngle=-45; // for example...
    var tableSize=720; //size of your LUT

    var outputSine;
    var outputCosine;

    var splineRatio=360/tableSize;
    var sine = new Array();
    var cosine=new Array();
    angle=Math.abs(inputAngle);

    if(inputAngle<0) {angleSign=-1;} else {angleSign=1;}


    for (i=0;i<tableSize;i++) {
    sine.push(Math.sin(i*2*Math.PI*splineRatio/360));
    cosine.push(Math.cos(i*2*Math.PI*splineRatio/360));
    }

    outputSine=angleSign*sine[Math.floor(angle/splineRatio)];
    outputCosine=cosine[Math.floor(angle/splineRatio)];

    trace("sine for angle="+inputAngle+" deg is "+outputSine);
    trace("cosine for angle="+inputAngle+" deg is "+outputCosine);

  5. #5
    Senior Member
    Join Date
    Nov 2003
    Posts
    524
    Hi brintcorb,
    Thanks very much. It is just what I needed. So simple I feel really dumb even asking the question!!
    Shipstern

  6. #6
    Senior Member
    Join Date
    Jan 2002
    Posts
    368
    just for additional info, it has pretty much been demonstrated that lookup tables are far less efficient than just using the math function in Flash. The math functions in Flash are pretty fast and the array functions are relatively slow.

  7. #7
    Originally posted by bit-101
    just for additional info, it has pretty much been demonstrated that lookup tables are far less efficient than just using the math function in Flash. The math functions in Flash are pretty fast and the array functions are relatively slow.
    really? and array reference (a[3]) is slower than a sin funxtion call (sin(2.423))?

    Got any links for that?

  8. #8
    Junior Member
    Join Date
    Jul 2003
    Location
    London, UK
    Posts
    18
    Just to jump in here ... probably depends on just how calculations you make, and how complex each one is ... just test both for your application.

  9. #9
    Senior Member
    Join Date
    Jan 2002
    Posts
    368
    there are many factors. if you are actually using trig functions on degrees, and only whole number degrees, you can make a table like so:

    Code:
    table = new Array();
    for (i=0; i<10000; i++) {
    	table[i] = Math.sin(i*Math.PI/180);
    }
    that covers all degrees from 0 to 10000. Note that you are pre-converting from degrees radians, which is what the trig functions require.

    you can time how long it takes to access all of them like this:

    Code:
    start = getTimer();
    for (i=0; i<10000; i++) {
    	dummyVar = table[i];
    }
    trace(getTimer()-start);
    on my pc it comes in at 119 ms.

    do do similar with trig functions would be like this:

    Code:
    start = getTimer();
    for (i=0; i<10000; i++) {
    	dummyVar = Math.cos(i*Math.PI/180);
    }
    trace(getTimer()-start);
    on my pc, that comes in at 169 ms. slightly longer. Note that you have to do the degree-to-radian conversion on the fly, though that doesn't seem to take much time.

    So in one sense, a lookup table could be slightly quicker. But how often do you actually use degrees and only whole numbers in trig? In most cases you are getting angles by calculating distances and using atan2 for example. so you have to factor in converting from radians to degrees and rounding off. Also, realize you are losing a degree of accuracy, which may or may not cause problems. All in all, for most practical uses, it is quicker and easier to use Math, from my own experience.
    Last edited by bit-101; 01-24-2004 at 05:16 PM.

  10. #10
    Senior Member
    Join Date
    Nov 2003
    Posts
    524
    Hi everyone.
    All your input is great!!
    I have a question for you all.

    Why is this:
    Code:
     
    var rad=Math.PI/180;
    start = getTimer();
    for (i=0; i<10000; i++) {
    	dummyVar = Math.cos(i*rad);
    }
    trace(getTimer()-start);
    slower than this.(on my machine anyway)
    Code:
     
    start = getTimer();
    for (i=0; i<10000; i++) {
    	dummyVar = Math.cos(i*Math.PI/180);
    }
    trace(getTimer()-start);
    I would have thought that doing the radian conversion outside of the loop would have been faster. Does anyone know why. What is the Flash player doing here?????
    Shipstern

  11. #11
    Senior Member
    Join Date
    Jan 2002
    Posts
    368
    Math.PI is a constant. it is always 3.14....
    180 is a constant. it is always 180.
    when Flash compiles your movie, it sees Math.PI/180 and calculates it and inserts that number into the swf. the calculation is only done once at compile time, not during the running of the swf.

    on the other hand, rad is a variable. it could be anything. so Flash keeps it as a variable in the swf. and it must be evaluated each time it is used.

  12. #12
    Senior Member
    Join Date
    Nov 2003
    Posts
    524
    Hi bit-101,
    Can you declare a variable as a constant in actionscript like in other languages??? .
    Shipstern

  13. #13

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center