填充正六边形

填充正六边形

扫描线法

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)
{										//计算y值
	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;				//edges是指针数组
	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);  	//调用fillScan
			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);
	// TODO: add draw code for native data here
}

画线法

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);
				//pDC->LineTo(x2,y2);
				//以上LineTo两行任选其一
			}
		}
	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);
				//pDC->LineTo(x4,y4);
				//以上LineTo两行任选其一
			}
			
		}
}

画点法

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);
		}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值