查找算法

//查找
//顺序查找 通过在0号位设监视哨免去越界检查
int sequentialSearchBySurveillancePost(vector<int>& v1, int val) {
	v1[0] = val;
	int i = -1;
	for (i = v1.size() - 1; v1[i] != val;--i);
	return i;//返回值若为0则说明未找到
}

void test21() {
	vector<int> vec{ -1,2,3,4,5,6,7,8,9,10 };
	int pos = sequentialSearchBySurveillancePost(vec,2);
	if (pos == 0) cout << "not find" << endl;
	else cout << pos << endl;
}

//折半查找法 (即二分查找法):数据应提前排好序
int foidInHalfSearch(vector<int>& v1, int val){
	int low = 0; int high = v1.size() - 1;
	int mid = (low + high) / 2;
	while (low < high) {
		if (v1[mid] == val) return mid;
		if (val < v1[mid]) high = mid - 1;
		else if (val > v1[mid]) low = mid + 1;
		mid = (low + high) / 2;
	}
}

void test22() {
	vector<int> vec{ -1,2,3,4,5,6,7,8,9,10 };
	int pos = foidInHalfSearch(vec, 3);
}

func:
//1.激活zw文件
int userActivateFile(QString& inName)
{
QString qsName1 = getActiveName();
if (inName == “” || inName.indexOf(“*”) >= 0) {//不合法的名称跳过
return 0;
}
int count, err; vxLongName *Names = NULL; QString qname, qfile;

//内存中的文件,大小写都支持
vxLongName Name, zw_oldName; QString csIntext = inName;
int nBytes = 254;
vxLongName cName; QString oldName;
int isSession = 0;
csIntext = csIntext.toUpper();//转换大写
if (csIntext.indexOf(".PRT") >= 0)csIntext.replace(".PRT", ".Z3PRT");
if (csIntext.indexOf(".ASM") >= 0)csIntext.replace(".ASM", ".Z3ASM");
if (csIntext.indexOf(".DRW") >= 0)csIntext.replace(".DRW", ".Z3DRW");
err = cvxFileLoadList(0, &count, &Names);
for (int i = 0; i < count; i++)
{
	qname = Names[i];
	qname = qname.toUpper();//用大写比较
	if (csIntext == qname)
	{
		strcpy(zw_oldName, Names[i]);//oldName是记录真实的大小写,用于下面的激活
		isSession = 1;
		break;
	}
}
cvxMemFree((void**)&Names);
qfile = csIntext;
if (isSession)
{
	vxDispState dispstate;

	//cvxDispState(0, dispstate);//禁用显示
	err = cvxRootActivate2("*", NULL);//修改退回机制,
									  //cvxDispState(2, dispstate);//禁用显示
									  //char *intext = QstringToChar(csIntext);//带后缀
									  //char *intext2 = QstringToChar(csIntext.left(csIntext.indexOf(".")));//带后缀
	vxLongName cname, cname2;
	strcpy(cname, QstringToChar(csIntext.toLower()));
	//err = cvxFileActivate(cname);//这个api对大小写敏感,如果不完全相同,会创建一个  这个API一定要用数组,用指针有可能出错
	strcpy(cname2, QstringToChar(csIntext.left(csIntext.indexOf(".")).toLower()));
	//err = cvxRootActivate2(cname, cname2);//这个api对大小写敏感,如果不完全相同,会创建一个  这个API一定要用数组,用指针有可能出错
	//------------------------------------------------------------------------
	qfile = zw_oldName;
	strcpy(cname, QstringToChar(qfile));
	strcpy(cname2, QstringToChar(qfile.left(qfile.indexOf("."))));
	err = cvxRootActivate2(cname, cname2);
	return 0;
}

#if 1 //这种方式,不定时把主窗口关闭,要提问题个中望
//如果后台打开,
vxPath filepath; vxLongName cname5, cname6;
cvxFileInqActive(cName, 255);//这个带后缀(全部是大写)
cvxPathFind(cName, filepath);
strcpy(cname5, QstringToChar(qfile));
strcpy(cname6, QstringToChar(qfile.left(qfile.indexOf(“.”))));
string strFilepath = filepath;
QString qsPath = QString::fromLocal8Bit(filepath);
if (qsPath.indexOf(“.Z3”) != -1)
{
int pos = qsPath.lastIndexOf(“\”);
QString newPath = qsPath.left(pos);
cvxPathSearchFirst(QstringToChar(newPath));
}
else
{
cvxPathSearchFirst(QstringToChar(qsPath));
}

err = cvxRootActivate2(cname5, cname6);
if (err == 1)
	return -1;
return 0;

#endif
//打开后,还需要再次几何文件
err = cvxFileOpen(QstringToChar(csIntext));
if (err == 0)
{
char *intext = QstringToChar(csIntext);
vxLongName cname;
strcpy(cname, intext);
err = cvxFileActivate(cname);//这个api对大小写敏感,如果不完全相同,会创建一个 这个API一定要用数组,用指针有可能出错
return 0;
}
return -1;
}

