[TOC]
1.绘制扇形 函数solidpie(left,top,right,bottom,stangle,endangle)可以绘制无边框的填充扇形。
其中,stangle是扇形的起初角,endangle是终止角,角度单位为弧度。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include<graphics.h> #include<conio.h> #include<stdio.h> int main() { float PI = 3.14159;//圆周率 initgraph(600, 600);//打开一个窗口 int centerX = 300;//圆心坐标 int centerY = 300; int radius = 200;//圆半径 circle(centerX, centerY, radius);//画出圆边框 int left = centerX - radius; int top = centerY - radius; int right = centerX + radius; int bottom = centerY + radius; solidpie(left, top, right, bottom, PI / 6, PI / 3);//画填充扇形,角度为PI/6到PI/3 _getch(); closegraph(); return 0; }
2.RGB颜色模型 EasyX可以设置绘图颜色(要先设置颜色,再绘图)
1 2 3 4 setbkcolor(WHITE);//设置背景颜色为白色 setlinecolor(RED);//设置线条为红色 setfillcolor(GREEN);//设置填充颜色为绿色 cleardevice();//以背景颜色清空画布
3.绘制一个扇形单元 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include<graphics.h> #include<conio.h> #include<stdio.h> int main() { float PI = 3.14159;//圆周率 initgraph(600, 600);//打开一个窗口 setbkcolor(RGB(128, 128, 128));//设置背景颜色为灰色 cleardevice();//以背景颜色清空画布 int centerX = 300;//圆心坐标 int centerY = 300; int radius = 200;//圆半径 circle(centerX, centerY, radius);//画出圆边框 int left = centerX - radius; int top = centerY - radius; int right = centerX + radius; int bottom = centerY + radius; int i; float offset; for (i = 0; i < 20; i++) { offset = i * PI / 10;//各组扇形之间的偏移的角度 setfillcolor(GREEN); solidpie(left, top, right, bottom,offset, 2 * PI / 60 + offset);//画填充扇形 setfillcolor(WHITE); solidpie(left, top, right, bottom, 2 * PI / 60 + offset, 3 * PI / 60 + offset);//画填充扇形 setfillcolor(RED); solidpie(left, top, right, bottom, 3 * PI / 60 + offset, 5 * PI / 60 + offset);//画填充扇形 setfillcolor(BLUE); solidpie(left, top, right, bottom, 5 * PI / 60 + offset, 6 * PI / 60 + offset);//画填充扇形 } _getch(); closegraph(); return 0; }
4.循环嵌套 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 #include<graphics.h> #include<conio.h> #include<stdio.h> int main() { float PI = 3.14159;//圆周率 initgraph(600, 600);//打开一个窗口 setbkcolor(RGB(128, 128, 128));//设置背景颜色为灰色 cleardevice();//以背景颜色清空画布 int centerX = 300;//圆心坐标 int centerY = 300; int radius = 200;//圆半径 circle(centerX, centerY, radius);//画出圆边框 int i; float offset;//同一半径各组扇形之间的偏移的角度 float totaloffset = 0;//不同半径之间的角度偏移量 for (radius = 200; radius > 0; radius = radius - 50) {//半径从小到大绘制 int left = centerX - radius; int top = centerY - radius; int right = centerX + radius; int bottom = centerY + radius; for (i = 0; i < 20; i++) {//旋转一周,绘制扇形区域 offset = i * PI / 10 + totaloffset; setfillcolor(GREEN); solidpie(left, top, right, bottom, offset, 2 * PI / 60 + offset);//画填充扇形 setfillcolor(WHITE); solidpie(left, top, right, bottom, 2 * PI / 60 + offset, 3 * PI / 60 + offset);//画填充扇形 setfillcolor(RED); solidpie(left, top, right, bottom, 3 * PI / 60 + offset, 5 * PI / 60 + offset);//画填充扇形 setfillcolor(BLUE); solidpie(left, top, right, bottom, 5 * PI / 60 + offset, 6 * PI / 60 + offset);//画填充扇形 } totaloffset = totaloffset + PI / 20; } _getch(); closegraph(); return 0; }
5.多个圆盘 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 for (centerX - 200; centerX < 1200; centerX = centerX + 400) { for (centerY = 200; centerY < 800; centerY = centerY + 400) { for (radius = 200; radius > 0; radius = radius - 50) {//半径从小到大绘制 int left = centerX - radius; int top = centerY - radius; int right = centerX + radius; int bottom = centerY + radius; for (i = 0; i < 20; i++) {//旋转一周,绘制扇形区域 offset = i * PI / 10 + totaloffset; setfillcolor(GREEN); solidpie(left, top, right, bottom, offset, 2 * PI / 60 + offset);//画填充扇形 setfillcolor(WHITE); solidpie(left, top, right, bottom, 2 * PI / 60 + offset, 3 * PI / 60 + offset);//画填充扇形 setfillcolor(RED); solidpie(left, top, right, bottom, 3 * PI / 60 + offset, 5 * PI / 60 + offset);//画填充扇形 setfillcolor(BLUE); solidpie(left, top, right, bottom, 5 * PI / 60 + offset, 6 * PI / 60 + offset);//画填充扇形 } totaloffset = totaloffset + PI / 20; } } }
6.HSV颜色模型
在HSV模型中,颜色是由色度 (Hue),饱和度 (Saturation),明度 (Value)共同组成
COLORREFN color;//定义颜色变量
color = HSVtoRGB(1,1,1);//HSV设置的颜色
setfillcolor(color);//设置填充颜色为color
7.全部代码 使用srand(time(0))表示用当前时间对随机函数初始化,必须添加头文件#include<time.h>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 #include<graphics.h> #include<conio.h> #include<stdio.h> #include<time.h> int main() { float PI = 3.14159;//圆周率 initgraph(1600, 800);//打开一个窗口 setbkcolor(RGB(128, 128, 128));//设置背景颜色为灰色 cleardevice();//以背景颜色清空画布 srand(time(0));//随机种子函数 int centerX = 300;//圆心坐标 int centerY = 300; int radius = 200;//圆半径 int i; float offset;//同一半径各组扇形之间的偏移的角度 float totaloffset = 0;//不同半径之间的角度偏移量 for (centerX - 200; centerX < 1200; centerX = centerX + 400) { for (centerY = 200; centerY < 800; centerY = centerY + 400) { float h = rand() % 180;//随机色调 COLORREF color1 = HSVtoRGB(h, 0.9, 0.8);//色调1生成的颜色1 COLORREF color2 = HSVtoRGB(h + 180, 0.9, 0.8);//色调2生成的颜色2 for (radius = 200; radius > 0; radius = radius - 50) {//半径从小到大绘制 int left = centerX - radius; int top = centerY - radius; int right = centerX + radius; int bottom = centerY + radius; for (i = 0; i < 20; i++) {//旋转一周,绘制扇形区域 offset = i * PI / 10 + totaloffset; setfillcolor(color1); solidpie(left, top, right, bottom, offset, 2 * PI / 60 + offset);//画填充扇形 setfillcolor(WHITE); 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(BLUE); solidpie(left, top, right, bottom, 5 * PI / 60 + offset, 6 * PI / 60 + offset);//画填充扇形 } totaloffset = totaloffset + PI / 20; } } } _getch(); closegraph(); return 0; }