用c?

ruoxqz4g  于 2023-04-19  发布在  其他
关注(0)|答案(1)|浏览(91)
#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;

}

这就是程序。为什么我们要增加Angular ,这个Angular 与图形有什么关系?我把Angular 的值改为0,波就变成了一条直线。我想知道这个增量会发生什么。

nbysray5

nbysray51#

为什么我们要增加这个Angular ,这个Angular 与图形有什么关系
sine function以一个Angular 作为参数,通常以辐射角为单位。程序以度为单位实现Angular ,因此它被缩放为辐射角,时刻被传递给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 ...

我将Angular 的值更改为0,波变成了直线
sin(0)的结果是0
For the mathematical derivation you might like to have a look here

相关问题