# MFC下实现图形学之多边形扫描转化填充算法

//*************************
//获取点中y坐标最大值
//*************************
int CPolygonFillView::GetMaxY()
{
int result = points[0].y;
for(int i = 1; i < count; i++)
if(result < points[i].y)
result = points[i].y;
return result;
}
//*************************
//获取点中y坐标最小值
//*************************
int CPolygonFillView::GetMinY()
{
int result = points[0].y;
for(int i = 0; i < count; i++)
if(result > points[i].y)
result = points[i].y;
return result;
}
//*******************************************************************************
//判断是否为极值点,参数为一条边的y较小点和其在pionts数组中的下标
//*******************************************************************************
bool CPolygonFillView::IsSuperPoint(CPoint &point,int i)
{
if(i == 0)
return ((points[0].y > points[1].y&&points[0].y > points[count-1].y)
||(points[0].y < points[1].y&& points[0].y < points[count-1].y));
else if(i == count-1)
return ((points[i].y > points[0].y&&points[i].y > points[i-1].y)
||(points[i].y < points[0].y&&points[i].y < points[i-1].y));
else
return ((points[i].y > points[i-1].y&&points[i].y > points[i+1].y)
||(points[i].y < points[i-1].y&&points[i].y < points[i+1].y));
}
//*************************
//用改进的冒泡法对链表排序
//*************************
void CPolygonFillView::SortEdges()
{
float tymax;
float txmin;
float tm;
bool flag = true;
while(flag)
{
flag = false;
for(p=q;p->next!=NULL;p=p->next)
{
if(p->xmin > p->next->xmin)
{
tymax = p->ymax;
p->ymax = p->next->ymax;
p->next->ymax = tymax;

txmin = p->xmin;
p->xmin = p->next->xmin;
p->next->xmin = txmin;

tm = p->m;
p->m = p->next->m;
p->next->m = tm;
flag = true;
}
q = q->next;
}
}
}
//**************************************
// 从活动边表中清除不满足条件的边
//**************************************
void CPolygonFillView::ClearEdges(int ymax)
{
{
}
else
{
{

r->next = NULL;
}
while(q!=NULL)
{
if(q->ymax == ymax)
{
p->next = q->next;
q->next = NULL;//删除边
q = p->next;//恢复q为恰当的值
}
else
{
if(p==NULL) break;
p = p->next;
if(q==NULL) break;
q = q->next;
}
}
}
}
//**************************************
// 向活动边表中添加的边
//**************************************
{
{
}
else
{
}
}
//*****************************
//初始化边表
//*****************************
void CPolygonFillView::InitEdgeTable()
{
int ymin,t;
Edge *p = NULL;
CPoint cp;
for(int i=0;i<count;i++)
{
cp = points[i].y > points[(i+1)%count].y
? points[(i+1)%count] : points[i];
t = points[i].y > points[(i+1)%count].y
? (i+1)%count : i;
edge[i].m =(float) (points[i].x - points[(i+1)%count].x)/
(points[i].y - points[(i+1)%count].y);

if(IsSuperPoint(cp,t))
{
edge[i].xmin = cp.x;
edge[i].ymax = points[i].y > points[(i+1)%count].y
? points[i].y : points[(i+1)%count].y;
ymin = cp.y;
}
else
{
edge[i].xmin = cp.x + edge[i].m;
edge[i].ymax = points[i].y > points[(i+1)%count].y
? points[i].y : points[(i+1)%count].y;
ymin = cp.y + 1;
}
{
}
else
{
if(edge[i].xmin<=p->xmin)
{
edge[i].next = p;
}
else
{
while(p->next!=NULL&&p->next->xmin<edge[i].xmin)
{
p = p->next;
}
edge[i].next = p->next;
p->next = &edge[i];
}
}
}
}
//******************
//对多边形进行填充
//******************
void CPolygonFillView::Polygonfill(CClientDC &dc,COLORREF color)
{
int begin = GetMinY(),end = GetMaxY();
Edge *q = NULL;
for(int i=begin;i<=end;i++)
{
SortEdges();
if(q==NULL) break;
while(q->next!=NULL)
{
for(int j=q->xmin;j<q->next->xmin;j++)
{
SetPixel(dc,j,i,color);
}
q=q->next->next;
if(q==NULL) break;
}
ClearEdges(i);
while(q!=NULL)
{
q->xmin += q->m;
q = q->next;
}
}
}

06-03 3608

12-06 6467

06-18 359

05-27 1113

01-31 1万+

#### 多边形的扫描转换算法

©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。