当前位置:首页 » 编程语言 » 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下的啊