多边形有效边表填充算法

绘制多边形时用桶结构,其实类似于图里面的邻接表,从上到下是桶(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);