c - 用c画正弦波?

标签 c bgi

#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;

}

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

最佳答案

Why are we incrementing the angle and how this angle is relevant to graph

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

changed the value of angle to 0 and the wave became a straight line

sin(0) 的结果是 0

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

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

相关文章:

c++ - 错误 : ISO C++ forbids conversion from string constant to char*

c - 图形头无法编译?

c++ - 如何解决在开发 C++ 中未定义的对 readimagefile 的引用错误

c - getch() 将回车等同于 esc 键

c - 在c中使用graphics.h中的outtextxy实现退格键

c - 如何使用原始套接字在 C 中接收 ICMP 请求

c 指针数组

c - C语言链表去重

c - 一维数组和二维数组(矩阵)之间是否存在效率差异?

使用预处理器在 C 中计算两个数组的笛卡尔积