當前位置:首頁 » 編程語言 » c語言畫多邊形
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言畫多邊形

發布時間: 2022-05-29 13:32:16

① 如何利用c語言程序設計繪制一個形狀(圓,橢圓,矩形都可以),用線條動態地填充其內部

你的c 編譯器需帶 繪圖函數庫 才行。
c++ API 程序 可以繪圖。只要得到窗口句柄,就可在該窗畫圖。畫直線,多邊形,圓,橢圓,扇形 等 都是基本函數。
下面程序在桌面窗口畫線:
#include <Afxwin.h>
#include <Windows.h>
#pragma comment (lib, "User32.lib")
int main(void){
HWND hWnd = ::GetDesktopWindow();
HDC hdc = ::GetDC(hWnd);
// HDC hdc = ::GetDC(NULL);
RECT rect;
::GetWindowRect(hWnd, &rect);
::MoveToEx(hdc, 0, 0, NULL);
::LineTo(hdc, rect.right, rect.bottom);
::MoveToEx(hdc, rect.right, 0, NULL);
::LineTo(hdc, 0, rect.bottom);
::ReleaseDC(hWnd, hdc);
system("PAUSE");
return 0;
}
===
用「刷子」畫填充的封閉圖形 (參數是 花紋,顏色)
HBRUSH Brush[5];
case WM_PAINT:
Brush[0] = CreateHatchBrush(HS_BDIAGONAL, RGB(0, 0, 255));
Brush[1] = CreateHatchBrush(HS_CROSS, RGB(200, 0, 0));
Brush[2] = CreateHatchBrush(HS_DIAGCROSS, RGB(0, 128, 0));
Brush[3] = CreateHatchBrush(HS_FDIAGONAL, RGB(0, 128, 192));
Brush[4] = CreateHatchBrush(HS_DIAGCROSS, RGB(255, 128, 0));
----
編譯器 MS VC++ 6.0.
API 程序羅嗦,這里就不列出來了。

② c語言 給出任意多邊形頂點,這些定點連起來是一個凸多邊形,如何對它進行填充希望有源程序。

// 點
struct point
{

float x,y; //x y 坐標
};

// 多邊形結構體
struct polygon
{

int num; // 點的數目

float offset_x,offset_y; // 偏移

float left,right,top,bottom; // 多邊形 上/下/左/右區域

struct point *points; //組成多邊形的點
};

// 生成多變形
// 點必須是有序的,按多邊形的頂點順時針給出
// p 組成多邊形的點
// size 點的數量
int gen_polygon_clockwise(const struct point* p,int size,struct polygon *poly)
{

int i ;

struct point *p_adr = (point *)malloc(sizeof(struct point) * size);

poly->num = size;

for (i = 0;i < size;i++) {

p_adr[i].x = p[i].x;

p_adr[i].y = p[i].y;

if (i == 0) {

poly->left = p[i].x;

poly->right = p[i].x;

poly->bottom = p[i].y;

poly->top = p[i].y;

} else {
// 確定區域的上/下/左/右 坐標

if (p[i].x < poly->left) {

poly->left = p[i].x;

}

if (p[i].x > poly->right) {

poly->right = p[i].x;

}

if (p[i].y < poly->top) {

poly->top = p[i].y;

}

if (p[i].y > poly->bottom) {

poly->bottom = p[i].y;

}

}

}

poly->points = p_adr;

return 0 ;
}

void free_polygon(struct polygon *poly)
{

free(poly->points);
}

// 點是順時針給出的
// 判斷點是否在直線的左/右區域
// 判斷一個點是否在一個區域里,主要是根據該點和組成多邊形的直線的關系來判斷
int is_point_local_line_region_clockwise(float x1,float x2,float y1,float y2,float x,float y)
{

float w,h,tmp;

// 下面的代碼,就不解釋了,你【順時針】畫一個多邊行,然後在任意畫一個點,然後
// 判斷組成多邊形的每條直線和點的關系就清楚了,主要還是根據該點是在直線的左側還是
// 右側(上側/下側)來判斷一個點是否在多邊行的裡面
//
// 直線傾率為無窮大

if (x1 == x2) {

if (y1 > y2) {

if (x < x1) {

return 0;

}

} else {

if (x > x1) {

return 0 ;

}

}

}

// 直線傾率為0

if (y1 == y2) {

if (x1 > x2) {

if (y > y1) {

return 0;

}

} else {

if (y < y1) {

return 0;

}

}

}

w = x2 - x1;

h = y2 - y1;

// 直線上的點
// 直線的傾率 直線的點傾式(y = k(x - x1) + y1)

tmp = h/w * (x -x1) + y1;

if (x1 > x2) {

if (y > tmp) {

return 0;

}

} else {

if (y < tmp) {

return 0;

}

}

return 1;
}