///2.把输入的三角形细分为小于指定面积的三角形
bool TMath::subdivideTriangle(TMathTriangle triang, double maxArea,vector & triangles_out)
{
double area = triang.GetAreaHeron();
if (triang.GetAreaHeron() < maxArea)
{
triangles_out.push_back(triang);
return true;
}
double p0[3] = { 0 };
double p1[3] = { 0 };
double p2[3] = { 0 };
memcpy(p0, triang.points[0],sizeof(double[3]));
memcpy(p1, triang.points[1], sizeof(double[3]));
memcpy(p2, triang.points[2], sizeof(double[3]));

//取三条边的中点进行划分
//p1,p2的中点
double a1 = triang.points[0][0];//test
double a2 = triang.points[1][0];//test
double a3 = (a1 + a3) / 2;//test
double mid0[3] = { (triang.points[0][0] + triang.points[1][0])/2,(triang.points[0][1] + triang.points[1][1])/2,(triang.points[0][2] + triang.points[1][2])/2 };
//p2,p3的中点
double mid1[3] = { (triang.points[1][0] + triang.points[2][0])/2,(triang.points[1][1] + triang.points[2][1])/2,(triang.points[1][2] + triang.points[2][2])/2 };
//p1,p3条边的中点
double mid2[3] = { (triang.points[0][0] + triang.points[2][0])/2,(triang.points[0][1] + triang.points[2][1])/2,(triang.points[0][2] + triang.points[2][2])/2 };

//得到4个三角形
TMathTriangle Triangle1(triang.points[0], mid0, mid2);
double area1 = Triangle1.GetAreaHeron();
if (Triangle1.GetAreaHeron() <= maxArea)
{
	triangles_out.push_back(Triangle1);
}
else
{
	subdivideTriangle(Triangle1, maxArea, triangles_out);
}

TMathTriangle Triangle2(triang.points[1], mid0, mid1);
double area2 = Triangle1.GetAreaHeron();
if (Triangle2.GetAreaHeron() <= maxArea)
{
	triangles_out.push_back(Triangle2);
}
else
{
	subdivideTriangle(Triangle2, maxArea, triangles_out);
}

TMathTriangle Triangle3(triang.points[2], mid1, mid2);
double area3 = Triangle1.GetAreaHeron();
if (Triangle3.GetAreaHeron() <= maxArea)
{
	triangles_out.push_back(Triangle3);
}
else
{
	subdivideTriangle(Triangle3, maxArea, triangles_out);
}

TMathTriangle Triangle4(mid0, mid1, mid2);
double area4 = Triangle1.GetAreaHeron();
if (Triangle4.GetAreaHeron() <= maxArea)
{
	triangles_out.push_back(Triangle4);
}
else
{
	subdivideTriangle(Triangle4, maxArea, triangles_out);
}

return true;

}

//求两条线段重合长度
double TMath::GetTwoLineIncludeDist(double line1_end[2][3], double line2_end[2][3])
{
if (!IsTwoLineParallel(line1_end, line2_end)) return 0;

double l1 = TMath::Twod3Dist(line1_end[0], line1_end[1]);
double l2 = TMath::Twod3Dist(line2_end[0], line2_end[1]);

double L = D_MIN;
if (l1 > l2)
{
	L = l1;
}
else
{
	L = l2;
}
for (int i = 0; i < 2; i++)
{
	for (int j = 0; j < 2; j++)
	{
		double d = TMath::Twod3Dist(line1_end[i], line2_end[j]);
		if (d > L)
		{
			L = d;
		}
	}
}
double x = l1 + l2 - L;
if (x < 0)
{
	return 0;
}
return x;

}

