01 簡單繪圖
在這個程序中,我們先初始化繪圖窗口。其次,簡單繪制兩條線。
#include <graphics.h>//繪圖庫頭文件
#include <stdio.h>
int main()
{initgraph(640, 480);//初始化640?480繪圖屏幕line(200, 240, 440, 240);//畫線(200,240)-(440,240)line(320, 120, 320, 360);//畫線(320,120)-(320,360)getchar();closegraph();//關閉繪圖屏幕return 0;
}
02 熟悉更多的繪圖語句
上面中我們繪制了線段。
下面可以同時可以繪制圓,以及指定線條的顏色。
#include <graphics.h>//繪圖庫頭文件
#include <stdio.h>int main()
{initgraph(640, 480);//初始化640 480繪圖屏幕setlinecolor(BLUE); //指定線的顏色,注意這個必須在 前面。circle(240, 240, 50); //三個參數分別書圓的左邊x值,y值以及半徑getchar();closegraph();//關閉繪圖屏幕return 0;}
關于更多的顏色:
自由配置我們想要的顏色:
用數字表示顏色:
延時語句:
03 利用流程控制語句繪制
利用循環繪制線段
#include <graphics.h>
#include <conio.h>int main()
{initgraph(640, 480);for(int y=100; y<200; y+=10)line(100, y, 300, y);_getch();closegraph();return 0;
}
繪制漸進色
#include <graphics.h>//繪圖庫頭文件
#include <stdio.h>int main()
{initgraph(640, 480);//初始化640 480繪圖屏幕//畫10條線for (int y = 100; y <= 256; y ++){setcolor(RGB(0, 0, y));line(100, y, 300, y);}getchar();closegraph();//關閉繪圖屏幕return 0;
}
判斷奇偶
#include <graphics.h>
#include <conio.h>int main()
{initgraph(640, 480);for (int y = 100; y < 200; y += 10){if (y / 10 % 2 == 1) // 判斷奇數行偶數行setcolor(RGB(255, 0, 0));elsesetcolor(RGB(0, 0, 255));line(100, y, 300, y);}_getch();closegraph();return 0;
}
04 漸進色
實現滿屏的漸進色
#include <graphics.h>
#include <conio.h>int main()
{initgraph(640, 480);int c;for (int y = 0; y < 480; y++){c = y * 256 / 480;setlinecolor(RGB(0, 0, c));line(0, y, 639, y);}_getch();closegraph();return 0;
}
漸變圓
#include <graphics.h>
#include <conio.h>
#include <math.h>#define PI 3.14159265359int main()
{initgraph(640, 480);int c;double a;int x, y, r = 200;//利用弧度制進行計算for (a = 0; a < PI * 2; a += 0.0001)//a表示弧度{x = (int)(r * cos(a) + 320 + 0.5);//xy = (int)(r * sin(a) + 240 + 0.5);//yc = (int)(a * 255 / (2 * PI) + 0.5);//c顏色setlinecolor(RGB(c, 0, 0));line(320, 240, x, y);}_getch();closegraph();return 0;
}