基于MFC的圆环的消隐实现

基于 M F C MFC MFC的圆环的消隐实现

本文是在上一篇博客基于 M F C MFC MFC的凸多面体的消隐实现的基础上实现。

  • 添加头文件#include"math.h"
  • 设置圆环的绘制 I D ID ID I D _ Y u a n h u a n ID\_Yuanhuan ID_Yuanhuan,添加事件相应程序 O n Y u a n h u a n ( ) OnYuanhuan() OnYuanhuan()
  • 实现代码
void CDrawConvexPolygonView::OnYuanhuan(){
	// TODO: 在此添加命令处理程序代码
	CClientDC *pdc = new CClientDC(this);
	CPen *pen1 = new CPen(PS_SOLID, 1, RGB(0, 0XFF, 0));
	CPen *pen2 = new CPen(PS_SOLID, 1, RGB(255, 0, 0));
	CPen *OldPen = pdc->SelectObject(pen1);
	CBrush brush;
	brush.CreateSolidBrush(RGB(0, 0, 0));
	CBrush *oldbrush = (CBrush*)pdc->SelectObject(&brush);
	int flag, k1, k2, r1, r2, n, d, m, p;
	int xs1, xs2, xs3, xs4, ys1, ys2, ys3, ys4, i, j, lastp;
	double x, y, z, thx, th1, th3, yw, zw, xw, thy, th2;
	double PI, ed, od, eh, zzw, ppw;
	double xs[50][17], zs[50][17], ys[50][17], zc[50][17], x1, y1;
	int zz[850], pp[850];
	r1 = 120;
	r2 = 40;
	k1 = 20;
	k2 = 16;
	ed = 1500;
	eh = 0;
	od = 0;
	n = 0;
	PI = 3.14159;
	th3 = 1;
	thx = 0.9;
	for (d = -1; d <= 1; d += 2) {
		for (th1 = 0; th1 <= 2 * PI + 0.1; th1 += 2 * PI / k1) {
			n = n + 1;
			m = 0;
			for (th2 = 0; th2 <= 2 * PI + 0.1; th2 += 2 * PI / k2) {
				m = m + 1;
				x = r1 + r2 * cos(th2);
				y = r2 * sin(th2);
				z = 0;
				thy = th1;
				zw = z;
				xw = x;
				x = zw * cos(thy) - xw * sin(thy);
				z = zw * sin(thy) + xw * cos(thy);
				x = x + r1 / 2 * d;
				if (d == 1) {
					yw = y;
					zw = z;
					y = yw * cos(PI / 2) - zw * sin(PI / 2);
					z = yw * sin(PI / 2) + zw * cos(PI / 2);
				}
				thy = th3;
				zw = z;
				xw = x;
				x = zw * cos(thy) - xw * sin(thy);
				z = zw * sin(thy) + xw * cos(thy);
				yw = y;
				zw = z;
				y = yw * cos(thx) - zw * sin(thx);
				z = yw * sin(thx) + zw * cos(thx);
				x = x * ed / (ed - od - z);
				y = (y*ed - eh * (od + z)) / (ed - od - z);
				xs[n][m] = x;
				ys[n][m] = y;
				zs[n][m] = z;
			}
			flag = 0;
		}
		flag = 0;
	}
	p = 0;
	for (n = 1; n <= k1; n += 1) {
		for (m = 1; m <= k2; m += 1) {
			zc[n][m] = int((zs[n][m] + zs[n + 1][m + 1]) / 2);
			zz[p] = zc[n][m];
			pp[p] = p;
			p = p + 1;
		}
	}
	lastp = p - 1;
	for (i = 2; i <= lastp; i += 1) {
		for (j = i - 1; j >= 0; j += -1) {
			if (zz[j] > zz[j + 1]) {
				zzw = zz[j];
				zz[j] = zz[j + 1];
				zz[j + 1] = zzw;
				ppw = pp[j];
				pp[j] = pp[j + 1];
				pp[j + 1] = ppw;
			}
		}
	}
	for (p = 0; p <= lastp; p += 1) {
		n = int(pp[p] / k2) + 1;
		m = pp[p] % k2 + 1;
		if (n != (k1 + 1)) {
			xs1 = int(xs[n][m]);
			ys1 = int(ys[n][m]);
			xs2 = int(xs[n + 1][m]);
			ys2 = int(ys[n + 1][m]);
			xs3 = int(xs[n + 1][m + 1]);
			ys3 = int(ys[n + 1][m + 1]);
			xs4 = int(xs[n][m + 1]);
			ys4 = int(ys[n][m + 1]);
		}
		if (abs(ys1*(xs2 - xs3) + ys2 * (xs3 - xs1) + ys3 * (xs1 - xs2)) > 80){
			pdc->MoveTo(xs1 + 320, ys1 + 200);
			pdc->SelectObject(pen1);
			pdc->LineTo(xs2 + 320, ys2 + 200);
			pdc->LineTo(xs3 + 320, ys3 + 200);
			pdc->LineTo(xs4 + 320, ys4 + 200);
			pdc->LineTo(xs1 + 320, ys1 + 200);
			x = (xs[n][m] + xs[n + 1][m + 1]) / 2;
			y = (ys[n][m] + ys[n + 1][m + 1]) / 2;
			x1 = int(x + 320);
			y1 = int(y + 200);
			pdc->ExtFloodFill(x1, y1, RGB(0, 255, 0), 0);//条件语句结束
		}
		pdc->MoveTo(xs1 + 320, ys1 + 200);
		pdc->SelectObject(pen2);
		pdc->LineTo(xs2 + 320, ys2 + 200);
		pdc->LineTo(xs3 + 320, ys3 + 200);
		pdc->LineTo(xs4 + 320, ys4 + 200);
		pdc->LineTo(xs1 + 320, ys1 + 200);
	}
	pdc->SelectObject(OldPen);
	delete pen1;
	delete pen2;
	pdc->DeleteDC();
}
		

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值