天天看點

C語言圖形函數代碼~持續更新中

    下面總結的是一些C語言圖形函數代碼~持續更新中

畫三類圓

#include
    
     
#include
     
      
#include
      
       
#include
       
        
#include
        
          int main(void) { initgraph(640,480); setfillcolor(YELLOW); solidcircle(100,100,50); fillcircle(200,200,50); circle(300,300,50);//circle(&x,&y,&r) 以(300,300)為圓心,以50為半徑 Sleep(1000); cleardevice();//清屏 Sleep(1000); closegraph();//關閉圖形界面 return 0; }
        
       
      
     
               

畫長方體

#include
     
      
#include
      
       
#include
       
        
#include
        
         
#include
         
           int main(void) { int gdriver = DETECT;//DETECE是定義在graphic.h裡的宏,值為0 //gdiver代表的是圖形驅動器(graphics driver) int gmode;//gmode代表的是圖形模式(graphics mode) int midx, midy;//midx=x軸的中值,midy=y軸的中值 initgraph(&gdriver, &gmode, " ");//initgraph(&形驅,&形模," ")函數用于初始化圖形系統,或一般為initgraph(640,480) midx = getwidth() / 2; midy = getheight() / 2; setfillstyle(DIAGCROSS_FILL); setfillcolor(YELLOW);//填充顔色 setcolor(YELLOW);//外框顔色 bar3d(midx - 50, midy - 50, midx + 50, midy + 50, 50, 50); _getch(); closegraph(); return 0; }
         
        
       
      
                

關于三類畫線函數

#include
  
   
#include
   
    
#include
    
     
#include
     
      
#include
      
        int main(void) { initgraph(640, 480); linerel(175, 200);//畫線函數,從目前點到相對坐标 line(175, 200, 450, 450);//劃線函數,從左~上點到右~下點 //它不會改變光标的位置,也就是說光标現在還在(175,200) moveto(450, 450);//用于将光标移動到x,y lineto(0, 0);//劃線函數,從目前點到絕對坐标 _getch(); return 0; } 
      
     
    
   
             

畫長方形

#include
  
   
#include
   
    
#include
    
     
#include
     
      
#include
      
        int main(void) { initgraph(640, 480); setfillcolor(YELLOW); solidrectangle(100, 100, 300, 200);//是無邊框可填充矩形 fillrectangle(300, 200, 450, 300);//是有邊框可填充矩形 rectangle(450, 300, 550, 400);//是有邊框不可填充矩形 _getch(); return 0; } 
      
     
    
   
             

畫橢圓

#include
   
    
#include
    
     
#include
     
      
#include
      
       
#include
       
         int main(void) { initgraph(640, 480); setfillcolor(YELLOW); solidellipse(50,50,150,100);//左 上 右 橢圓就形成在這個矩形裡 fillellipse(150, 100, 250, 150); ellipse(250, 150, 350, 200); _getch(); return 0; } 
       
      
     
    
              

畫圓角矩形

#include
    
     
#include
     
      
#include
      
       
#include
       
        
#include
        
          int main(void) { initgraph(640, 480); setfillcolor(YELLOW); solidroundrect(50,50,150,100,10,10); fillroundrect(170, 120, 270, 170,10,10); roundrect(290, 190, 370, 220,10,10);// (int left, int top, int right, int bottom, int ellipsewidth, int ellipseheight); _getch(); return 0; } 
        
       
      
     
               

畫扇形

#include
    
     
#include
     
      
#include
      
       
#include
       
        
#include
        
          #define N 3.14159266 int main(void) { initgraph(640, 480); setfillcolor(YELLOW); // solidpie(90, 190, 370, 270,0,N/2); // fillpie(90, 190, 370, 270,0,N/2); pie(290, 190, 370, 270,0,N/2);//(int left, int top, int right, int bottom, double stangle, double endangle) _getch(); return 0; } 
        
       
      
     
               

畫多邊形

#include
     
      
#include
      
       
#include
       
        
#include
        
         
#include
         
           int main(void) { initgraph(640, 480); POINT a[3] = {100, 100, 300, 100, 300, 450 }; polygon(a, 3);//void polygon (const POINT *points, int num); /*typedef struct tagPOINT { LONG x; LONG y; } POINT, *PPOINT, NEAR *NPPOINT, FAR *LPPOINT; */ //polygon函數的第一個參數就是POINT結構類型的指針結構變量points,後一個參數是頂點個數 //畫的多邊形是不可填充的 _getch(); return 0; }
         
        
       
      
                

畫折線

#include
    
     
#include
     
      
#include
      
       
#include
       
        
#include
        
          int main(void) { initgraph(640, 480); POINT a[3] = {100, 100, 300, 100, 300, 450 }; polyline(a, 3);//與polygon函數的差別在于它最後不會自動封口。 //最後一個參數是點的個數。 _getch(); return 0; }
        
       
      
     
               

關于文本函數

#include
     
      
#include
      
       
#include
       
        
#include
        
         
#include
         
           #include
          
            #define N 3.14159266 int main(void) { initgraph(640, 480); int height, width; settextstyle(100, 100, L"仿宋",-900,900,FW_THIN,false,false,false); //(int nHeight, int nWidth, LPCTSTR lpszFace, int nEscapement, int nOrientation, int nWeight, bool bItalic, bool bUnderline, bool bStrikeOut); //(單字高,單字寬,字型,字型的傾斜度[0.1°為機關],字元的傾斜度[0.1°為機關],字的粗細<0,100,200···1000>,是否斜體[true&false],是否加下劃線,是否加删除線 //關于字型的傾斜度 eg.-900即豎向輸出文本 //關于字元的傾斜度 eg.900即将字元逆時針旋轉90° setbkcolor(YELLOW);//用于給字元加底色 setcolor(RED);//設定字元顔色 outtextxy(50,0,L"hello");//在設定位置輸出字元串 height = textheight(L"hello");//傳回字元串所用像素的高度 width = textwidth(L"hello");//傳回字元串所用像素的寬度 //高寬會比設定的大一些,因為還有預設的邊框,和設定的底色 _getch(); printf("height=%d,width=%d\n", height, width); return 0; }
          
         
        
       
      
                

在圖形框顯示圖檔

#include
    
     
#include
     
      
#include
      
       
#include
       
        
#include
        
          #include
         
           #define N 3.14159266 int main(void) { initgraph(640, 480); IMAGE img;//IMAGE是定義的結構體,參數為getwidth(),getheight() int width, height; loadimage(&img, L"D:\\try.jpg.jpg",200,200,false); //(IMAGE *pDstImg, LPCTSTR pImgFile, int nWidth = 0, int nHeight = 0, bool bResize = false); //參數2是圖檔儲存的位址,參數3,4是設定圖檔的寬和高(大小) // Resize(&img, 100, 100);//是設定圖形裝置的尺寸,100,100,螢幕上就隻能顯示出100,100那個矩形的圖檔 width=img.getwidth();//圖形框内圖檔的寬度 height = img.getheight();//圖形框内圖檔的高度 putimage(0, 0, &img, SRCCOPY);//在圖形框内顯示圖檔(int dstX, int dstY, const IMAGE *pSrcImg, DWORD dwRop = SRCCOPY); //前兩個參數,是圖檔顯示的左上角的點的坐标 _getch(); printf("height=%d\nwidth=%d\n", height, width); return 0; }
         
        
       
      
     
               

setlinestyle和HSLtoRGB函數

#include
   
    
#include
    
     
#include
     
      
#include
      
       
#include
       
         #include
        
          #define N 3.14159266 int main(void) { initgraph(640, 640); float H = 0; float S = 1; float L = 0.5f; setlinestyle(PS_SOLID, 1); // 設定線寬為 2 //線型(PS_SOLID實線型),線粗(一般是1,3代表細粗) setcolor(HSLtoRGB(0,0, L)); //HSLtoRGB(a,b,c);參數都是float型的,代表色相(0~360,可百度色相圖),飽和度(0~1,0為灰色,1為最純的色光),亮度(0黑~0.5最鮮豔~1白) circle(320, 640, 400); _getch(); return 0; } 
        
       
      
     
    
              

國際象棋盤

#include
      
       
#include
       
        
#include
        
         
#include
         
          
#include
          
            #include
           
             #define N 3.14159266 char ch[9] = "ABCDEFGH"; int num[8] = { 12345678 }; void draw_even_square(int i); void draw_odd_square(int i); void printf_letter(void); int main(void) { initgraph(500, 500); int i; setcolor(HSLtoRGB(0, 0, 0.5)); setfillcolor(HSLtoRGB(0, 0, 0.5)); fillrectangle(50, 50, 450, 450); setfillcolor(HSLtoRGB(15,0.4,0.4)); fillrectangle(60, 60, 440, 440); setfillcolor(HSLtoRGB(0, 0, 0.5)); fillrectangle(80, 80, 420, 420); setfillcolor(HSLtoRGB(35, 0.2, 0.5)); fillrectangle(90, 90, 410, 410); setlinecolor(BLACK); for (i = 1; i <= 8; i++) { if (i%2!=0) draw_odd_square(i); else draw_even_square(i); } printf_letter(); _getch(); return 0; } void draw_odd_square(int i) { int k; setfillcolor(BLACK); setcolor(BLACK); for (k = 1; k <= 4; k++) { fillrectangle(90 + (i / 2) * 40 * 2, 90 + 40 + (k - 1) * 40 * 2, 90 + 40 + (i / 2) * 40 * 2 , 90 + 40 + (k - 1) * 40 * 2+40); } } void draw_even_square(int i) { int k; setfillcolor(BLACK); setcolor(BLACK); for (k = 1; k <= 4; k++) { fillrectangle(90 +40+ (i/2-1) * 40*2, 90 + (k - 1) * 40*2, 90 +40+ (i/2-1) * 40*2+40 , 90 + (k-1) * 40*2+40); } } void printf_letter(void) { int j,k; for (j = 0; j < 8; j++) { settextcolor(WHITE); setbkcolor(HSLtoRGB(15, 0.4, 0.4)); settextstyle(10,10, L"宋體", 0, 1800, 1, false, false, false); outtextxy(107 + 40 * j, 75,ch[j]); settextstyle(10,10, L"宋體", 0, 0, 1, false, false, false); outtextxy(108 + 40 * j, 425,ch[j]); } for (k = 0; k < 8; k++) { settextcolor(WHITE); setbkcolor(HSLtoRGB(15, 0.4, 0.4)); settextstyle(10, 10, L"宋體", 0, 1800, 1, false, false, false); outtextxy(425, 115+40*k, ch[k]); settextstyle(10, 10, L"宋體", 0, 0, 1, false, false, false); outtextxy(65,105 + 40 * k, ch[k]); } }
           
          
         
        
       
                 

星空和RGB函數

#include
    
     
#include
     
      
#include
      
       
#include
       
        
#include
        
          #define N 200 //N為星星總數 struct STAR { double x; //作為點的坐标應該是int型的,因為putpixel函數的參數就是int x,int y double y; //但是為了更真實就用了double double step; double colortemp; int color;//RGB(紅,綠,藍)函數的傳回值為int型 }; STAR star[N]; void initstar(int i) { star[i].x = 0; star[i].y = rand() % 480; star[i].step = rand() % 5000 / 1000.0 + 1; star[i].colortemp = star[i].step * 255 / 6; star[i].color = (RGB(star[i].colortemp, star[i].colortemp, star[i].colortemp)); //RGB(a,b,c),當a=b=c時,為灰色,值越大就越亮 } void movestar(int i) { putpixel(star[i].x, star[i].y,BLACK);//這裡要設成黑色,才可以擦掉原來的點~ star[i].x += star[i].step; putpixel(star[i].x, star[i].y, star[i].color); if (star[i].x > 640) { initstar(i); putpixel(star[i].x, star[i].y, star[i].color); } } int main(void) { initgraph(640, 480); srand(time(NULL)); int i; unsigned long num=0; for (i = 1; i <= N; i++) { initstar(i); star[i].x = rand() % 640;//這個可以讓開始的時候不出現右邊是空的的情況 } while (!_kbhit()) //_kbkit() 若沒有擊鍵,則傳回值為0 { for (i = 1; i < N; i++) { movestar(i); } Sleep(100);//發現星星移動的巨快,就先讓這個程式睡0.1s~ //以毫秒為機關0.001s } _getch(); return 0; }
        
       
      
     
               

上面的星空的代碼,就真的很好很好,說明了兩個很重要的道理, 其一:所謂的動态不過是靜态說了一個謊···· 其二:以後做動态代碼,就按照這種思路。 建立主體,初始化主體函數,移動主體函數,主函數。

會動的圓

#include
    
     
#include
     
      
#include
      
       
#include
       
        
#include
        
          void set_move_circle(void) { int i,k; setfillcolor(GREEN); fillcircle(40, 240, 40); for (i = 1; i <= 560; i++) { setfillcolor(BLACK); setcolor(BLACK); fillcircle(40 + i - 1, 240, 40); setfillcolor(GREEN); setcolor(WHITE); fillcircle(40 + i, 240, 40); Sleep(10); } for (k = 1; k <= 560; k++) { setfillcolor(BLACK); setcolor(BLACK); fillcircle(40 + i-(k - 1), 240, 40); setfillcolor(GREEN); setcolor(WHITE); fillcircle(40 + i-k, 240, 40); Sleep(10); } } int main(void) { initgraph(640, 480); set_move_circle(); _getch(); return 0; }
        
       
      
     
               

移動的車

#include
    
     
#include
     
      
#include
      
       
#include
       
        
#include
        
          #include
         
           #include
          
            #include
           
             #include
            
              #include
             
               void set_move_car(void) { int i; setfillcolor(HSLtoRGB(180,1,0.5)); fillrectangle(10, 200, 50, 220); setfillcolor(HSLtoRGB(30, 0.5, 0.5)); fillcircle(20, 225, 5); fillcircle(40, 225, 5); for (i = 1; i <= 560; i++) { setcolor(BLACK); setfillcolor(BLACK); fillrectangle(10+(i-1), 200, 50+(i-1), 220); fillcircle(20 + (i - 1), 225, 5); fillcircle(40 + (i - 1), 225, 5); setcolor(BLACK); setfillcolor(HSLtoRGB(180, 1, 0.5)); fillrectangle(10+i, 200, 50+i, 220); setfillcolor(HSLtoRGB(30, 0.5, 0.5)); fillcircle(20+i, 225, 5); fillcircle(40+i, 225, 5); Sleep(10); } } int main(void) { initgraph(640, 480); set_move_car(); _getch(); return 0; }
             
            
           
          
         
        
       
      
     
               

煙花

#include
     
      
#include
      
       
#include
       
        
#include
        
         
#include
         
           #include
          
            #define Pi 3.14159266 #define N 200 //Littlestar的數目 void draw_Bigstar(void)//畫空心五角星的代碼// { int num_x[10] = { 50, 110, 170, 230, 290, 350, 410, 470, 530, 590 }; int num_y[10] = { 50, 75, 35, 42, 60, 32, 45, 65, 48, 40 }; int bian = 5;//定義星星的邊長為5 int i, k, t; POINT STAR[10];//為使用polygon函數為星星的十個點的xy坐标提供儲存的位置 for (i = 0; i < 9; i++) { STAR[0].x = num_x[i]; STAR[0].y = num_y[i]; t = -4; for (k = 1; k < 10; k++) { if (k % 2) { t += 4; STAR[k].x =STAR[k-1].x+ (int)(5 * cos(t * 36 * Pi / 180)); STAR[k].y = STAR[k - 1].y + (int)(5 * sin(t * 36 * Pi / 180)); } else { t -= 2; STAR[k].x = STAR[k - 1].x+(int)(5 * cos(t * 36 * Pi / 180)); STAR[k].y = STAR[k - 1].y+(int)(5 * sin(t * 36 * Pi / 180)); } setlinecolor(WHITE); line(STAR[k - 1].x, STAR[k - 1].y, STAR[k].x, STAR[k].y); if (k == 9) { line(STAR[9].x, STAR[9].y, STAR[0].x, STAR[0].y); } } } } void draw_Littlestar(void) { srand(time(NULL)); int i; int x, y, color; for (i = 0; i < N; i++) { color = rand() % 100 + 150; x = rand() % 640; y = rand() % 640; putpixel(x, y, RGB(color, color, color)); } } /* void init_Firework(void) { int point_x, point_y; int size; int color; srand(time(NULL)); point_x = rand() % 450; point_y = rand() % 260 + 170; size = rand() % 50 + 50; color = rand() % 225 + 90; } 由于初始化的好多變量都在其他兩個函數裡面有很大的作用,是以就幹脆在創造煙花函數裡面做好了*/ void cover_Firework(int point_x,int point_y,int size) { int cover = 0; setcolor(BLACK); setfillcolor(BLACK); for (; cover <= size; cover+=3) { if (cover + 3 - size) { cover = size; } fillcircle(point_x, point_y, cover); Sleep(30); } Sleep(10); } void create_Firework(void) { while (!_kbhit()) { srand(time(NULL)); //初始化煙花變量 int point_x, point_y; int size; int color; srand(time(NULL)); point_x = rand() % 300+100; point_y = rand() % 200 + 150; size = rand() % 10 + 50; color = rand() % 225 + 90; //初始化完了~ //正式的建立煙花函數開始 int i, j; int x, y; for (i = 0; i < size; i+=5) { for (j = 0; j < 6; j++) { color--; x = point_x + (int)i*cos(j * 60 * Pi / 180); y = point_y + (int)i*sin(j * 60 * Pi / 180); putpixel(x, y, HSLtoRGB(color, 1, 0.5)); putpixel(x - 1, y, HSLtoRGB(color, 1, 0.5)); putpixel(x + 1, y, HSLtoRGB(color, 1, 0.5)); putpixel(x, y - 1, HSLtoRGB(color, 1, 0.5)); putpixel(x, y + 1, HSLtoRGB(color, 1, 0.5)); putpixel(x - 1, y - 1, HSLtoRGB(color, 1, 0.5)); putpixel(x + 1, y - 1, HSLtoRGB(color, 1, 0.5)); putpixel(x - 1, y + 1, HSLtoRGB(color, 1, 0.5)); putpixel(x + 1, y + 1, HSLtoRGB(color, 1, 0.5)); } Sleep(100); } //這個煙花建立完了,開始cover cover_Firework(point_x,point_y,size); //這裡之是以要用清屏函數就是因為cover總有可能覆寫一些星星,又沒有什麼更好的辦法 //雖然我想到了星空那樣的代碼,不過感覺沒必要 cleardevice(); draw_Bigstar(); draw_Littlestar(); } } int main(void) { initgraph(640, 640); draw_Bigstar(); draw_Littlestar(); create_Firework(); _getch(); return 0; }
          
         
        
       
      
                

記得知道C語言圖形學的願望就是因為想做煙花,10天多以後終于實作了

方塊填色遊戲

#include
    
     
#include
     
      
#include
      
       
#include
       
        
#include
        
          /* struct MOUSEMSG { UINT uMsg; // 目前滑鼠消息 bool mkCtrl; // Ctrl 鍵是否按下 bool mkShift; // Shift 鍵是否按下 bool mkLButton; // 滑鼠左鍵是否按下 bool mkMButton; // 滑鼠中鍵是否按下 bool mkRButton; // 滑鼠右鍵是否按下 int x; // 目前滑鼠 x 坐标 int y; // 目前滑鼠 y 坐标 int wheel; // 滑鼠滾輪滾動值 }; */ /* WM_MOUSEMOVE 滑鼠移動消息 WM_MOUSEWHEEL 滑鼠滾輪撥動消息 WM_LBUTTONDOWN 左鍵按下消息 WM_LBUTTONUP 左鍵彈起消息 WM_LBUTTONDBLCLK 左鍵輕按兩下消息 WM_MBUTTONDOWN 中鍵按下消息 WM_MBUTTONUP 中鍵彈起消息 WM_MBUTTONDBLCLK 中鍵輕按兩下消息 WM_RBUTTONDOWN 右鍵按下消息 WM_RBUTTONUP 右鍵彈起消息 WM_RBUTTONDBLCLK 右鍵輕按兩下消息 */ void setcolorblock(void); void set_Block(void); int main(void) { initgraph(640, 480); MOUSEMSG m; double x, y; int i; int color; setcolorblock(); set_Block(); printf("%d\n", BLUE); while (true) { m = GetMouseMsg(); if (m.uMsg != WM_LBUTTONDOWN&&m.uMsg != WM_RBUTTONDOWN) { continue; } else if (m.uMsg == WM_LBUTTONDOWN) { if (m.y >= 400 && m.y <= 450 && m.x <= 400 && m.x >= 200) { for (i = 0; i < 5; i++) { if (m.x >= (200 + i * 50) && m.x <= (200 + (i + 1) * 50)) { switch (i) { case 0: color = BLUE; break; case 1: color = YELLOW; break; case 2: color = GREEN; break; case 3: color = RED; break; } } } } else if (m.x >= 100 && m.x <= 500 && m.y >= 100 && m.y <= 300) { for (i = 0; i < 2; i++) { if (m.x >= (100 + i * 200) && m.x <= (100 + (i + 1) * 200)) { setcolor(BLACK); setfillcolor(BLACK); fillrectangle(100 + 200 * i, 100 , 100 + 200 * (i + 1), 300); setcolor(WHITE); setfillcolor(color); fillrectangle(100 + 200 * i, 100, 100 + 200 * (i + 1), 300); } } } } else if (m.uMsg == WM_RBUTTONDOWN) { break; } } closegraph(); return 0; } void setcolorblock(void) { setcolor(WHITE); setfillcolor(BLUE); int num = getfillcolor(); fillrectangle(200, 400, 250, 450); setfillcolor(YELLOW); fillrectangle(250, 400, 300, 450); setfillcolor(GREEN); fillrectangle(300, 400, 350, 450); setfillcolor(RED); fillrectangle(350, 400, 400, 450); } void set_Block(void) { setcolor(WHITE); setfillcolor(BLACK); fillrectangle(100, 100, 500, 300); line(300, 100, 300, 300); }
        
       
      
     
               
C語言圖形函數代碼~持續更新中

繼續閱讀