#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include <iostream>
#include <math.h>
using namespace std;
#define PI 3.14159
int main() {
int width, height,linelength;
width = 800; height = 600; linelength = 160;
int lineNum = 20;
float Angles[20];
initgraph(width, height);
setbkcolor(WHITE);
cleardevice();
float xEnd, yEnd,rotatespeed;
rotatespeed = PI / 360;
for (int i = 0; i < lineNum; i++)//20根针,均分360度
{
Angles[i] = i * 2 * PI / lineNum;
}
BeginBatchDraw();/*这个函数用于开始批量绘图。执行后,任何绘图操作都将暂时不输出到绘图窗口上,直到执行FlushBatchDraw 或 EndBatchDraw 才将之前的绘图输出。*/
while (1)//重复循环
{
setlinestyle(PS_SOLID, 3);//针的宽度为3
setlinecolor(BLACK);//针的颜色为黑色
line(0, height / 2, width / 5, height / 2);
for (int i = 0; i < lineNum; i++)//遍历20根针
{
Angles[i] += rotatespeed;/*这一句非常关键,我们可以想象初始化的Angles[20]里有20根针,各自有初始角度, 在for循环里每画一根针,这根针的角度都会+rotatespeed,相当于一组for循环下来,20根针的角度都更新了,也就相当于这20根针集体转了rotatespeed,然后进入while循环,导致这20根针不断地重复一组组地for循环,也即20根针每集体转rotatespeed,就刷新一下,所以看起来就是不停在转*/
if (Angles[i] == 2 * PI)
{
Angles[i] = Angles[i] - 2 * PI;
}
xEnd = linelength * cos(-Angles[i]) + width / 2;
yEnd = linelength * sin(-Angles[i]) + height / 2;
line(width / 2, height / 2, xEnd, yEnd);
}
setlinecolor(RED);
circle(width / 2, height / 2, 60);
FlushBatchDraw();//解决屏幕闪烁的问题(屏幕闪烁是因为刷新太频繁)
Sleep(80);
cleardevice();
}
_getch();
closegraph();
return 0;
}