fuliangliang的Blog

合抱之木,生于毫末;九层之台,起于累土;千里之行,始于足下。

fuliangliangID:fuliangliang
60297次访问,排名1613好友0人,关注者2
fuliangliang的文章
原创 95 篇
翻译 0 篇
转载 25 篇
评论 24 篇
fuliang的公告

我的联系方式:20542606

Email:fuliangliang@gmail.com


最近评论
tbsc3:我也遇到了这个问题,如果配1 M就有用,大于2M就还是默认的 不知道你有没有解决呀,教教我
zhoufeng345678:Thank u!
lyzhouhailong:很好!
GoEastward:顶下,SHH2,不错的例子,如果能配上分页Book列表的分页显示界面例子就更好了。
uhlanme:写得不错,参考了,呵呵
文章分类
收藏
    相册
    净月潭一日游
    页面图片
    日历
    文章收藏
    我的JavaEye博客
    存档
    软件项目交易
    订阅我的博客
    XML聚合  FeedSky
    订阅到鲜果
    订阅到Google
    订阅到抓虾
    订阅到BlogLines
    订阅到Yahoo
    订阅到GouGou
    订阅到飞鸽
    订阅到Rojo
    订阅到newsgator
    订阅到netvibes

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

    新一篇: MFC下实现图形学之立方体平移、比例、旋转、投影变换算法 | 旧一篇: blog创建一个月了,还算能坚持写点东西,继续努力...

    //*************************
    //获取点中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;
        }
     }
    }

    发表于 @ 2005年11月07日 09:26:00|评论(loading...)|编辑

    评论

    #神仙茶 发表于2006-03-31 15:14:00  IP: 218.76.65.*
    points[0].y;
    这个POINTS在那里定义了?
    count是什么?
    #fuliang 发表于2006-04-16 10:23:00  IP: 202.198.33.*
    用MFC做的,他自己产生的一大堆代码,我都没有贴,只把
    核心的部分贴了上去
    #wbrs13 发表于2007-11-04 20:07:32  IP: 58.216.220.*
    那个POINTS是不是在view类里面 一开始就定义啊???
    发表评论  


    当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击登录
    Csdn Blog version 3.1a
    Copyright © fuliang