多边形有效边表填充算法

本文介绍了一种使用桶结构和邻接表来实现多边形填充的算法。通过边链表E[]存储线段信息,利用AET(Active Edge Table)进行排序,遍历每个扫描线,动态调整边表,最终实现多边形内部的填充。代码中展示了具体的类定义和填充流程,包括创建桶、构建边表、填充多边形等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

    // TODO: add draw code for native data here

    pDC->Polygon(Point,7);//绘制多边形

    //输出多边形的顶点编号

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值