// 判斷一個點是否在多邊行裡面
// 在多邊形區域里則返回非0值,否則,返回0值
int is_point_in_polygon(float x,float y,const struct polygon *poly)
{

int i ;

if (x < poly->left || x > poly->right ||

y < poly->top || y > poly->right) {

return 0;

}

for (i = 0; i < poly->num - 1;i++) {

if (!is_point_local_line_region_clockwise(poly->points[i].x,

poly->points[i + 1].x,

poly->points[i].y,

poly->points[i + 1].y,x,y)) {

return 0 ;

}

}

if (!is_point_local_line_region_clockwise(poly->points[i].x,

poly->points[0].x,

poly->points[i].y,

poly->points[0].y,

x,y)) {

return 0 ;

}

return 1;
}

#define RESOLUTION_X 100

// 填充多邊形
// buffer 填充的二維數組
void fill_poly(const struct polygon *poly,char buffer[][RESOLUTION_X])
{

int x,y,w,h,i,j;

x = (int)poly->left + poly->offset_x;

y = (int)poly->top + poly->offset_y;

w = (int)(poly->right - poly->left);

h = (int)(poly->bottom - poly->top);

for (i = y;i < y + h;i++) {

for (j = x;j < x + w;j++) {

if (is_point_in_polygon(j,i,poly)) {

buffer[i][j] = 255;

}

}

}
}

int main()
{

char buffer[100][100];

struct polygon poly;

struct point points[8];

poly.offset_x = 0;

poly.offset_y = 0 ;

points[0].x = 50;

points[0].y = 50 - 30;

points[1].x = 50 + 21;

points[1].y = 50 - 21;

points[2].x = 50 + 30;

points[2].y = 50;

points[3].x = 50 + 21;

points[3].y = 50 + 21;

points[4].x = 50;

points[4].y = 50 + 30;

points[5].x = 50 - 21;

points[5].y = 50 + 21;

points[6].x = 50 - 30;

points[6].y = 50 ;

points[7].x = 50 - 21;

points[7].y = 50 - 21;

memset(buffer,0,sizeof(buffer));

gen_polygon_clockwise(points,8,&poly);

fill_poly(&poly,buffer);

for (int i = 0;i < 100;i++) {

for (int j = 0;j < 100;j++) {

if (buffer[i][j]) {

std::cout << "*";

} else {

std::cout << " " ;

}

}

std::cout << std::endl;

}

return 0;
}

####
你看對你有沒有幫助
浮點坐標到整數坐標只是簡單的類型轉換

③ 通過C語言建立一個不規則多邊形

我將在他身上擱這纖勁的手
當我膩了這些不虔敬的把戲;
我鋒利的指甲,象只哈哈兇猛的鷲,
將會劈開條血路直透他心裡。
我將從他胸內挖出這顆紅心,

④ 速求 c語言編程 給定n個點的坐標,這n個點依次圍成一閉合多邊形,再給一點(x,y),判斷它是否在多邊形中

程序代碼如下(直接套用函數pnpoly):

int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy)

