谁有用C语言编写的直线,圆弧插补程序

如题所述

/*************************************************************************
   Function:          int ArcXY(double dfx0,double dfy0,double dfrx,
                               double dfry,int angle);
   Description:       在X-Y轴所构成的平面上,以圆弧运动的方式从目前位置经过指
                     定的参考点到目的点。调用此函数成功将增加运动命令的库存数目。 
   Parameters:        dfx0, dfy0     参考点的X-Y轴座标值
                     dfrx, dfry     圆心的X-Y轴座标值
                     angle          插补角度
   Calls:             无   
   Return Value:      大於或等於0      给予此运动命令的编码
                     小於0            失败,传回值的意义可参考错误信息代码
**************************************************************************/

#include <stdio.h>
#include <math.h>
int ArcXY(double dfx0,double dfy0,double dfrx, double dfry,int angle);
int symbol(double number);

main()
{

ArcXY(0,0,-3,5,360);
getch();

}
int ArcXY(double dfx0,double dfy0,double dfrx, double dfry,int angle)
{
     FILE *f1;

     double i,j,dx,dy,dfr,x,y,ang,step,f = 0.01;

     int flag,tempx,tempy,statex,statey,direction = 1;

dfr = sqrt((dfrx - dfx0) * (dfrx - dfx0) + (dfry - dfy0) * (dfry - dfy0));

       if(dfx0 == 0)
        {
         dfx0 = 1;
         dfrx = dfrx + 1;
         statex =1;
        }
      if(dfy0 == 0)
        {
         dfy0 = 1;
         dfry = dfry + 1;
         statey =1;
        }

     dfrx = 2 * dfx0   - dfrx;

     i = dfx0 - dfrx;
     j = dfy0 - dfry;

     x = dfx0 ;
     y = dfy0 ;

     step = ang = 180 * 2 * asin(f/(2*dfr))/3.1415926;

     if(((dfx0 > 0) && (dfy0 > 0)) || ((dfx0 < 0) && (dfy0 < 0)))
          {
           flag = direction;
          }

     if(((dfx0 < 0) && (dfy0 > 0)) || ((dfx0 > 0) && (dfy0 < 0)))
          {
           flag = -direction;
          }

     f1=fopen("c:\\c.txt","w+");

     if(statex ==1)
       {
         x = x - 1;
       }
     if(statey ==1)
       {
         y = y - 1;
       }

     fprintf(f1,"%f,",x);
     fprintf(f1,"%f\n",y);

     while(ang < angle)
          {
           dx = f * (j + flag*(f * i)/(2 * dfr))/dfr;
           dy = f * (i - flag*(f * j)/(2 * dfr))/dfr;
           tempx = symbol(x);
           tempy = symbol(y);

           x = x + dx;
           y = y + dy;

          fprintf(f1,"%f,",x);
          fprintf(f1,"%f\n",y);

          if( (tempx !=symbol(x)) ||   (tempy != symbol(y)) )
            {
              flag = -flag;
            }

          i = i - dx;
          j = j + dy;
          ang = ang + step;

          }

     return 0;

}

