EasyX簡易繪圖實例
繪製一個圓從左向右移動
#include <graphics.h>
int main()
{
initgraph(640,480);//繪圖環境640*480
setcolor(WHITE);//繪圖前景色爲白色
setfillstyle(BS_SOLID);//填充樣式爲固實填充
setfillcolor(RED);//填充顏色爲藍色
BeginBatchDraw();//開始批量繪圖
for(int i=50; i<600; i++)
{
circle(i,100,40);//畫圓
floodfill(i, 100, WHITE);//填充
FlushBatchDraw();//執行未完成的繪製任務
Sleep(10);//掛起50毫秒
cleardevice();//用背景色清空屏幕
}
EndBatchDraw();//結束批量繪製
closegraph();//關閉圖形環境
return 0;
}
力學:彈跳球模擬程序
#include <graphics.h>
#include <conio.h>
int main()
{
double h = 300; // 高度
double v = 0; // 速度(方向向下)
double dv = 9.8 / 50; // 加速度(每 1/50 秒)
// 初始化繪圖窗口
initgraph(640, 480);
// 畫地平線
line(100, 421, 540, 421);
while(!_kbhit())
{
v += dv; // 根據加速度計算速度
h -= (v - dv / 2); // 計算高度
// 如果高度低於地平線,實現反彈,速度方向取反
if (h <= 0)
{
h += (v - dv / 2);
v = - v * 0.9; // 反彈時能量損耗 10%
}
// 畫綠色球
setcolor(GREEN);
circle(320, 400 - int(h), 20);
Sleep(20); // 延時(每幀延時 1/50 秒)
// 擦掉球
setcolor(BLACK);
circle(320, 400 - int(h), 20);
}
// 關閉繪圖窗口
closegraph();
return 0;
}
鐘錶模擬程序(錶針形式)
#include <graphics.h>
#include <conio.h>
#include <math.h>
#define PI 3.1415926536
void DrawHand(int hour, int minute, int second)
{
double a_hour, a_min, a_sec; // 時、分、秒針的弧度值
int x_hour, y_hour, x_min, y_min, x_sec, y_sec; // 時、分、秒針的末端位置
// 計算時、分、秒針的弧度值
a_sec = second * 2 * PI / 60;
a_min = minute * 2 * PI / 60 + a_sec / 60;
a_hour= hour * 2 * PI / 12 + a_min / 12;
// 計算時、分、秒針的末端位置
x_sec = int(120 * sin(a_sec));
y_sec = int(120 * cos(a_sec));
x_min = int(100 * sin(a_min));
y_min = int(100 * cos(a_min));
x_hour= int(70 * sin(a_hour));
y_hour= int(70 * cos(a_hour));
// 畫時針
setlinestyle(PS_SOLID, 10);
setcolor(WHITE);
line(320 + x_hour, 240 - y_hour, 320 - x_hour / 7, 240 + y_hour / 7);
// 畫分針
setlinestyle(PS_SOLID, 6);
setcolor(LIGHTGRAY);
line(320 + x_min, 240 - y_min, 320 - x_min / 5, 240 + y_min / 5);
// 畫秒針
setlinestyle(PS_SOLID, 2);
setcolor(RED);
line(320 + x_sec, 240 - y_sec, 320 - x_sec / 3, 240 + y_sec / 3);
}
void DrawDial()
{
// 繪製一個簡單的錶盤
circle(320, 240, 2);
circle(320, 240, 60);
circle(320, 240, 160);
outtextxy(296, 310, "BestAns");
// 繪製刻度
int x, y;
for (int i=0; i<60; i++)
{
x = 320 + int(145 * sin(PI * 2 * i / 60));
y = 240 + int(145 * cos(PI * 2 * i / 60));
if (i % 15 == 0)
bar(x - 5, y - 5, x + 5, y + 5);
else if (i % 5 == 0)
circle(x, y, 3);
else
putpixel(x, y, WHITE);
}
}
void main()
{
initgraph(640, 480); // 初始化 640 x 480 的繪圖窗口
DrawDial(); // 繪製錶盤
setwritemode(R2_XORPEN); // 設置 XOR 繪圖模式
// 繪製錶針
SYSTEMTIME ti; // 定義變量保存當前時間
while(!kbhit()) // 按任意鍵退出鐘錶程序
{
GetLocalTime(&ti); // 獲取當前時間
DrawHand(ti.wHour, ti.wMinute, ti.wSecond); // 畫表針
Sleep(1000); // 延時 1 秒
DrawHand(ti.wHour, ti.wMinute, ti.wSecond); // 擦錶針(擦錶針和畫表針的過程是一樣的)
}
closegraph(); // 關閉繪圖窗口
}
3 輛行駛的小車
#include <graphics.h>
#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
// 聲明需要使用的函數
void carstart(int x, int y, int z);
void drawbus();
void init();
// 定義全局變量
IMAGE img;
////////////////////////////////////////////
void main()
{
init();
int x=0, y=0, z=0;
BeginBatchDraw();
while(!kbhit())
{
x += 2;
y++;
z += 3;
if (x > 600) x = -200;
if (y > 600) y = -200;
if (z > 600) z = -200;
carstart(x, y, z);
FlushBatchDraw();
Sleep(5);
}
EndBatchDraw();
closegraph();
}
////////////////////////////////////////////
// 初始化函數,初始化窗口大小,獲取所畫圖片
void init()
{
// 初始化窗口大小
initgraph(600, 600);
outtextxy(70, 250, "大家好,新手來報到,希望大家多多指教");
outtextxy(70, 270, "下面你們會看到我程序的效果,程序很簡單");
outtextxy(70, 290, "希望以後再跟大家的交流中學到更多,希望自己以後能編出更好的程序");
outtextxy(70, 320, "請按任意鍵進觀看程序執行效果");
// 等待按鍵按下
getch();
cleardevice(); // 清除上面的文字進入運行效果畫面
drawbus(); // 調用繪圖函數,繪製 BUS
getimage(&img, 80, 40, 180, 90); // 獲取 BUS 圖片位置,保存在 img 變量中
}
//////////////////////////////////////////////////////////////
// 車輛行駛程序,通過 putimge 函數,改變移動的像素來達到圖片移動
void carstart(int x, int y, int z)
{
cleardevice();
putimage(x, 40, &img);
setlinestyle(PS_SOLID, 10); //設置畫線的大小
line(0, 135, 600, 135);
putimage(y, 220, &img);
line(0, 315, 600, 315);
putimage(z, 380, &img);
line(0, 475, 600, 475);
}
//////////////////////////////////////////////////////////////
// 繪製 BUS 函數,通過畫一些線條,組合它們的位置,組合成一輛小車
void drawbus()
{
setcolor(RED);
setfillstyle(BLUE);
fillcircle(120, 120, 10); // 畫車的輪胎
fillcircle(200, 120, 10); // 畫車的輪胎
line(80, 120, 110, 120); // 畫車的底部車板
line(80, 40, 80, 120); // 畫車的四周車板
line(130, 120, 190, 120); // 畫車的底部車板
line(210, 120, 250, 120); // 畫車的底部車板
line(250, 40, 250, 120); // 畫車的四周車板
line(80, 40, 250, 40); // 畫車的頂部車板
// 畫車窗
for(int x = 90, y = 100; x < 190 && y < 190; x += 15, y += 15)
{
rectangle(x, 60, y, 70);
}
// 畫車門
rectangle(220, 60, 240, 120);
line(230, 60, 230, 120);
circle(230, 90, 5);
}
鼠標操作演示
#include <graphics.h>
#include <conio.h>
int main()
{
// 初始化圖形窗口
initgraph(640, 480);
MOUSEMSG m; // 定義鼠標消息
while(true)
{
// 獲取一條鼠標消息
m = GetMouseMsg();
switch(m.uMsg)
{
case WM_MOUSEMOVE:
// 鼠標移動的時候畫紅色的小點
putpixel(m.x, m.y, RED);
break;
case WM_LBUTTONDOWN:
// 如果點左鍵的同時按下了 Ctrl 鍵
if (m.mkCtrl)
// 畫一個大方塊
rectangle(m.x-10, m.y-10, m.x+10, m.y+10);
else
// 畫一個小方塊
rectangle(m.x-5, m.y-5, m.x+5, m.y+5);
break;
case WM_RBUTTONUP:
return 0; // 按鼠標右鍵退出程序
}
}
// 關閉圖形窗口
closegraph();
return 0
}
模擬蒙特卡羅法求π的值
#include <graphics.h>
#include <conio.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
int main()
{
long m=0,n=0,i;
double xi,yi,y;
initgraph(640, 480);// 初始化 640 x 480 的繪圖窗口
setcolor(YELLOW);
//line(200,50,200,400);line(200,50,205,60); line(200,50,195,60);
//line(50,200,400,200);line(400,200,390,195); line(400,200,390,205);
rectangle(220,100,420,300);
circle(320,200,100);
srand((unsigned)time(NULL));
for(i=0; i<100000; i++) /*設置產生隨機數的個數*/
{
/*---產生-1到1之間的隨機數xi和yi-----*/
xi=(rand()%(1000-0+1)+0)/500.0-1.0;
yi=(rand()%(1000-0+1)+0)/500.0-1.0;
putpixel((int)(xi*100+320),(int)(yi*100+200),(int)GREEN);/*在正方形區域內畫點*/
/*printf("xi=%f,yi=%f\n",xi,yi);*/
if(yi*yi>=(-(1-xi*xi))&&yi*yi<=1-xi*xi) /*判斷是否在圓內並計數*/
m++,n++;
else m++;
Sleep(10);
}
y=4.0*n/m;
/*計算PI值*/
char str[100];
sprintf(str,"PI=%f",y);
outtextxy(280,400,str);
getch();
closegraph();// 關閉繪圖窗口
return 0;
}