{

int i, j, c = 0;

for (i = 0, j = nvert-1; i < nvert; j = i++) {

if ( ((verty[i]>testy) != (verty[j]>testy)) &&

(testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )

c = !c;

}

return c;

}

參數說明:

nvert: 多邊形的頂點數

vertx, verty: 頂點X坐標和Y坐標分別組成的數組

testx, testy: 需要測試的點的X坐標和Y坐標

(4)c語言畫多邊形擴展閱讀:

判定一個點是否在多邊形內部最簡單的方法是使用射線法,因為它能適用於所有類型的多邊形,不用考慮特殊的情況而且速度也比較快。

該演算法的思想很簡單:在多邊形外面任意一點畫一條虛擬的射線到p(x,y)然後計算該射線與多邊形上的邊相交的次數。如果該次數是偶數,說明p(x,y)在多邊形外,如果是奇數,則在多邊形內。

⑤ 怎麼在VC環境下用C語言編程畫多邊形

不怎麼明白,是在控制台下畫多邊形呢,還是在窗口下呢?
窗口下很簡單吧,控制台下貌似也得創建窗口,然後使用API吧

⑥ c語言的圖形函數庫有哪些

圖形和圖像函數包含在graphics.h裡面

(一) 像素函數
56. putpiel() 畫像素點函數
57. getpixel()返回像素色函數
(二) 直線和線型函數
58. line() 畫線函數
59. lineto() 畫線函數
60. linerel() 相對畫線函數
61. setlinestyle() 設置線型函數
62. getlinesettings() 獲取線型設置函數
63. setwritemode() 設置畫線模式函數
(三)、多邊形函數
64. rectangle() 畫矩形函數
65. bar() 畫條函數
66. bar3d() 畫條塊函數
67. drawpoly() 畫多邊形函數
(四)、 圓、弧和曲線函數
68. getaspectratio()獲取縱橫比函數
69. circle()畫圓函數
70. arc() 畫圓弧函數
71. ellipse()畫橢圓弧函數
72. fillellipse() 畫橢圓區函數
73. pieslice() 畫扇區函數
74. sector() 畫橢圓扇區函數
75. getarccoords()獲取圓弧坐標函數
(五)、 填充函數
76. setfillstyle() 設置填充圖樣和顏色函數
77. setfillpattern() 設置用戶圖樣函數
78. floodfill() 填充閉域函數
79. fillpoly() 填充多邊形函數
80. getfillsettings() 獲取填充設置函數
81. getfillpattern() 獲取用戶圖樣設置函數
(六)、圖像函數
82. imagesize() 圖像存儲大小函數
83. getimage() 保存圖像函數
84. putimage() 輸出圖像函數

四、圖形和圖像函數
對許多圖形應用程序,直線和曲線是非常有用的。但對有些圖形只能靠操作單個像素才能畫出。當然如果沒有畫像素的功能,就無法操作直線和曲線的函數。而且通過大規模使用像素功能,整個圖形就可以保存、寫、擦除和與屏幕上的原有圖形進行疊加。
(一) 像素函數

56. putpixel() 畫像素點函數
功能: 函數putpixel() 在圖形模式下屏幕上畫一個像素點。
用法: 函數調用方式為void putpixel(int x,int y,int color);
說明: 參數x,y為像素點的坐標,color是該像素點的顏色,它可以是顏色符號名,也可以是整型色彩值。
此函數相應的頭文件是graphics.h
返回值: 無
例: 在屏幕上(6,8)處畫一個紅色像素點:
putpixel(6,8,RED);

57. getpixel()返回像素色函數
功能: 函數getpixel()返回像素點顏色值。
用法: 該函數調用方式為int getpixel(int x,int y);
說明: 參數x,y為像素點坐標。
函數的返回值可以不反映實際彩色值,這取決於調色板的設置情況(參見setpalette()函數)。
這個函數相應的頭文件為graphics.h
返回值: 返回一個像素點色彩值。
例: 把屏幕上(8,6)點的像素顏色值賦給變數color。
color=getpixel(8,6);

⑦ C語言實現多邊形填充

樓主問的是圖形學演算法。。1樓給出的是??
有以前寫了一個多邊形種子填充演算法用的是貝塞爾方法你拿去看下把。。
1 用Bresenham直線與圓組成的扇形
void CFill4Dlg::Bresenham(int x0,int y0,int x1,int y1,int color)
{
int x,y,dx,dy,e;
dx=y1-x0;
dy=y1-y0;
e=-dx;
x=x0;
y=y0;
CClientDC dc(this);
for(x=x0;x<x1;x++)
{
dc.SetPixel(x,(int)(y+0.5),color);
e=e+2*dy;
if(e>=0)
{
y++;
e=e-2*dx;
}
}
}
void CFill4Dlg::CirclePoints(int x,int y,int m,int n,int color)
{
CClientDC dc(this);
dc.SetPixel(x+m_cx,y+m_cy,color);
dc.SetPixel(y+m_cx,x+m_cy,color);
dc.SetPixel(-x+m_cx,y+m_cy,color);
dc.SetPixel(y+m_cx,-x+m_cy,color);
dc.SetPixel(x+m_cx,-y+m_cy,color);
dc.SetPixel(-y+m_cx,x+m_cy,color);
dc.SetPixel(-x+m_cx,-y+m_cy,color);
dc.SetPixel(-y+m_cx,-x+m_cy,color);
}
void CFill4Dlg::MidPointCircle(int r,int m,int n,int color)
{
int x,y;
float d;
x=0;
y=r;
d=float(1.25-r);
CirclePoints(x,y,m,n,color);
while(x<=y)
{
if(d<0)
d+=2*x+3;
else
{
d+=2*(x-y)+5;
y--;
}
x++;
CirclePoints(x,y,m,n,color);
}
}

2 填充:
種子設定 filled_color=getpixel(m_zx,m_zy);
typedef struct
{//記錄種子點
int x;
int y;
}seed;
seed * seed_p;
#define STACKTOTAL 3000
void stack_init();
void setstackempty();
void stackpush(seed pt);
seed stackpop();
bool isstackempty();
int stack_number;
填充:
void CFill4Dlg::FloodFill4(int x,int y,int oldcolor,int newcolor)
{
CClientDC dc(this);
if((int)dc.GetPixel(x,y)==oldcolor)
{
drawpixel(x,y,newcolor);
FloodFill4(x,y+1,oldcolor,newcolor);
FloodFill4(x,y-1,oldcolor,newcolor);
FloodFill4(x-1,y,oldcolor,newcolor);
FloodFill4(x+1,y,oldcolor,newcolor);
}
}
void CFill4Dlg::drawpixel(int x, int y, int color)
{
CClientDC dc(this);
dc.SetPixel(x,y,color);
}

⑧ 用c語言編寫一個多邊形

直接用角度的三角函數來計算就可以了。

取一個半徑R,在中心的上方R處取第一個定點,然後逆時針旋轉360/n度,依次取出所有點,然後連線。

這樣可以嗎?

⑨ ·用C語言編多變的填充多邊形

#include Conio.h
#include graphics.h
#define closegr closegraph

void initgr(void) /* BGI初始化 */
{int gd=DETECT,gm=0; /* 和gd=VGA,gm=VGAHI是同樣效果 */
registerbgidriver(EGAVGA_driver);/* 注冊BGI驅動後可以不需要.BGI文件的支持運行 */
initgraph(&gd,&gm,);
}
void seedfilling(x,y,fill_color,boundary_color)

int x,y,fill_color,boundary_color;

{

int c;

c=getpixel(x,y); /*獲取當前點的顏色*/

if((c!=boundary_color)&&(c!=fill_color)) /*如果顏色為邊界色則不填充*/

{

putpixel(x, y, fill_color); /*畫點*/
getch(); /*加上這條語句可以顯示填充狀態 */

seedfilling(x+1,y, fill_color, boundary_color);

seedfilling(x-1,y, fill_color, boundary_color);

seedfilling(x, y+1, fill_color, boundary_color);

seedfilling(x, y-1, fill_color, boundary_color);

}

}

void main()
{
int a,b,color;

int gd=DETECT , gm;
int poly[10];
a=150 ;
b=140;
color=4;
initgraph(&gd , &gm , );

poly[0] = 110; /* 第一個點的x坐標以及y坐標 */
poly[1] = 110;

poly[2] = 200; /* 第二點 */
poly[3] = 105;

poly[4] = 170; /* 第三點 */
poly[5] = 120;

poly[6]=150; /*第四點*/
poly[7]=170;

poly[8]=110; /*多邊形的起點與終點一樣*/
poly[9]=110;
drawpoly(5,poly);/* 顯示各點連接起來的多邊形 */
seedfilling(a,b,color,15); /*種子填充多邊形*/

getch();
closegraph();
}

TC環境或者Win-TC下的啊