#include <conio.h>
#include <math.h>
#include <graphics.h>
#include <dos.h>


int main() {

    int gd = DETECT, gm;

    int angle = 0;

    double x, y;

    initgraph(&gd, &gm, "C:\\TC\\BGI");


    line(0, getmaxy() / 2, getmaxx(), getmaxy() / 2);

    /* generate a sine wave */

    for(x = 0; x < getmaxx(); x+=3) {

        /* calculate y value given x */

        y = 50*sin(angle*3.141/180);

        y = getmaxy()/2 - y;

        /* color a pixel at the given position */

        putpixel(x, y, 15);

        delay(100);

        /* increment angle */

        angle+=5;

    }

    getch();

    /* deallocate memory allocated for graphics screen */

    closegraph();

    return 0;

}


这是程序。为什么我们要增加角度以及该角度与图形有何关系?我将angle的值更改为0,该波变成了一条直线。我想知道此增量发生了什么。

最佳答案

我们为什么要增加角度以及该角度与图形的关系


sine function以角度作为参数,通常以辐射为角度。该程序以度为单位实现角度,因此将其传递给sin()时将其缩放为辐射度。

正弦函数是周期性的(在2 * pi或360度后重复出现):

+---------+---------+------------+
|       angle       | sin(angle) |
+---------+---------+            |
| Radiant | Degrees |            |
+---------+---------+------------+
|       0 |       0 |          0 |
+---------+---------+------------+
|  1/2*pi |      90 |          1 |
+---------+---------+------------+
|      pi |     180 |          0 |
+---------+---------+------------+
|  3/2*pi |     270 |         -1 |
+---------+---------+------------+
|    2*pi |     360 |          0 |
+---------+---------+------------+
|  5/2*pi |     450 |          1 |
+---------+---------+------------+
|    3*pi |     540 |          0 |
+---------+---------+------------+
|  7/2*pi |     630 |         -1 |
+---------+---------+------------+
|    4*pi |     720 |          0 |
+---------+---------+------------+
|     ... |     ... |        ... |

and so on ...



  将angle的值更改为0,并且该波变成了一条直线


sin(0)的结果为0

For the mathematical derivation you might like to have a look here

关于c - 使用c绘制正弦波?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45402229/

10-17 02:34