扫描线法
struct Edge
{
int yUpper;;
float xIntersect, dxPerScan;
struct Edge * next;
}tEdge;
void insertEdge(Edge *list, Edge *edge)
{
Edge *p, *q = list;
p = q ->next;
while(p != NULL)
{
if (edge ->xIntersect < p->xIntersect)
p = NULL;
else
{
q = p;
p = p ->next;
}
}
edge ->next = q ->next;
q ->next = edge;
}
int yNext(int k, int cnt, CPoint *pts)
{
int j;
if((k + 1) > (cnt - 1))
j = 0;
else
j = k+1;
while(pts[k].y == pts[j].y)
if((j + 1) > (cnt - 1))
j = 0;
else
j++;
return (pts[j].y);
}
void makeEdgeRec(CPoint lower, CPoint upper, int yComp, Edge *edge, Edge *edges[])
{
edge ->dxPerScan = (float)(upper.x - lower.x) / (upper.y - lower.y);
edge ->xIntersect = lower.x;
if(upper.y < yComp)
edge -> yUpper = upper.y - 1;
else
edge ->yUpper = upper.y;
insertEdge(edges[lower.y],edge);
}
void buildEdgeList(int cnt, CPoint *pts, Edge *edges[])
{
Edge *edge;
CPoint v1, v2;
int i, yPrev = pts[cnt - 2].y;
v1.x = pts[cnt - 1].x;
v1.y = pts[cnt - 1].y;
for(i=0; i<cnt; i++)
{
v2 = pts[i];
if(v1.y != v2.y)
{
edge = (Edge* )malloc(sizeof(Edge));
if(v1.y < v2.y)
makeEdgeRec(v1, v2, yNext(i, cnt, pts),edge, edges);
else
makeEdgeRec(v2, v1, yPrev,edge, edges);
}
yPrev = v1.y;
v1 = v2;
}
}
void buildActiveList(int scan, Edge *active, Edge *edges[])
{
Edge *p, *q;
p = edges[scan] ->next;
while(p)
{
q = p -> next;
insertEdge(active,p);
p = q;
}
}
void fillScan(int scan, Edge *active, CDC *pDC)
{
Edge *p1,*p2;
int i;
p1 = active->next;
while(p1)
{
p2 = p1 -> next;
for(i=p1->xIntersect; i<p2->xIntersect; i++)
pDC ->SetPixel(i,scan,0);
p1 = p2->next;
}
}
void deleteAfter(Edge *q)
{
Edge *p = q->next;
q->next = p->next;
free(p);
}
void updateActiveList(int scan, Edge * active)
{
Edge *q = active, *p = active->next;
while(p)
{
if(scan >= p->yUpper)
{
p = p->next;
deleteAfter(q);
}
else
{
p ->xIntersect = p->xIntersect + p->dxPerScan;
q = p;
p = p->next;
}
}
}
void resortActiveList(Edge * active)
{
Edge *q, *p = active ->next;
active ->next = NULL;
while(p)
{
q = p->next;
insertEdge(active,p);
p = q;
}
}
void scanFill(int cnt, CPoint *pts, CDC *pDC)
{
Edge *edges[300], *active;
int i, scan;
for(i=0; i<300; i++)
{
edges[i] = (Edge * )malloc(sizeof(Edge));
edges[i]->next = NULL;
}
buildEdgeList(cnt,pts,edges);
active = (Edge * )malloc(sizeof(Edge));
active ->next = NULL;
for(scan = 0; scan<300; scan++)
{
buildActiveList(scan,active,edges);
if(active->next)
{
fillScan(scan,active,pDC);
updateActiveList(scan,active);
resortActiveList(active);
}
}
}
void CMy2View::OnDraw(CDC* pDC)
{
CMy2Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CPoint V[6]; int cnt = 6;
V[0].x = 200; V[0].y = 100;
V[1].x = 300; V[1].y = 100;
V[2].x = 360; V[2].y = 180;
V[3].x = 300; V[3].y = 260;
V[4].x = 200; V[4].y = 260;
V[5].x = 140; V[5].y = 180;
scanFill(cnt,V,pDC);
}
画线法
void CMyView::OnDraw(CDC* pDC)
{
CMyDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
float x, y;
float x1,x2,x3,x4,x5,x6,y1,y2,y3,y4,y5,y6;
x1 = 200; y1 = 100;
x2 = 300; y2 = 100;
x3 = 360; y3 = 180;
x4 = 300; y4 = 260;
x5 = 200; y5 = 260;
x6 = 140; y6 = 180;
pDC ->MoveTo(x1,y1);
pDC ->LineTo(x2,y2);
pDC ->LineTo(x3,y3);
pDC ->LineTo(x4,y4);
pDC ->LineTo(x5,y5);
pDC ->LineTo(x6,y6);
pDC ->LineTo(x1,y1);
for(x = x6; x <= x3; x++)
for(y = y1; y <= y6; y++)
{
if(y - y1 > (y6 - y1)/ (x6 - x1) * (x- x1) && y - y2 > (y3 - y2)/ (x3 - x2) * (x- x2))
{
pDC->MoveTo(x,y);
pDC->LineTo(x1,y1);
}
}
for(x = x6; x <= x3; x++)
for(y = y6; y <= y5; y++)
{
if(y - y5 < (y6 - y5)/ (x6 - x5) * (x- x5) && y - y4 < (y3 - y4)/ (x3 - x4) * (x- x4))
{
pDC->MoveTo(x,y);
pDC->LineTo(x5,y5);
}
}
}
画点法
void CMyView::OnDraw(CDC* pDC)
{
CMyDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
float x, y;
float x1,x2,x3,x4,x5,x6,y1,y2,y3,y4,y5,y6;
x1 = 200; y1 = 100;
x2 = 300; y2 = 100;
x3 = 360; y3 = 180;
x4 = 300; y4 = 260;
x5 = 200; y5 = 260;
x6 = 140; y6 = 180;
pDC ->MoveTo(x1,y1);
pDC ->LineTo(x2,y2);
pDC ->LineTo(x3,y3);
pDC ->LineTo(x4,y4);
pDC ->LineTo(x5,y5);
pDC ->LineTo(x6,y6);
pDC ->LineTo(x1,y1);
for(x = x6; x <= x3; x++)
for(y = y1; y <= y6; y++)
{
if(y - y1 > (y6 - y1)/ (x6 - x1) * (x- x1) && y - y2 > (y3 - y2)/ (x3 - x2) * (x- x2))
pDC ->SetPixel((int)x, (int)y, 0);
}
for(x = x6; x <= x3; x++)
for(y = y6; y <= y5; y++)
{
if(y - y5 < (y6 - y5)/ (x6 - x5) * (x- x5) && y - y4 < (y3 - y4)/ (x3 - x4) * (x- x4))
pDC ->SetPixel((int)x, (int)y, 0);
}
}