C語言利用EasyX實現繪制足球圖案
思路
如何使用 C 語言配置EasyX繪圖庫繪制一個足球呢,今天我嘗試了一下,難度還算可以,但是過程比較繁瑣,代碼寫的有些復雜,后期有興趣的,可以在我的代碼的基礎上進行優(yōu)化,用更短的代碼將這個圖形繪制出來。
簡單描述一下這個程序的思路和實現過程。足球是一個由五邊形和六邊形組成的球體。這里我使用一個旋轉算法,首先確定一個足球的圓心。然后確定半徑,通過等間距旋轉,先獲得一個五邊形的坐標,使用多邊形填充的方式,得到一個填充的五邊形。然后通過同樣的方式,獲取一個十邊形以及二十邊形的坐標。然后按照一定規(guī)律連接這些坐標點。
以上只是我個人的思路,當然有更好的思路可以進行計算和繪制,例如足球是個球體,可以使用 EasyX 繪圖庫,通過計算每一個多邊形角點在三維空間中的位置,就可以繪制出一個三維的足球。當然這個就需要有一定的數學邏輯能力和計算機圖形學的知識了。
截圖

源碼
///
// 程序名稱:繪制足球
// 編譯環(huán)境:Mictosoft Visual Studio 2013, EasyX_20200315(beta)
//
#include<graphics.h>
#include<math.h>
#include<conio.h>
#define PI acos(-1.0)
int main()
{
initgraph(640, 480);
setbkcolor(GREEN);
setlinecolor(BLACK);
cleardevice();
setlinestyle(PS_SOLID, 4);
setfillcolor(WHITE);
fillcircle(320, 240, 150); // 繪制一個球形狀
setlinestyle(PS_SOLID, 2);
POINT pts[5];
POINT Fpts[5];
POINT Zpts[5];
// 計算五邊形的五個頂點坐標
double a = PI / 2;
for (int i = 0; i < 5; i++)
{
pts[i].x = int(320 + cos(a) * 60);
pts[i].y = int(240 - sin(a) * 60);
Zpts[i].x = int(320 + cos(a) * 100);
Zpts[i].y = int(240 - sin(a) * 100);
Fpts[i].x = int(320 + cos(a) * 110);
Fpts[i].y = int(240 - sin(a) * 110);
a += PI * 2 / 5;
}
setlinecolor(BLACK);
setfillcolor(BLACK);
solidpolygon(pts, 5);
for (int i = 0; i < 5; i++)
{
line(pts[i].x, pts[i].y, Zpts[i].x, Zpts[i].y);
}
POINT Spts[10];
POINT Wpts[20];
a = PI / 2 + PI / 20;
for (int i = 0; i < 20; i++)
{
Wpts[i].x = int(320 + cos(a) * 150);
Wpts[i].y = int(240 - sin(a) * 150);
a += PI * 1 / 10;
}
a = PI / 2 + PI / 10;
for (int i = 0; i < 10; i++)
{
Spts[i].x = int(320 + cos(a) * 128);
Spts[i].y = int(240 - sin(a) * 128);
a += PI * 1 / 5;
}
int P = 9;
for (int i = 0; i < 5; i++)
{
line(Zpts[i].x, Zpts[i].y, Spts[P].x, Spts[P].y);
P++;
if (P == 10)
{
P = 0;
}
line(Zpts[i].x, Zpts[i].y, Spts[P].x, Spts[P].y);
P++;
}
for (int i = 0; i < 9; i++)
{
line(Spts[i].x, Spts[i].y, Spts[i + 1].x, Spts[i + 1].y);
i++;
}
int PS = 0;
for (int i = 0; i < 20; i++)
{
if (i % 2 == 0)
{
if (PS % 2 == 0)
{
line(Wpts[i].x, Wpts[i].y, Spts[i / 2].x, Spts[i / 2].y);
}
else
{
line(Wpts[i + 1].x, Wpts[i + 1].y, Spts[i / 2].x, Spts[i / 2].y);
}
PS++;
}
}
polygon(Wpts, 20);
setlinestyle(PS_SOLID, 3);
circle(320, 240, 150);
setfillcolor(BLACK);
for (int i = 0; i < 5; i++)
{
floodfill(Fpts[i].x, Fpts[i].y, BLACK);
}
_getch();
return 0;
}到此這篇關于C語言利用EasyX實現繪制足球圖案的文章就介紹到這了,更多相關C語言EasyX繪制足球內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