int symbol(double number)
{
if(number > 0)
    {
     return 1;
    }
else
    {
    return -1;
    }
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-11-13
给你推荐一本书,唐泽圣,周嘉玉,李新友写的,我们以前上课用的,上面有常用的直线和圆弧算法,比如数值微分法生成直线,逐点比较法插补圆弧,角度DDA法产生圆弧,基本都有C语言的源程序.你可以搜搜这本书,本来都有源程序的,后来给删掉了~~
贴一个别人的
/*************************************************************************
Function: int ArcXY(double dfx0,double dfy0,double dfrx,
double dfry,int angle);
Description: 在X-Y轴所构成的平面上,以圆弧运动的方式从目前位置经过指
定的参考点到目的点。调用此函数成功将增加运动命令的库存数目。
Parameters: dfx0, dfy0 参考点的X-Y轴座标值
dfrx, dfry 圆心的X-Y轴座标值
angle 插补角度
Calls: 无
Return Value: 大於或等於0 给予此运动命令的编码
小於0 失败,传回值的意义可参考错误信息代码
**************************************************************************/

#include
#include
int ArcXY(double dfx0,double dfy0,double dfrx, double dfry,int angle);
int symbol(double number);

main()
{

ArcXY(0,0,-3,5,360);
getch();

}
int ArcXY(double dfx0,double dfy0,double dfrx, double dfry,int angle)
{
FILE *f1;

double i,j,dx,dy,dfr,x,y,ang,step,f = 0.01;

int flag,tempx,tempy,statex,statey,direction = 1;

dfr = sqrt((dfrx - dfx0) * (dfrx - dfx0) + (dfry - dfy0) * (dfry - dfy0));

if(dfx0 == 0)
{
dfx0 = 1;
dfrx = dfrx + 1;
statex =1;
}
if(dfy0 == 0)
{
dfy0 = 1;
dfry = dfry + 1;
statey =1;
}

dfrx = 2 * dfx0 - dfrx;

i = dfx0 - dfrx;
j = dfy0 - dfry;

x = dfx0 ;
y = dfy0 ;

step = ang = 180 * 2 * asin(f/(2*dfr))/3.1415926;

if(((dfx0 > 0) && (dfy0 > 0)) || ((dfx0 < 0) && (dfy0 < 0)))
{
flag = direction;
}

if(((dfx0 0) && (dfy0 > 0)) || ((dfx0 > 0) && (dfy0 < 0)))
{
flag = -direction;
}

f1=fopen("c:\\c.txt","w+");

if(statex ==1)
{
x = x - 1;
}
if(statey ==1)
{
y = y - 1;
}

fprintf(f1,"%f,",x);
fprintf(f1,"%f\n",y);

while(ang < angle)
{
dx = f * (j + flag*(f * i)/(2 * dfr))/dfr;
dy = f * (i - flag*(f * j)/(2 * dfr))/dfr;
tempx = symbol(x);
tempy = symbol(y);

x = x + dx;
y = y + dy;

fprintf(f1,"%f,",x);
fprintf(f1,"%f\n",y);

if( (tempx !=symbol(x)) || (tempy != symbol(y)) )
{
flag = -flag;
}

i = i - dx;
j = j + dy;
ang = ang + step;

}

return 0;

}

int symbol(double number)
{
if(number > 0)
{
return 1;
}
else
{
return -1;
}
}
int ArcXY(double dfx0,double dfy0,double dfrx, double dfry,int angle); 这个就是子函数,你放到你需要的地方注意参数,按自己需要修改,要么添加返回值返回感兴趣的部分,要么添加参数按照传引用的方法取得,直线的有简单DDA算法的,需要的话明天打上,今天有点晚了.
第2个回答  2020-11-08
#include<stdio.h>
#include<math.h>
main()
{
int x,y,x2,y2,dx,dy,n,k,i,f,a;
printf("请输入起始点比如2,3:\n");
scanf("%d,%d",&x,&y);
printf("请输入终止点比如4,9:\n");
scanf("%d,%d",&x2,&y2);
dx=abs(x2-x);
dy=abs(y2-y);
n=dx+dy;
printf("步数n=%d\n",n);
{
if(x2>0) if(y2>0) k=1;
else k=4;
else if(y2>0)k=2;
else k=3;
}
printf("相限k=%d\n",k);
for(i=0,f=0;i<n;i++)
if(f>=0)
{
switch(k)
{
case 1:
x++;
printf("(%d,%d)\n",x,y);
f-=dy;
continue;
case 2:
y++;
printf("(%d,%d)\n",x,y);
f-=dx;
continue;
case 3:
x--;
printf("(%d,%d)\n",x,y);
f-=dy;
continue;
case 4:
y--;
printf("(%d,%d)\n",x,y);
f-=dx;
continue;
}
}
else
{
switch(k)
{
case 1:
y++;
printf("(%d,%d)\n",x,y);
f+=dx;
continue;
case 2:
x--;
printf("(%d,%d)\n",x,y);
f+=dy;
continue;
case 3:
y--;
printf("(%d,%d)\n",x,y);
f+=dx;
continue;
case 4:
x++;
printf("(%d,%d)\n",x,y);
f+=dy;
continue;
}
}
}
相似回答