多边形有效边表填充算法

绘制多边形时用桶结构,其实类似于图里面的邻接表,从上到下是桶(bucket),右边链接的指针是边(edge).

class Bucket  //桶类

{

public:

    Bucket();

    virtual ~Bucket();

    int ScanLine;

    AET *p;//桶上的边表指针

    Bucket *next;

};

class Edge  //边类

{

public:

    Edge();

    virtual ~Edge();

    double x;

    int yMax;

    double k;//代替1/k

    Edge *next;

};

边链表E[]中保存的是: yMax表示一条线段的下面点的Y,x表示上面点的X(扫描下一条线时会加 1/k, 其中k为斜率)

 

绘制扫描线时,有时扫描线被分为几段,但每次都是取二个点并绘制(Y0中的L1,L2)如下图所示:

 

源代码如下:

// TestView.cpp : implementation of the CTestView class

//

 

#include "stdafx.h"

#include "Test.h"

 

#include "TestDoc.h"

#include "TestView.h"

 

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

#define ROUND(a) int(a+0.5)//四舍五入

/

// CTestView

 

IMPLEMENT_DYNCREATE(CTestView, CView)

 

BEGIN_MESSAGE_MAP(CTestView, CView)

    //{ {AFX_MSG_MAP(CTestView)

    ON_COMMAND(ID_MENUAET, OnMenuAET)

    //}}AFX_MSG_MAP

    // Standard printing commands

    ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)

    ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)

    ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)

END_MESSAGE_MAP()

 

/

// CTestView construction/destruction

 

CTestView::CTestView()

{

    // TODO: add construction code here

    //设置多边形的7个顶点

    Point[0]=CPoint(550,400);//P0

    Point[1]=CPoint(350,600);//P1

    Point[2]=CPoint(250,350);//P2

    Point[3]=CPoint(350,50);//P3

    Point[4]=CPoint(500,250);//P4

    Point[5]=CPoint(600,50);//P5

    Point[6]=CPoint(800,450);//P6

}

 

CTestView::~CTestView()

{

 

}

 

BOOL CTestView::PreCreateWindow(CREATESTRUCT& cs)

{

    // TODO: Modify the Window class or styles here by modifying

    //  the CREATESTRUCT cs

 

    return CView::PreCreateWindow(cs);

}

 

/

// CTestView drawing

 

void CTestView::OnDraw(CDC* pDC)

{

    CTestDoc* pDoc = GetDocument();

    ASSERT_VALID(pDoc);

  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
#include #include #include #include #include //////////////////////////////////////////////////////////////functions///////////////////////////////////////////////// void swap(float &m,float &n) { float temp=n; n=m; m=temp; } int sign(float a,float b)//sign() { int t; if(a>b) { t=1;} else if(adx) { swap(dx,dy); Flag=1; } else Flag=0; float Nerror=2*dy-dx; for(int i=1;i=0) { if(Flag) { x=x+sx;} else y=y+sy; Nerror=Nerror-2*dx; } if(Flag) { y=y+sy;} else x=x+sx; Nerror=Nerror+2*dy; } } ///////////////////////////////////四连通种子填充/////////////////////////////////////////////////// void BoundaryFill4(HDC hdc,int x,int y,int FilledColor,int BoundaryColor) { int CurrentColor; CurrentColor=GetPixel(hdc,x,y); if(CurrentColor!=BoundaryColor&&CurrentColor!=FilledColor) { SetPixel(hdc,x,y,FilledColor); BoundaryFill4(hdc,x+1,y,FilledColor,BoundaryColor); BoundaryFill4(hdc,x-1,y,FilledColor,BoundaryColor); BoundaryFill4(hdc,x,y+1,FilledColor,BoundaryColor); BoundaryFill4(hdc,x,y-1,FilledColor,BoundaryColor); } } ////////////////////////////////////////扫描线填充/////////////////////////////////////////////////// //DrawLine()函数:在(x1,y)和(x2,y)两点之间画一条颜色为FilledColor的横线(用来扫描填充) void drawline(HDC hdc, int x1, int x2,int y0, int FilledColor) { for(int n=x1+1;n<x2;n++) { SetPixel(hdc,n,y0,FilledColor); } } //Scan()函数:扫描线函数,将扫描线与图形的交点坐标存在数组中 //数组中同行的点即为该行扫描线与图形的交点(一般为2个) //数组中的行代表扫描线的纵坐标 void scan(HDC hdc, int boundarycolor) { int currentcolor; int a[300][2]={0}; for (int j=0;j<300;j++) { for(int i=300;i<700;i++) { currentcolor=GetPixel(hdc,i,j); if((currentcolor==boundarycolor)&&(GetPixel(hdc,i+1,j)!=boundarycolor)&&(i500)) {a[j][1]=i;} } } //利用循环调用DrawLine函数逐行填充两交点之间的点 for(int k=0;k<300;k++) { if((a[k][0]!=0)&&(a[k][1]!=0)) drawline(hdc,a[k][0],a[k][1],k,RGB(255,0,0));} } ///////////////////////////////////////////////边界填充////////////////////////////////////// //Contrary()取反函数:如果点的颜色为白,则将点置为填充色;如果点的颜色为填充色,则将点置为白色 //忽略了边界色,即不对边界点作颜色处理 void contrary(HDC hdc, int x, int y,int FilledColor) { for(int h=x;h<280;h++) { if(GetPixel(hdc,h,y)==RGB(255,255,255)) { SetPixel(hdc,h,y,FilledColor); } else if(GetPixel(hdc,h,y)==FilledColor) { SetPixel(hdc,h,y,RGB(255,255,255)); } } } //borderline()边线函数: 先找出图形的边界 左边和右边,从右到左的顺序调用contrary()函数进行填充 void borderline(HDC hdc, int boundarycolor) { for(int j=280;j<499;j++) { for(int i=80;i100)) { contrary(hdc,i,j,RGB(0,0,255)); } } } for(int m=280;m<499;m++) { for(int n=80;n<280;n++) { int currentcolor=GetPixel(hdc,n,m); if((currentcolor==boundarycolor)&&(GetPixel(hdc,n+1,m)!=boundarycolor)&&(GetPixel(hdc,n-1,m)!=boundarycolor)&&(n<101)) { contrary(hdc,n,m,RGB(0,0,255)); } } } }
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值