Search code examples
c++opencvcmath

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 

Solution

  • 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.