//求轮廓干涉体积:
double TMath::GetTwoBoxIntersectVolume(double iOutLine1[2][3], double iOutLine2[2][3])
{
double XLine1[2][3] = { iOutLine1[0][0],0,0, iOutLine1[1][0],0,0 };
double YLine1[2][3] = { 0,iOutLine1[0][1],0, 0,iOutLine1[1][1],0 };
double ZLine1[2][3] = { 0,0,iOutLine1[0][2], 0,0,iOutLine1[1][2] };

double XLine2[2][3] = { iOutLine2[0][0],0,0, iOutLine2[1][0],0,0 };
double YLine2[2][3] = { 0,iOutLine2[0][1],0, 0,iOutLine2[1][1],0 };
double ZLine2[2][3] = { 0,0,iOutLine2[0][2], 0,0,iOutLine2[1][2] };

//计算出X轴方向投影的长度
double xLength = GetTwoLineIncludeDist(XLine1, XLine2);
//计算出Y轴方向的投影长度
double yLength = GetTwoLineIncludeDist(YLine1, YLine2);
//计算出Y轴方向的投影长度
double zLength = GetTwoLineIncludeDist(ZLine1, ZLine2);
if (xLength < 0.01 || yLength < 0.01 || zLength < 0.01)	return 0;
double val = xLength*yLength*zLength;
return val;

}

//输入三点确定一个圆,得到该圆的信息
bool TMath::GetCirCleByThreePnt(double pnt1[3], double pnt2[3], double pnt3[3], double oCentPnt[3], double& oRad)
{
double m_X1 = pnt1[0]; double m_Y1 = pnt1[1]; double m_Z1 = pnt1[2];
double m_X2 = pnt2[0]; double m_Y2 = pnt2[1]; double m_Z2 = pnt2[2];
double m_X3 = pnt3[0]; double m_Y3 = pnt3[1]; double m_Z3 = pnt3[2];

double A = 0.5*(m_X1*m_X1 - m_X2*m_X2 + m_Y1*m_Y1 - m_Y2*m_Y2 + m_Z1*m_Z1 - m_Z2*m_Z2);
double B = 0.5*(m_X1*m_X1 - m_X3*m_X3 + m_Y1*m_Y1 - m_Y3*m_Y3 + m_Z1*m_Z1 - m_Z3*m_Z3);
double a1 = m_X1 - m_X2;
double b1 = m_Y1 - m_Y2;
double c1 = m_Z1 - m_Z2;

double a2 = m_X1 - m_X3;
double b2 = m_Y1 - m_Y3;
double c2 = m_Z1 - m_Z3;

double E = b2*a1 - b1*a2;
double F = a2*b1 - a1*b2;
double G = b2*A - b1*B;
double H = b1*c2 - b2*c1;
double J = a2*A - a1*B;
double K = a1*c2 - a2*c1;


double E1 = a1*c2 - a2*c1;
double F1 = a2*c1 - a1*c2;
double G1 = c2*A - c1*B;
double H1 = b2*c1 - b1*c2;
double J1 = a2*A - a1*B;
double K1 = a1*b2 - a2*b1;

double E2 = b1*c2 - b2*c1;
double F2 = b2*c1 - b1*c2;
double G2 = c2*A - c1*B;
double H2 = a2*c1 - a1*c2;
double J2 = b2*A - b1*B;
double K2 = a2*b1 - a1*b2;
double x = -99.9, y = -99.9, z = -99.9;
if (abs(E) > 0.0001&&abs(F) > 0.0001)
{
	z = (2 * m_X1*H / E + 2 * m_Y1*K / F + 2 * m_Z1 - 2 * G*H / (E*E) - 2 * J*K / (F*F)) / (2 * (H*H / (E*E) + K*K / (F*F) + 1));
	x = (G + H*z) / E;
	y = (J + K*z) / F;
}
else
{
	if (abs(E1) > 0.0001&&abs(F1) > 0.00001)
	{
		y = (2 * m_X1*H1 / E1 + 2 * m_Z1*K1 / F1 + 2 * m_Y1 - 2 * G1*H1 / (E1*E1) - 2 * J1*K1 / (F1*F1)) / (2 * (H1*H1 / (E1*E1) + K1*K1 / (F1*F1) + 1));
		x = (G1 + H1*y) / E1;
		z = (J1 + K1*y) / F1;
	}
	else
	{
		if (abs(E2) > 0.0001&&abs(F2) > 0.0001)
		{
			x = (2 * m_Y1*H2 / E2 + 2 * m_Z1*K2 / F2 + 2 * m_X1 - 2 * G2*H2 / (E2*E2) - 2 * J2*K2 / (F2*F2)) / (2 * (H2*H2 / (E2*E2) + K2*K2 / (F2*F2) + 1));
			y = (G2 + H2*x) / E2;
			z = (J2 + K2*x) / F2;
		}
		else
		{
			//三点共线
			oRad = -1;
			return false;
		}
	}
}
oCentPnt[0] = x;
oCentPnt[1] = y;
oCentPnt[2] = z;

oRad = Twod3Dist(oCentPnt, pnt1);
return true;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值