多边形填充实验
2007-11-07 15:00
141 查看
实验目标:
1.窗口对用户"左按键"和"右按键"事件的响应
2.实现多边形填充
步骤:
1.运行VC6.0
2.创建工程Fill , (工程类型选择:MFC Wizard (.exe) )
3. 添加 "双击左键"和"单击右键"事件的消息响应
(1).选择菜单"View"->"class Wizard"
(2)在弹出的窗口里
classname: 选择CFillView
Messages: 分别选择 WM_LBUTTONDBLCLK , WM_RBUTTONDOWN
然后Add Function
4.修改代码:
(1) 修改fillview.h文件 :
在类的定义中,加入如下代码:
public:
CPoint spt[7];
void Floodfill(CPoint s_point, int f_color, int b_color) ;
(2)修改fillview.cpp文件
修改OnDraw,OnLButtonDblClk,OnRButtonDown的三个函数
加入一个函数: Floodfill
void CFillView::OnDraw(CDC* pDC)
{
CFillDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CPen newpen(PS_SOLID,1,RGB(255,0,0));
CPen *old=pDC->SelectObject(&newpen);
pDC->TextOut(20,20,"双击鼠标左键, 出现需填充的多边形");
pDC->TextOut(20,50,"进行填充, 需用鼠标右键, 单击多边形内一点, 作为开始填充的种子点");
pDC->SelectObject(old);
}
(2)void CFillView::OnLButtonDblClk(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
RedrawWindow();
CDC* pDC=GetDC();
CPen newpen(PS_SOLID,1,RGB(255,0,0));
CPen *old=pDC->SelectObject(&newpen);
//多边形各顶点的坐标
spt[0]=CPoint(100,100);
spt[1]=CPoint(300,100);
spt[2]=CPoint(250,250);
spt[3]=CPoint(100,250);
spt[4]=CPoint(150,200);
spt[5]=CPoint(90,180);
spt[6]=CPoint(150,150);
spt[7]=CPoint(100,100);
pDC->Polyline(spt,8); //画多边形
pDC->SelectObject(old);
ReleaseDC(pDC);
CView::OnLButtonDblClk(nFlags, point);
}
void CFillView::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CView::OnRButtonDown(nFlags, point);
int fill=RGB(0,255,0);
int boundary=RGB(255,0,0);
Floodfill(point, fill, boundary);
}
/*
s_point 为种子点
f_color是要填充的颜色, b_color是边界的颜色
*/
void CFillView::Floodfill(CPoint s_point, int f_color, int b_color)
{
CWindowDC dc (this);
int x,y,p0,pmin,pmax;
//求多边形的最大最小值
pmin=pmax=spt[0].y;
for(int i=1;i<8;i++)
{
if(spt[i].y<pmin) pmin=spt[i].y;
if(spt[i].y>pmax) pmax=spt[i].y;
}
x=s_point.x;
y=s_point.y;
for(;y<pmax;y++)
{
int current=dc.GetPixel(x,y);
while((current!=b_color)&&(current!=f_color))
{
dc.SetPixel(x,y,f_color);
x++;
current=dc.GetPixel(x,y);
}
x=s_point.x;
x--;
current=dc.GetPixel(x,y);
while((current!=b_color)&&(current!=f_color))
{
dc.SetPixel(x,y,f_color);
x--;
current=dc.GetPixel(x,y);
}
x=s_point.x;
}
x=s_point.x;
y=s_point.y-1;
for(;y>pmin+2;y--)
{
int current=dc.GetPixel(x,y);
while((current!=b_color)&&(current!=f_color))
{
dc.SetPixel(x,y,f_color);
x++;
current=dc.GetPixel(x,y);
}
x=s_point.x;
x--;
current=dc.GetPixel(x,y);
while((current!=b_color)&&(current!=f_color))
{
dc.SetPixel(x,y,f_color);
x--;
current=dc.GetPixel(x,y);
}
x=s_point.x;
}
}
1.窗口对用户"左按键"和"右按键"事件的响应
2.实现多边形填充
步骤:
1.运行VC6.0
2.创建工程Fill , (工程类型选择:MFC Wizard (.exe) )
3. 添加 "双击左键"和"单击右键"事件的消息响应
(1).选择菜单"View"->"class Wizard"
(2)在弹出的窗口里
classname: 选择CFillView
Messages: 分别选择 WM_LBUTTONDBLCLK , WM_RBUTTONDOWN
然后Add Function
4.修改代码:
(1) 修改fillview.h文件 :
在类的定义中,加入如下代码:
public:
CPoint spt[7];
void Floodfill(CPoint s_point, int f_color, int b_color) ;
(2)修改fillview.cpp文件
修改OnDraw,OnLButtonDblClk,OnRButtonDown的三个函数
加入一个函数: Floodfill
void CFillView::OnDraw(CDC* pDC)
{
CFillDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CPen newpen(PS_SOLID,1,RGB(255,0,0));
CPen *old=pDC->SelectObject(&newpen);
pDC->TextOut(20,20,"双击鼠标左键, 出现需填充的多边形");
pDC->TextOut(20,50,"进行填充, 需用鼠标右键, 单击多边形内一点, 作为开始填充的种子点");
pDC->SelectObject(old);
}
(2)void CFillView::OnLButtonDblClk(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
RedrawWindow();
CDC* pDC=GetDC();
CPen newpen(PS_SOLID,1,RGB(255,0,0));
CPen *old=pDC->SelectObject(&newpen);
//多边形各顶点的坐标
spt[0]=CPoint(100,100);
spt[1]=CPoint(300,100);
spt[2]=CPoint(250,250);
spt[3]=CPoint(100,250);
spt[4]=CPoint(150,200);
spt[5]=CPoint(90,180);
spt[6]=CPoint(150,150);
spt[7]=CPoint(100,100);
pDC->Polyline(spt,8); //画多边形
pDC->SelectObject(old);
ReleaseDC(pDC);
CView::OnLButtonDblClk(nFlags, point);
}
void CFillView::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CView::OnRButtonDown(nFlags, point);
int fill=RGB(0,255,0);
int boundary=RGB(255,0,0);
Floodfill(point, fill, boundary);
}
/*
s_point 为种子点
f_color是要填充的颜色, b_color是边界的颜色
*/
void CFillView::Floodfill(CPoint s_point, int f_color, int b_color)
{
CWindowDC dc (this);
int x,y,p0,pmin,pmax;
//求多边形的最大最小值
pmin=pmax=spt[0].y;
for(int i=1;i<8;i++)
{
if(spt[i].y<pmin) pmin=spt[i].y;
if(spt[i].y>pmax) pmax=spt[i].y;
}
x=s_point.x;
y=s_point.y;
for(;y<pmax;y++)
{
int current=dc.GetPixel(x,y);
while((current!=b_color)&&(current!=f_color))
{
dc.SetPixel(x,y,f_color);
x++;
current=dc.GetPixel(x,y);
}
x=s_point.x;
x--;
current=dc.GetPixel(x,y);
while((current!=b_color)&&(current!=f_color))
{
dc.SetPixel(x,y,f_color);
x--;
current=dc.GetPixel(x,y);
}
x=s_point.x;
}
x=s_point.x;
y=s_point.y-1;
for(;y>pmin+2;y--)
{
int current=dc.GetPixel(x,y);
while((current!=b_color)&&(current!=f_color))
{
dc.SetPixel(x,y,f_color);
x++;
current=dc.GetPixel(x,y);
}
x=s_point.x;
x--;
current=dc.GetPixel(x,y);
while((current!=b_color)&&(current!=f_color))
{
dc.SetPixel(x,y,f_color);
x--;
current=dc.GetPixel(x,y);
}
x=s_point.x;
}
}
相关文章推荐
- 吉林大学图形学实验课作业边标记填充多边形
- 图形学实验之多边形填充算法
- 三维软件开发笔记---彩色多边形填充
- 【opencv】填充多边形 fillPoly
- 算法系列之十二:多边形区域填充算法--改进的扫描线填充算法
- 算法系列之十二:多边形区域填充算法--递归种子填充算法
- 填充实心多边形(一)
- 算法系列之十二:多边形区域填充算法--几种边标志填充算法 .
- 计算机图形学(二)输出图元_10_多边形填充区_6_前向面与后向面
- cocos2dx中DrawNode画线锯齿和非凸多边形填充
- 算法系列之十二:多边形区域填充算法--扫描线种子填充算法
- 基于扫描种子线算法的多边形区域填充实现
- 【计算机图形2】实验2: 扫描线填充算法+Z-buffer被00覆盖点消除
- 矢量数据栅格化——多边形填充
- 计算机图形学(三)扫描线多边形填充算法讲解与源代码
- 计算机图形学-多边形填充法
- 多边形的扫描转换与区域填充
- simple polygon fill program(简单多边形填充程序)
- 多边形区域填充算法--扫描线种子填充算法
- OpenGL-扫描多边形填充算法