網站首頁 編程語言 正文
C語言求解圖形問題
什么是圖形問題?
由字符,特殊符號或數字組成的各種形狀的圖形問題。
怎么求解圖形問題?
用循環,確定輸出圖形的行數和每行上的列數,確定每行第一個字符輸出的空格數,找出每行上字符變化的規律。
圖形輸出常用模塊:
for(i=1;i<=行數;i++)
{
? ? ?for(j=1;j<=第i行前導空格數;j++)
? ? ? ? ? ? ?printf("");
? ? ?for(k=1;k<=第i行字符串;k++)
? ? ? ? ? ? ?printf(第i行,第j行字符);
? ? ?printf("\n");
}? ??
1.輸出一個數字三角形。
代碼如下:
#include <stdio.h>
#include <stdlib.h>
int main()
{ int i,j,k;
? for(i=1;i<=5;i++)
? {for(j=1;j<=5-i;j++)
? ? ? printf(" ");
? ?for(k=1;k<=2*i-1;k++)
? ? ? printf("%d",k);
? ?printf("\n");
? }
? ? return 0;
}
2.輸出一個菱形。
代碼如下:
#include <stdio.h>
int main()
{ ? int i,j ,k;
? ? ? for(i=1;i<=4;i++)
? ? ? ?{
? ? ? ? ? for(j=1;j<=5-i;j++)
? ? ? ? ? ? ? printf(" ");
? ? ? ? ? for(k=1;k<=2*i-1;k++)?
? ? ? ? ? ? ? printf("*");
? ? ? ? ? ? ?printf("\n");
? ? ? ? }
? ? ? ? for(i=1;i<=3;i++)
? ? ? ? ?{
? ? ? ? ? ? for(j=1;j<=i+1;j++)
? ? ? ? ? ? ? ? printf(" ");
? ? ? ? ? ? for(k=1;k<=7-2*i;k++)
? ? ? ? ? ? ? ? printf("*");
? ? ? ? ? ? ? printf("\n");
? ? ? ? ? }
? ? ? ? ? ?return 0;
?}
C語言常用圖形函數
屏幕顏色的設置和清屏函數
① 設置背景色:
void setbkcolor(int color);
② 設置前景色:
void setcolor(int color);?
③ 清除圖形屏幕內容,使用清屏函數,其調用格式如下:
void cleardevice( ); ?
注:清除前景圖形,不清除背景。?
基本圖形函數
① 畫點函數:
將點(x, y)置成color色:void putpixel(int x, int y, int color);
獲得當前點(x, y)的顏色值:int getpixel(int x, int y);?
例:使(20, 50)的像素點置成紅色輸出。
putpixel(20, 50, RED);
② 有關坐標位置的函數:
- 返回x軸的最大值:int getmaxx( );?
- 返回y軸的最大值:int getmaxy( );?
- 返回游標在x軸的位置:int getx( );
- 返回游標在y軸的位置:int gety( );
- 移動游標到(x, y)點:void moveto(int x, int y);
- 將游標從現行位置(x, y)移動到(x+dx, y+dy)的位置,移動過程中不畫點:void moverel(int dx, int dy);
③ 畫線函數:
畫一條從點(x0, y0)到(x1, y1)的直線:
void ?line(int x0, int y0, int x1, int y1);
畫一條從現行游標到點(x, y)的直線:
void ?lineto(int x, int y);
畫一條從現行游標(x, y)到按相對增量確定的點(x+dx, y+dy)的直線:void ?linerel(int dx, int dy);
④ 畫圓弧類函數:
以(x, y)為圓心,radius為半徑,畫一個圓:
void ?circle(int x, int y, int radius);
以(x, y)為圓心,radius為半徑,從stangle開始到endangle結束(用度表示),畫一段圓弧線,逆時針方向:
void ?arc(int x, int y, int stangle, int endangle, int radius);
以(x, y)為中心,xradius、yradius為x軸和y軸半徑,從角stangle開始,endangle結束,畫一段橢圓線,
當stangle=0,endangle=360時,畫出一個完整的橢圓:
void ellipse(int x, int y, int stangle, int endangle, int xradius, int yradius);
⑤ 畫多邊形類函數:
以(x1, y1)為左上角,(x2, y2)為右下角,畫一個矩形框:
void rectangle(int x1, int y1, int x2, inty2);
畫一個頂點數為numpoints,各頂點坐標由整型數組polypoints給出的多邊形。polypoints必須至少有2倍頂點數個元素。
每個頂點坐標都定義為x、y,且x在前。當畫一個封閉多邊形時,numpoints的值取實際多邊形的頂點數 ? +1,且數組polypoints中第一個和最后一個點的坐標相同:void drawpoly(int numpoints, int *polypoints);
設置線型和線寬
① 線型:C語言能夠使用的線型如表所示。
② 線寬:C語言能夠使用的線寬如表所示。
③ setlinestyle函數:
- 功能:用于設置當前繪圖所用的線型和寬度。
- 原型:setlinestyle(int style, unsigned pattern, int width)
- 參數style:用來指定所畫直線的類型,取值見線型表,缺省為實線;
- 參數width:用來指定所畫直線的粗細,以像素為單位,取值見線寬表,缺省值為1個像素寬。
- 參數pattern:該參數在用戶自定義線型時使用。如果使用線型表中前4種系統預定義的線型,則該參數取值0。pattern是一個16位二進制數,每一位代表一個像素,該位為1時顯示,為0時不顯示。
例如:希望設置的線型為前12位不顯示,后4位顯示。
此時,pattern值為15,調用方法為setlinestyle(4, 15, 1)。
填充圖形函數
① setfillstyle函數:
- 功能:為各種圖形函數設置填充模式和顏色。
- 原型:setfillstyle(int ?pattern, int ?color);
- 參數:color指定填充所用的顏色;pattern用于指定填充模式,取值見下表。
② floodfill函數
- 功能:用于對一指定的封閉區域進行填充,其填充模式和顏色由setfillstyle函數指定。
- 原型:floodfill(int ?x, int ?y, int ?border);
- 參數:x、y指位于填充區域內任意一點的坐標,該點作為填充的起始點;border為填充區域的邊界顏色。
- 注意:使用該函數必須保證要填充的區域是完全封閉的,否則,該形狀外面的區域也將被填充。
③ setfillpattern函數
- 功能:實現用戶自定義的填充模式。
- 原型:setfillpattern(char *pattern, int color);
- 參數:color指定用戶自定義填充模式的顏色;pattern指向8個字節,一個字節對應8個像素,8*8個像素,每一位有亮暗兩種(亮為1,暗為0),通過一個指向8個字節的指針判斷。
圖形存取處理函數 ?
測試要保存左上角為(x1, y1),右下角為(x2, y2)的圖形屏幕區域內的全部內容需多少個字節:
unsigned imagesize(int x1, int y1, int x2, int y2);?
將左上角為(x1, y1),右下角為(x2, y2)的圖形屏幕區域內的圖像保存在內存中,指針為mapbuf:
void getimage(int x1, int y1, int x2, int y2, void ?*mapbuf);
將圖像輸出到左上角為點(x, y)的位置上,其中參數op規定如何釋放內存中的圖像,具體值如表:
void putimage(int x, int y, void * mapbuf, int op);?
文本輸出函數 在現行位置輸出字符串指針textstring所指的文本:
void outtext(char *textstring);
在規定的(x, y)位置輸出字符串指針textstring所指的文本,其中x和y為象元坐標:
void outtextxy(int x, int y, char *textstring);
原文鏈接:https://blog.csdn.net/minghaibuai/article/details/124294232
相關推薦
- 2022-08-05 利用Python?list列表修改元素_python
- 2022-04-16 實例講解python讀取各種文件的方法_python
- 2022-04-07 淺談C++11中=delete的巧妙用法_C 語言
- 2023-12-19 Mybatis使用注解實現復雜動態SQL
- 2023-01-12 Python讀取及保存mat文件的注意事項說明_python
- 2022-09-03 C#中DataSet、DataTable、DataRow數據的復制方法_C#教程
- 2023-05-07 Python3中省略號(...)用法介紹_python
- 2023-07-04 springboot引入外部sdk,以及在maven中配置,以及連同sdk打包
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支