c语言圆弧插补源代码,模拟PLC 的圆弧插补方式在VC中绘制圆弧

G02     顺时针圆弧插补

G03    逆时针圆弧插补

//已知圆弧上两点 和半径，求圆心

void CircleCenter(double x1,double y1,double x2,double y2,double R,double &x01,double &y01,double &x02,double &y02)

{

//x1 == x2

if (abs(x1-x2)<0.0000001)

{

//(x1,y1)(x2,y2)之间的距离 /2

double dis = abs(y1-y2)/2;

double dx = sqrt(R*R-dis*dis);

double dy = (y1+y2)/2;

x01 = x1-dx;

y01 = dy;

x02 = x1+dx;

y02 = dy;

return ;

}

double c1 = (x2*x2 - x1*x1 + y2*y2 - y1*y1) / (2 *(x2 - x1));

double c2 = (y2 - y1) / (x2 - x1); //斜率

double A = (c2*c2 + 1);

double B = (2 * x1*c2 - 2 * c1*c2 - 2 * y1);

double C = x1*x1 - 2 * x1*c1 + c1*c1 + y1*y1 - R*R;

y01 = (-B + sqrt(B*B - 4 * A*C)) / (2 * A);

x01 = c1 - c2 * y01;

y02 = (-B - sqrt(B*B - 4 * A*C)) / (2 * A);

x02 = c1 - c2*y02;

}

//逆时针画弧

void CDrawShapeCtrl::Arc_AntiClock(DOUBLE StartX, DOUBLE StartY, DOUBLE EndX, DOUBLE EndY, DOUBLE R)

{

AFX_MANAGE_STATE(AfxGetStaticModuleState());

//圆心坐标

double x01,y01,x02,y02;

double x_big,y_big;//大弧圆心

double x_small,y_small;//小弧圆心

LONG nLeftRect, nTopRect,nRightRect,nBottomRect;

CircleCenter(StartX,StartY,EndX,EndY,R,x01,y01,x02,y02);

//向量

double ax = EndX- StartX;

double ay = EndY - StartY;

double bx = x01 - StartX;

double by = y01 - StartY;

//利用向量的叉乘判断圆心位置

//叉乘<0 则为大弧圆心；否则为小弧圆心

double mulRt = ax*by-bx*ay;

if (mulRt<0)

{

x_big = x01;

y_big = y01;

x_small = x02;

y_small = y02;

}

else

{

x_big = x02;

y_big = y02;

x_small = x01;

y_small = y01;

}

CClientDC dc(this);

CRect rc;

GetClientRect(rc);

dc.SetMapMode(MM_ISOTROPIC);//MM_ISOTROPIC

//逻辑坐标原点

dc.SetViewportOrg(rc.right/2,rc.bottom/2);

//设置映射比例为1，逻辑坐标Y轴方向与设备坐标相反

dc.SetWindowExt(100,100);

dc.SetViewportExt(100,-100);

//R>0 弧<180度； R<0 弧>180度

if (R<0) //大弧

{

nLeftRect = x_big-R;

nTopRect = y_big + R;

nRightRect = x_big+R;

nBottomRect = y_big -R;

dc.Arc(nLeftRect,nTopRect,nRightRect,nBottomRect,StartX,StartY,EndX,EndY);

}

else //小弧

{

nLeftRect = x_small-R;

nTopRect = y_small+R;

nRightRect = x_small+R;

nBottomRect = y_small - R;

dc.Arc(nLeftRect,nTopRect,nRightRect,nBottomRect,StartX,StartY,EndX,EndY);

}

}

arc 函数参考：

03-13

01-21 909
06-29 4293
12-25
08-29
01-06

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助