简体   繁体   中英

What might cause to get exactly the same output of trigonometric functions in different angles

Try to manually draw a line using circle() (draws a filled circle around a its) slightly updating its center variable which is a coordinate on my image. Update happens by adding the sin(a) and cos(a) to X and Y of the plane, where the 'a' is the angle. This way:

        // This is a multi threaded application.
        // part of another function where i update the 'angle'variable
        // ............
        if (buffer.modified())  // If buffer is modified
        {
            for (int k = 0; k < PB; k++)
            {
                if (buffer.data[k]>0)
                {
                    size=buffer.data[k];
                    angle = k;
                    break;
                }
            }
            buffer.unmodify();                          // Disable flag
            draw_line( size, angle);
        }
        // ............
        // ............


        //The draw_line() function in an infinite loop

        // ............
        // circle() function goes here
        // ............
        //update coordinates
        x_coord += sin(angle*pi/180);
        y_coord += cos(angle*pi/180);

        //update circle()'s center Point
        image.start.x = x_coord;
        image.start.y = y_coord;

        //show the results
        cout<<"
              cos("<<angle*pi/180<<")="<<cos(angle*pi/180)<<"
              sin("<<angle*pi/180<<")="<<sin(angle*pi/180)<<endl;
        // ............
        // ............

The circle and the update functions are looped together. Here is the circle called:


        circle(bckg, image.start, 1, Scalar( color[0],color[1],color[2] ), FILLED,LINE_8 );

It was expected the code to have different values on sin(60) and sin(70) but the line stays the same as the debugged output. Check it out:

//THE OUTPUT

input angle: 30
cos(0.523599)=0.866025    sin(0.523599)=0.5

input angle: 60
cos(1.0472)=0.5    sin(1.0472)=0.866025

input angle: 70
cos(1.0472)=0.5    sin(1.0472)=0.866025


input angle: 80
cos(1.0472)=0.5    sin(1.0472)=0.866025

input angle: 90
cos(1.0472)=0.5    sin(1.0472)=0.866025 

You have some bug in the part of the code where you input the angle and calculate angle*pi/180 from it. For 60 degrees, indeed angle*pi/180 is 1.0472. For the other angles in you example output - 70, 80, 90, you clearly are not calculating this again, and you remain with 1.0472. I don't know why - the example code you pasted is obviously not the code which prints the debugging output you showed (eg, nothing in the code you pasted prints "input angle" or sets it).

Figured out that the thread which is responsible for the buffer update locks the buffer with mutex.lock() right after it needs to update the input, which lately would be drawn. While the state is locked I force it to draw (this means it should update angle). Since it can not update the angle The solution was to draw only after the buffer gets unlocked and modified at the same time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM