基于VS2019 EasyX插件 C/C++
旋转蛇
#include<conio.h>
#include<graphics.h>
#include<iostream>
using namespace std;
int main()
{
float Pi = 3.14159;
initgraph(800, 600);
setbkcolor(RGB(128, 128, 128));//设置背景颜色为灰色
cleardevice();//以背景颜色清空画布
int centerX = 150;//圆心
int centerY = 150;
int radius = 100;//圆半径
setlinecolor(RED);
//circle(centerX, centerY, radius);
int left, right, top, bottom;
left = centerX - radius;//圆外切矩形左上角x坐标
top = centerY - radius;//圆外切矩形左上角y坐标
right = centerX + radius;//圆外切矩形右下角y坐标
bottom = centerY + radius;//圆外切矩形右下角y坐标
float offset;
float totalOffset = 0;//不同半径之间的角度偏移量
//void solidpie(
// int left,//圆外切矩形左上角x坐标
// int top,//圆外切矩形左上角y坐标
// int right,//圆外切矩形右下角y坐标
// int bottom,//圆外切矩形右下角y坐标
// double stangle,//扇形的起始角的弧度
// double endangle//扇形的终止角的弧度
//);
while (1)
{
for (centerX = 100; centerX < 800; centerX = centerX + 200)
{
for (centerY = 100; centerY < 600; centerY = centerY + 200)
{
for (int radius = 100; radius > 0; radius = radius - 25)
{
left = centerX - radius;//圆外切矩形左上角x坐标
top = centerY - radius;//圆外切矩形左上角y坐标
right = centerX + radius;//圆外切矩形右下角y坐标
bottom = centerY + radius;//圆外切矩形右下角y坐标
for (int j = 0; j < 20; j++)
{
float h = rand() % 180;//使用随机数
COLORREF color1 = HSVtoRGB(h, 0.9, 0.8);//利用HSV函数来随机生成两种颜色
COLORREF color2 = HSVtoRGB(h + 180, 0.9, 0.8);
offset = j * Pi / 10 + totalOffset;
setfillcolor(color1);
solidpie(left, top, right, bottom, offset, 2 * Pi / 60 + offset);
setfillcolor(RGB(255, 255, 255));
solidpie(left, top, right, bottom, 2 * Pi / 60 + offset, 3 * Pi / 60 + offset);
setfillcolor(color2);
solidpie(left, top, right, bottom, 3 * Pi / 60 + offset, 5 * Pi / 60 + offset);
setfillcolor(RGB(0, 0, 0));
solidpie(left, top, right, bottom, 5 * Pi / 60 + offset, 6 * Pi / 60 + offset);
}
totalOffset = totalOffset + Pi / 20;
}
}
}
_getch();
}
closegraph();
return 0;
}
注释
1、solidpie()
该函数使用当前填充样式绘制无外框的填充扇形
void solidpie(
int left,//圆外切矩形左上角x坐标
int top,//圆外切矩形左上角y坐标
int right,//圆外切矩形右下角y坐标
int bottom,//圆外切矩形右下角y坐标
double stangle,//扇形的起始角的弧度
double endangle//扇形的终止角的弧度
);
2、setbkcolor()
这个函数用于设置当前设备绘图背景色
void setbkcolor(COLORREF color);
eg:设置背景颜色为灰色
setbkcolor(RGB(128, 128, 128));
3、setfillcolor()
这个函数用于设置当前设备填充颜色
void setfillcolor(COLORREF color);
eg:
setfillcolor(RGB(255, 255, 255));
setfillcolor(color1);
4、HSVtoRGB
该函数用于转换 HSV 颜色为 RGB 颜色
COLORREF HSVtoRGB(
float H,
float S,
float V
);
H
原 HSV 颜色模型的 Hue(色相) 分量,0 <= H < 360。
S
原 HSV 颜色模型的 Saturation(饱和度) 分量,0 <= S <= 1。
V
原 HSV 颜色模型的 Value(明度) 分量,0 <= V <= 1。
eg:
COLORREF color1 = HSVtoRGB(h, 0.9, 0.8);
COLORREF color2 = HSVtoRGB(h + 180, 0.9, 0.8);
成品截图