简体   繁体   English

可能导致在不同角度获得完全相同的三角函数输出的原因

[英]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. 尝试使用circle()手动绘制一条线circle()在其周围绘制一个实心圆),稍稍更新其中心变量,该变量是我图像上的坐标。 Update happens by adding the sin(a) and cos(a) to X and Y of the plane, where the 'a' is the angle. 通过将sin(a)和cos(a)添加到平面的X和Y来进行更新,其中'a'是角度。 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. 期望代码在sin(60)和sin(70)上具有不同的值,但该行与调试的输出相同。 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. 您在代码部分中输入了角度并由此计算angle*pi/180时会遇到一些错误。 For 60 degrees, indeed angle*pi/180 is 1.0472. 对于60度, angle*pi/180实际上为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. 对于示例输出中的其他角度-70、80、90,您显然不会再进行计算,并且仍为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. 找出负责缓冲区更新的线程在需要更新输入之后立即使用mutex.lock()锁定缓冲区,该缓冲区将在最近绘制。 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. 由于它无法更新角度,解决方案是仅在缓冲区同时解锁和修改后才绘制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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