概述
原项目是旋转蛇错觉图案,但我这里修改了一下,只随机生成同心圆及其颜色。
实现效果和涉及的知识
初步接触for循环
文件结构
具体代码
FQF_RotCir.cpp
// FQF_RotCir.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream> // C++ STL 输入输出库
#include <graphics.h> // 绘图函数库,非cpp STL,是TurboC扩展库(EasyX)
#include <conio.h> // console input/output的简写,非cpp STL,定义了通过控制台进行输入输出的函数,如getch()
#include "data.h"
#include "RotPie.h"
using namespace std;
int main()
{
RotPie rotpie;
initgraph(GraWid, GraHei);
setbkcolor(BkColor);
cleardevice();
for (int n = 0; n < 4; n++)
{
for (int m = 0; m < 6; m++)
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 9; j++)
{
rotpie.Cal_RotPie();
rotpie.Plot_RotPie();
rotpie.Updata_Ang_1();
}
rotpie.Updata_Circle_R();
rotpie.Updata_Ang();
int a = 1;
}
rotpie.Updata_Circle_X();
rotpie.Updata_Color();
Sleep(500);
}
rotpie.Updata_Circle_Y();
}
_getch();
closegraph();
return 0;
}
RotPie.h
#pragma once
class RotPie
{
private:
float Pie_left, Pie_up, Pie_right, Pie_down;
public:
RotPie();
~RotPie();
void Cal_RotPie();
void Plot_RotPie();
void Updata_Ang();
void Updata_Ang_1();
void Updata_Circle_R();
void Updata_Circle_X();
void Updata_Circle_Y();
void Updata_Color();
};
RotPie.cpp
#include "RotPie.h"
#include "data.h"
#include <graphics.h>
#include <ctime> // time()
#include <stdlib.h> // rand()和srand()
RotPie::RotPie()
{
Pie_left = 0;
Pie_up = 0;
Pie_right = GraWid;
Pie_down = GraHei;
}
RotPie::~RotPie()
{
}
void RotPie::Cal_RotPie()
{
Pie_left = CircleCenterX- CircleR;
Pie_up = CircleCenterY - CircleR;
Pie_right = CircleCenterX + CircleR;
Pie_down = CircleCenterY + CircleR;
}
void RotPie::Plot_RotPie()
{
setfillcolor(PieColor1);
solidpie(Pie_left, Pie_up, Pie_right, Pie_down, Angle+Angle_1, Angle + Angle_1 +Angle_Offset_1);
setfillcolor(PieColor2);
solidpie(Pie_left, Pie_up, Pie_right, Pie_down, Angle + Angle_1 + Angle_Offset_1, Angle + Angle_1 + 2*Angle_Offset_1);
setfillcolor(PieColor3);
solidpie(Pie_left, Pie_up, Pie_right, Pie_down, Angle + Angle_1 + 2*Angle_Offset_1, Angle + Angle_1 + 3*Angle_Offset_1);
}
void RotPie::Updata_Ang()
{
Angle = Angle + Angle_Offset;
if (Angle >= 2 * pi)
{
Angle = 0;
}
}
void RotPie::Updata_Ang_1()
{
Angle_1 = Angle_1 + Angle_Offset_1 * 3;
if (Angle_1 >= 2 * pi)
{
Angle_1 = 0;
}
}
void RotPie::Updata_Circle_R()
{
CircleR = CircleR - R_Offset;
if (CircleR <= 0)
{
CircleR = 50;
}
}
void RotPie::Updata_Circle_X()
{
CircleCenterX = CircleCenterX + CircleCenterX_Offset;
if (CircleCenterX >= GraWid)
{
CircleCenterX = 50;
}
}
void RotPie::Updata_Circle_Y()
{
CircleCenterY = CircleCenterY + CircleCenterY_Offset;
if (CircleCenterY >= GraHei)
{
CircleCenterY = 50;
}
}
void RotPie::Updata_Color()
{
srand(time(0));
BaseColor = rand() % 3 * 60 + 50;
srand(time(0));
int gap= rand() % 5 * 50;
PieColor1 = RGB(BaseColor, BaseColor, 0 + gap);
PieColor2 = RGB(0 + gap, BaseColor -50, BaseColor - 50);
PieColor3 = RGB(BaseColor + 50, 0 + gap, BaseColor + 50);
}
data.h
#pragma once
#include <graphics.h>
extern float GraWid, GraHei;
extern float pi;
extern float CircleCenterX, CircleCenterY;
extern float CircleR;
extern float Angle, Angle_1;
extern float CircleCenterX_Offset, CircleCenterY_Offset;
extern float R_Offset;
extern float Angle_Offset, Angle_Offset_1;
extern int BaseColor;
extern COLORREF BkColor, PieColor1, PieColor2, PieColor3;
data.cpp
#include "data.h"
float GraWid = 600, GraHei = 400;
float pi = 3.1415;
float CircleCenterX = 50, CircleCenterY = 50;
float CircleR = 50;
float Angle = 0, Angle_1 = 0;
float CircleCenterX_Offset = 100, CircleCenterY_Offset = 100;
float R_Offset = 10;
float Angle_Offset = pi / 20, Angle_Offset_1 = pi / 9;
int BaseColor = 0;
COLORREF BkColor = RGB(0, 0, 0) , PieColor1 = RGB(0, BaseColor+100, BaseColor + 100), PieColor2 = RGB(BaseColor + 170, 0, BaseColor + 170), PieColor3 = RGB(BaseColor + 240, BaseColor + 240, 0);