//该函数在VC++6.0下编译测试通过,可实现直线、圆弧、完整圆的数字积分法插补;并可将插补函数计算出的数//据点写入xxx.txt文件中//若有任何疑问,欢迎邮件联系,dingjiang90@foxmail.com#include//版权所有,侵权必究。转载时请注明来自大连理工机械工程学院Deanjiang#includemath.h#includestdlib.h#includestdio.h/********************************************************************//*函数名:InsertPoint*//*功能:控制机床各轴进给并将进给结果写入文件中*//*参数:doublex,doubley插补点单位:毫米*//*说明:与机床硬件关联,每产生一个点调用一次*//********************************************************************/voidInsertPoint(doublexCur,doubleyCur){externFILE*fp;charch=10;printf(xCur=%f,yCur=%f\n,xCur,yCur);fprintf(fp,%f,%f,xCur,yCur);fputc(ch,fp);}/********************************************************************//*函数名:Judge_Quadrant*//*功能:判断参数坐标的所在象限并返回相应象限值*//*参数:doublexmm*//*doubleymm*//********************************************************************/unsignedshortJudge_Quadrant(doublex,doubley){unsignedshortnDir;if(x=0){//象限判断if(y=0){nDir=1;return1;}else{nDir=4;return4;}}else{if(y=0){nDir=2;return2;}else{nDir=3;return3;}}}/********************************************************************//*函数名:DDA_Line*//*功能:数字积分法直线插补*//*参数:doubleXEnd,doubleYEnd插补终点mm*//*intstep步长mm*//*unsignedshortn寄存器位数*//********************************************************************/voidDDA_Line(unsignedshortn,intstep,doubleXEnd,doubleYEnd){longXRes,YRes;//寄存器溢出后余数longxEnd,yEnd;//插补终点值longXCur=0,YCur=0;//当前位置intIPCount=0;//累加值intnQuadrant;//象限intQ;//累加器容量intbInterpXEnable,bInterpYEnable;XRes=YRes=0;bInterpXEnable=bInterpYEnable=0;xEnd=labs(XEnd);yEnd=labs(YEnd);Q=(fabs(YEnd)+fabs(XEnd))/step;nQuadrant=Judge_Quadrant(XEnd,YEnd);//计算插补象限switch(nQuadrant){case1:while(IPCountQ){XRes+=xEnd/step;YRes+=yEnd/step;if(XRes=Q){XRes=XRes%Q;XCur+=step;bInterpXEnable=1;}if(YRes=Q){YRes=YRes%Q;YCur+=step;bInterpYEnable=1;}if(bInterpXEnable==1||bInterpYEnable==1){InsertPoint(XCur,YCur);bInterpXEnable=bInterpYEnable=0;}IPCount++;}break;case2:while(IPCountQ){XRes+=xEnd/step;YRes+=yEnd/step;if(XRes=Q){XRes=XRes%Q;XCur-=step;bInterpXEnable=1;}if(YRes=Q){YRes=YRes%Q;YCur+=step;bInterpYEnable=1;}if(bInterpXEnable==1||bInterpYEnable==1){InsertPoint(XCur,YCur);bInterpXEnable=bInterpYEnable=0;}IPCount++;}break;case3:while(IPCountQ){XRes+=xEnd/step;YRes+=yEnd/step;if(XRes=Q){XRes=XRes%Q;XCur-=step;bInterpXEnable=1;}if(YRes=Q){YRes=YRes%Q;YCur-=step;bInterpYEnable=1;}if(bInterpXEnable==1||bInterpYEnable==1){InsertPoint(XCur,YCur);bInterpXEnable=bInterpYEnable=0;}IPCount++;}break;case4:while(IPCountQ){XRes+=xEnd/step;YRes+=yEnd/step;if(XRes=Q){XRes=XRes%Q;XCur+=step;bInterpXEnable=1;}if(YRes=Q){YRes=YRes%Q;YCur-=step;bInterpYEnable=1;}if(bInterpXEnable==1||bInterpYEnable==1){InsertPoint(XCur,YCur);bInterpXEnable=bInterpYEnable=0;}IPCount++;}break;}}/********************************************************************//*函数名:DDA_Line*//*功能:数字积分法直线插补*//*参数:doubleXEnd,doubleYEnd插补终点mm*//*intstep步长mm*//*unsignedshortn寄存器位数*//********************************************************************/voidDDA_Circle(unsignedshortn,intstep,doubleXStart,doubleYStart,doubleXEnd,doubleYEnd,doubleradius,intbIsCW){doublexCur=XStart,yCur=YStart;longxRes=0,yRes=0;intndir,Q;intjudge=1;intfull_circle=0;intxEnable,yEnable;xEnable=yEnable=0;Q=radius;//溢出基值,也可取为pow(2,n)InsertPoint(xCur,yCur);if(XStart==XEnd&YStart==YEnd)full_circle=1;//判断是否要画整圆while(judge==1||full_circle==1)//检测终点{xRes+=fabs(xCur);yRes+=fabs(yCur);if(xRes=Q&fabs(yCur)=radius){xRes=xRes%Q;yEnable=1;}if(yRes=Q&fabs(xCur)=radius){yRes=yRes%Q;xEnable=1;}if(bIsCW==0)//逆圆插补{ndir=Judge_Quadrant(xCur,yCur);if(yEnable==1)//xRes溢出,y轴进给{switch(ndir){case1:if(fabs(yCur)radius)yCur+=step;break;//防止y轴超出半径范围case2:yCur-=step;break;case3:if(fabs(yCur)radius)yCur-=step;break;//防止y轴超出半径范围case4:yCur+=step;break;}}if(xEnable==1)//yRes溢出,x轴进给{switch(ndir){case1:xCur-=step;break;case2:if(fabs(xCur)radius)xCur-=step;break;//防止x轴超出半径范围case3:xCur+=step;break;case4:if(fabs(xCur)radius)xCur+=step;break;//防止x轴超出半径范围}}}else//顺圆插补{ndir=Judge_Quadrant(xCur,yCur);if(yEnable==1)//xRes溢出,y轴进给{switch(ndir){case1:yCur-=step;break;case2:if(fabs(yCur)radius)yCur+=step;break;//防止y轴超出半径范围case3:yCur+=step;break;case4:if(fabs(yCur)radius)yCur-=step;break;//防止y轴超出半径范围}}if(xEnable==1)//yRes溢出,x轴进给{switch(ndir){case1:if(fabs(xCur)radius)xCur+=step;break;//防止x轴超出半径范围case2:xCur+=step;break;case3:if(fabs(xCur)radius)xCur-=step;break;//防止x轴超出半径范围case4:xCur-=step;break;}}}judge=(fabs(xCur-XEnd)=step||fabs(yCur-YEnd)=step);if(xEnable||yEnable)//判断并进行进给运动{InsertPoint(xCur,yCur);xEnable=yEnable=0;full_circle=0;}}}FILE*fp;intmain(){charfn[10];printf(pleaseinputfilename:\n);scanf(%s,fn);if((fp=fopen(fn,w))==NULL){printf(can'topenfile\n);exit(0);}DDA_Circle(4,1,50,0,40,-30,50,1);//输入要插补的圆弧的参数//DDA_Line(4,1,50,-20);//输入要插补的圆弧的参数fclose(fp);return0;}