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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/fuliangliang/article/details/524246

//*************************
//获取点中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()
{
      if(head == NULL || head->next == NULL) return;
   Edge *p,*q = head;
      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)
{
 if(head == NULL) return;
 if(head->next==NULL)
 {
  if(head->ymax = ymax)
   head = NULL;
 }
 else
 {
  Edge *p = head,*q = head->next,*r = head;
  while(head!=NULL && head->ymax == ymax  )
  {
   
   head = head->next;
   r->next = NULL;
   r = head;
  }
  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;
     }
        }
 } 
}
//**************************************
// 向活动边表中添加的边
//**************************************
void CPolygonFillView::AddEdges(int index)
{
  if(edgeTable[index].head == NULL) return;
  if(head == NULL)
  {
   head = edgeTable[index].head;
  }
  else
  {
    for(Edge *p=head;p->next!=NULL;p=p->next){}
    p->next = edgeTable[index].head;
  }
}
//*****************************
//初始化边表
//*****************************
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;
  }
               if((p=edgeTable[ymin].head)==NULL)
  {
   edgeTable[ymin].head = &edge[i];
  }
  else
  {
                  if(edge[i].xmin<=p->xmin)
     {
      edge[i].next = p;
         edgeTable[ymin].head = &edge[i];
     }
     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++)
 {
           AddEdges(i);
           SortEdges();
    q=head;
    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);
    q=head;
    while(q!=NULL)
    {
     q->xmin += q->m;
     q = q->next;
    }
 }
}

展开阅读全文

没有更多推荐了,返回首页