C++是在C语言的基础上开发的一种面向对象编程语言,应用非常广泛。常用于系统开发,引擎开发等应用领域,支持类、封装、继承、多态等特性。C++语言灵活,运算符的数据结构丰富、具有结构化控制语句、程序执行效率高欢迎大家在这里学习C++多边形扫描转换算法!这里是我们给大家整理出来的精彩内容。
MFC还不是太明白,主要还是使用OnDraw函数...
大家多指教...
void CPolyFillView::OnDraw(CDC* pDC)
{
CPolyFillDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
const int POINTNUM=6; //多边形点数.
/定义结构体用于活性边表AET和新边表NET/
typedef struct XET
{
float x;
float dx,ymax;
XET* next;
}AET,NET;
/定义点结构体point*/
struct point
{
float x;
float y;
}polypoint[POINTNUM]={250,50,550,150,550,400,250,250,100,350,100,100};//多边形顶点
//mypoint[POINTNUM]={100,100,200,100,200,200,100,200};//正方形
/计算最高点的y坐标(扫描到此结束)**/
int MaxY=0;
int i;
for(i=0;i
if(polypoint.y>MaxY)
MaxY=polypoint.y;
/*初始化AET表/
AET *pAET=new AET;
pAET->next=NULL;
/初始化NET表*/
NET *pNET[1024];
for(i=0;i<=MaxY;i++)
{
pNET=new NET;
pNET->next=NULL;
}
/扫描并建立NET表*/
for(i=0;i<=MaxY;i++)
{
for(int j=0;j
if(polypoint[j].y==i)
{
if(polypoint[(j-1+POINTNUM)%POINTNUM].y>polypoint[j].y)
{
NET *p=new NET;
p->x=polypoint[j].x;
p->ymax=polypoint[(j-1+POINTNUM)%POINTNUM].y;
p->dx=(polypoint[(j-1+POINTNUM)%POINTNUM].x-polypoint[j].x)/(polypoint[(j-1+POINTNUM)%POINTNUM].y-polypoint[j].y);
p->next=pNET->next;
pNET->next=p;
}
if(polypoint[(j+1+POINTNUM)%POINTNUM].y>polypoint[j].y)
{
NET *p=new NET;
p->x=polypoint[j].x;
p->ymax=polypoint[(j+1+POINTNUM)%POINTNUM].y;
p->dx=(polypoint[(j+1+POINTNUM)%POINTNUM].x-polypoint[j].x)/(polypoint[(j+1+POINTNUM)%POINTNUM].y-polypoint[j].y);
p->next=pNET->next;
pNET->next=p;
}
}
}
/建立并更新活性边表AET/
for(i=0;i<=MaxY;i++)
{
//计算新的交点x,更新AET/
NET *p=pAET->next;
while(p)
{
p->x=p->x + p->dx;
p=p->next;
}
//更新后新AET先排序
好了,C++多边形扫描转换算法内容就给大家介绍到这里了。希望大家继续关注我们的网站!
最近学习计算机图形学,写了个多边形扫描转换算法,贴出来分享一下;
MFC还不是太明白,主要还是使用OnDraw函数...
大家多指教...
void CPolyFillView::OnDraw(CDC* pDC)
{
CPolyFillDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
const int POINTNUM=6; //多边形点数.
/定义结构体用于活性边表AET和新边表NET/
typedef struct XET
{
float x;
float dx,ymax;
XET* next;
}AET,NET;
/定义点结构体point*/
struct point
{
float x;
float y;
}polypoint[POINTNUM]={250,50,550,150,550,400,250,250,100,350,100,100};//多边形顶点
//mypoint[POINTNUM]={100,100,200,100,200,200,100,200};//正方形
/计算最高点的y坐标(扫描到此结束)**/
int MaxY=0;
int i;
for(i=0;i<POINTNUM;i++)
if(polypoint.y>MaxY)
MaxY=polypoint.y;
/*初始化AET表/
AET *pAET=new AET;
pAET->next=NULL;
/初始化NET表*/
NET *pNET[1024];
for(i=0;i<=MaxY;i++)
{
pNET=new NET;
pNET->next=NULL;
}
/扫描并建立NET表*/
for(i=0;i<=MaxY;i++)
{
for(int j=0;j<POINTNUM;j++)
if(polypoint[j].y==i)
{
if(polypoint[(j-1+POINTNUM)%POINTNUM].y>polypoint[j].y)
{
NET *p=new NET;
p->x=polypoint[j].x;
p->ymax=polypoint[(j-1+POINTNUM)%POINTNUM].y;
p->dx=(polypoint[(j-1+POINTNUM)%POINTNUM].x-polypoint[j].x)/(polypoint[(j-1+POINTNUM)%POINTNUM].y-polypoint[j].y);
p->next=pNET->next;
pNET->next=p;
}
if(polypoint[(j+1+POINTNUM)%POINTNUM].y>polypoint[j].y)
{
NET *p=new NET;
p->x=polypoint[j].x;
p->ymax=polypoint[(j+1+POINTNUM)%POINTNUM].y;
p->dx=(polypoint[(j+1+POINTNUM)%POINTNUM].x-polypoint[j].x)/(polypoint[(j+1+POINTNUM)%POINTNUM].y-polypoint[j].y);
p->next=pNET->next;
pNET->next=p;
}
}
}
/建立并更新活性边表AET/
for(i=0;i<=MaxY;i++)
{
//计算新的交点x,更新AET/
NET *p=pAET->next;
while(p)
{
p->x=p->x + p->dx;
p=p->next;
}
//更新后新AET先排序
欢迎光临 信息发布软件,b2b软件,广告发布软件 (http://postbbs.com/) | Powered by Discuz! X3.2 |