C + + polygon scan conversion algorithm

Source: Internet
Author: User

 Recently learned computer graphics, wrote a polygon scan conversion algorithm, posted out to share;

MFC is not too clear, mainly using the OnDraw function ...

Please advise ...

void Cpolyfillview::ondraw (cdc* pDC)

{

cpolyfilldoc* PDoc = GetDocument ();

Assert_valid (PDOC);

Todo:add Draw code for native

const int pointnum=6; Polygon points.

/define the structure body for active side table aet and new edge table net/

typedef struct XET

{

float x;

float Dx,ymax;

Xet* Next;

}aet,net;

/definition point Structure body point*/

struct POINT

{

float x;

Float y;

}polypoint[pointnum]={250,50,550,150,550,400,250,250,100,350,100,100};//Polygon Vertices

mypoint[pointnum]={100,100,200,100,200,200,100,200};//Square

/compute the y-coordinate of the highest point (scan to this end) **/

int maxy=0;

int i;

for (i=0;i<pointnum;i++)

if (Polypoint[i].y>maxy)

MAXY=POLYPOINT[I].Y;

/* Initialize AET table/

AET *paet=new AET;

paet->next=null;

/Initialization NET Table * *

NET *pnet[1024];

for (i=0;i<=maxy;i++)

{

Pnet[i]=new NET;

pnet[i]->next=null;

}

/Scan and build net table * *

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[i]->next;

pnet[i]->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[i]->next;

pnet[i]->next=p;

}

}

}

/Establish and update the active side table aet/

for (i=0;i<=maxy;i++)

{

Compute new intersection x, update aet/

NET *p=paet->next;

while (p)

{

P->x=p->x + p->dx;

p=p->next;

}

New AET first sorted after update

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.