什么可能导致三角函数在不同角度得到完全相同的输出

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

尝试使用 circle() 手动绘制一条线(围绕其绘制一个实心圆)稍微更新其中心变量,这是我图像上的一个坐标。通过将 sin(a) 和 cos(a) 添加到平面的 X 和 Y 来进行更新,其中 'a' 是角度。这样:

        // 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;
        // ............
        // ............

圆圈和更新函数一起循环。这里的圈子叫:


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

预计代码在 sin(60) 和 sin(70) 上具有不同的值,但该行与调试输出保持相同。看看:

//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 

您在输入角度并从中计算 angle*pi/180 的代码部分存在一些错误。对于 60 度,确实 angle*pi/180 是 1.0472。对于您示例输出中的其他角度 - 70、80、90,您显然 而不是 再次计算,并且您仍然使用 1.0472。我不知道为什么 - 您粘贴的示例代码显然不是打印您显示的调试输出的代码(例如,您粘贴的代码中没有任何内容打印 "input angle" 或设置它)。

发现负责缓冲区更新的线程在需要更新输入后立即用 mutex.lock() 锁定缓冲区,最近将绘制输入。当状态被锁定时,我强制它绘制(这意味着它应该更新角度)。因为它不能更新角度解决方案是只有在缓冲区解锁和同时修改后才绘制。