# include "wmlfunc.h"
void exp02_input_data (void);
void line_circle (float,float,float,float);
void line_line_cal (void);
int cal_trans_type (float,float,float,float);
float cal_vector (float,float,float,float,char);
float Xa,Ya,Xb,Yb,Xc,Yc,D_tool;
float Xas,Yas,Xbs,Ybs,Xbs1,Ybs1,Xbs2,Ybs2,Xcs,Ycs;
float crt_ratio=1.0;
// int feed_ratio=5;
int trans_type; //0--short, 1--long, 2--insert
char feed_direction[5];
void main (void)
{
st_graph ();
exp02_draw_picture ();
exp02_input_data ();
line_line_cal ();
real_quit (2);
}
void exp02_input_data (void)
{
float unit;
couttextxy (35,260,"请输入如下参数∶",1,1,3,RED);
couttextxy (15,290,"A点",1,1,1,RED);
outtextxy (50,295,"(Xa,Ya)=");
setcolor (BLUE);
gscanfxy (118,295,"%f,%f",&Xa,&Ya);
couttextxy (15,320,"B点",1,1,1,RED);
setcolor (RED);
outtextxy (50,325,"(Xb,Yb)=");
setcolor (BLUE);
gscanfxy (118,325,"%f,%f",&Xb,&Yb);
couttextxy (15,350,"C点",1,1,1,RED);
setcolor (RED);
outtextxy (50,355,"(Xc,Yc)=");
setcolor (BLUE);
gscanfxy (118,355,"%f,%f",&Xc,&Yc);
couttextxy (15,380,"刀具半径D=",1,1,1,RED);
gscanfxy (118,385," %f",&D_tool);
setcolor (RED);
outtextxy (15,410,"[G41 / G42] :");
setcolor (BLUE);
moveto (118,410);
ggets (feed_direction);
strlwr (feed_direction);
couttextxy (25,440,"输入完毕、开始计算",1,1,2,RED);
if (fabs(D_tool)<MIN) deal_error (27);
unit=fabs(Xa);
if (fabs(Ya)>unit) unit=fabs(Ya);
if (fabs(Xb)>unit) unit=fabs(Xb);
if (fabs(Yb)>unit) unit=fabs(Yb);
if (fabs(Xc)>unit) unit=fabs(Xc);
if (fabs(Yc)>unit) unit=fabs(Yc);
if (unit>160.0) crt_ratio=160.0/unit;
setlinestyle (SOLID_LINE,0,THICK_WIDTH);
line (org1_x+crt_ratio*Xa,org1_y-crt_ratio*Ya,org1_x+crt_ratio*Xb,org1_y-crt_ratio*Yb);
line (org1_x+crt_ratio*Xb,org1_y-crt_ratio*Yb,org1_x+crt_ratio*Xc,org1_y-crt_ratio*Yc);
outtextxy (org1_x+crt_ratio*Xa-15,org1_y-crt_ratio*Ya+5,"A");
outtextxy (org1_x+crt_ratio*Xb-15,org1_y-crt_ratio*Yb+5,"B");
outtextxy (org1_x+crt_ratio*Xc-15,org1_y-crt_ratio*Yc+5,"C");
setlinestyle (SOLID_LINE,0,NORM_WIDTH);
}
void line_circle (float xs,float ys,float xe,float ye)
{
float point,k,radius;
float step=1.0;
radius=crt_ratio*(fabs(D_tool)-2);
if (radius<1.5) radius=2;
sound (30);
setcolor (YELLOW);
if (fabs(xe-xs)<MIN)
{
if (ye>ys)
{
for (point=ys;point<=ye;point++)
{
delay (velocity);
circle (org1_x+crt_ratio*xs,org1_y-crt_ratio*point,radius);
floodfill (org1_x+crt_ratio*xs,org1_y-crt_ratio*point,YELLOW);
}
}
else
{
for (point=ys;point>=ye;point--)
{
delay (velocity);
circle (org1_x+crt_ratio*xs,org1_y-crt_ratio*point,radius);
floodfill (org1_x+crt_ratio*xs,org1_y-crt_ratio*point,YELLOW);
}
}
}
else
{
if (fabs(xe-xs)<=2) step=0.1;
k=(ye-ys)/(xe-xs);
if (xe>xs)
{
for (point=xs;point<=xe;point=point+step)
{
delay (velocity);
circle (org1_x+crt_ratio*point,org1_y-crt_ratio*(ys+(point-xs)*k),radius);
floodfill (org1_x+crt_ratio*point,org1_y-crt_ratio*(ys+(point-xs)*k),YELLOW);
}
}
else
{
for (point=xs;point>=xe;point=point-step)
{
delay (velocity);
circle (org1_x+crt_ratio*point,org1_y-crt_ratio*(ys+(point-xs)*k),radius);
floodfill (org1_x+crt_ratio*point,org1_y-crt_ratio*(ys+(point-xs)*k),YELLOW);
}
}
}
nosound ();
}
void line_line_cal (void)
{
float Xl1,Yl1,Xl2,Yl2;
if (strstr(feed_direction,"g42")!=NULL) D_tool=-D_tool;
Xl1=cal_vector (Xa,Ya,Xb,Yb,'X');
Yl1=cal_vector (Xa,Ya,Xb,Yb,'Y');
Xl2=cal_vector (Xb,Yb,Xc,Yc,'X');
Yl2=cal_vector (Xb,Yb,Xc,Yc,'Y');
trans_type=cal_trans_type (Xl1,Yl1,Xl2,Yl2);
if (trans_type==12)
{
Xbs=Xb-D_tool*Yl1;
Ybs=Yb+D_tool*Xl1;
}
if ((trans_type==1)||(trans_type==2))
{
Xbs=Xb+(Xl2-Xl1)*D_tool/(Xl1*Yl2-Xl2*Yl1);
Ybs=Yb+(Yl2-Yl1)*D_tool/(Xl1*Yl2-Xl2*Yl1);
}
if ((trans_type==3)||(trans_type==33))
{
Xbs1=Xb-D_tool*Yl1+fabs(D_tool)*Xl1;
Ybs1=Yb+D_tool*Xl1+fabs(D_tool)*Yl1;
Xbs2=Xb-D_tool*Yl2-fabs(D_tool)*Xl2;
Ybs2=Yb+D_tool*Xl2-fabs(D_tool)*Yl2;
}
Xas=Xa-D_tool*Yl1;
Yas=Ya+D_tool*Xl1;
Xcs=Xc-D_tool*Yl2;
Ycs=Yc+D_tool*Xl2;
setcolor (GREEN);
if ((trans_type==1)||(trans_type==2)||(trans_type==12))
{
line (org1_x+crt_ratio*Xas,org1_y-crt_ratio*Yas,org1_x+crt_ratio*Xbs,org1_y-crt_ratio*Ybs);
line (org1_x+crt_ratio*Xbs,org1_y-crt_ratio*Ybs,org1_x+crt_ratio*Xcs,org1_y-crt_ratio*Ycs);
line_circle (Xas,Yas,Xbs,Ybs);
line_circle (Xbs,Ybs,Xcs,Ycs);
}
if ((trans_type==3)||(trans_type==33))
{
line (org1_x+crt_ratio*Xas,org1_y-crt_ratio*Yas,org1_x+crt_ratio*Xbs1,org1_y-crt_ratio*Ybs1);
line (org1_x+crt_ratio*Xbs1,org1_y-crt_ratio*Ybs1,org1_x+crt_ratio*Xbs2,org1_y-crt_ratio*Ybs2);
line (org1_x+crt_ratio*Xbs2,org1_y-crt_ratio*Ybs2,org1_x+crt_ratio*Xcs,org1_y-crt_ratio*Ycs);
line_circle (Xas,Yas,Xbs1,Ybs1);
line_circle (Xbs1,Ybs1,Xbs2,Ybs2);
line_circle (Xbs2,Ybs2,Xcs,Ycs);
}
setcolor (RED);
if ((trans_type==1)||(trans_type==2)||(trans_type==12))
{
if (trans_type==1) couttextxy (220,390,"直线接直线缩短型",1,1,1,RED);
if (trans_type==2) couttextxy (220,390,"直线接直线伸长型",1,1,1,RED);
if (trans_type==12) couttextxy (220,390,"直线接直线转接特殊型",1,1,1,RED);
gprintfxy (220,20,"A'(%-5.2f, %-5.2f)",Xas,Yas);
gprintfxy (220,40,"B'(%-5.2f, %-5.2f)",Xbs,Ybs);
gprintfxy (220,60,"C'(%-5.2f, %-5.2f)",Xcs,Ycs);
}
if ((trans_type==3)||(trans_type==33))
{
if (trans_type==3) couttextxy (220,390,"直线接直线插入型",1,1,1,RED);
if (trans_type==33) couttextxy (220,390,"直线接直线转接特殊型",1,1,1,RED);
gprintfxy (220,20,"A'(%-5.2f, %-5.2f)",Xas,Yas);
gprintfxy (220,40,"B1'(%-5.2f, %-5.2f)",Xbs1,Ybs1);
gprintfxy (220,60,"B2'(%-5.2f, %-5.2f)",Xbs2,Ybs2);
gprintfxy (220,80,"C'(%-5.2f, %-5.2f)",Xcs,Ycs);
}
couttextxy (org1_x+20,390,"显示比例为∶",1,1,1,RED);
gprintfxy (org1_x+125,395,"%-4.2f",crt_ratio);
}
int cal_trans_type (float Xl1,float Yl1,float Xl2,float Yl2)
{
register sign;
if (fabs(Xl1*Yl2-Xl2*Yl1)<=MIN) //a=0 deg or 180 deg
{
if ((fabs(Xl1-Xl2)<MIN)&&(fabs(Yl1-Yl2)<MIN)) sign=12; //a=180 deg
if ((fabs(Xl1+Xl2)<MIN)&&(fabs(Yl1+Yl2)<MIN)) sign=33; //a=0 deg
}
if ((sgn(D_tool)*(Yl2*Xl1-Yl1*Xl2))>MIN) sign=1; //a=(180,360)
if (((sgn(D_tool)*(Yl2*Xl1-Yl1*Xl2))<-MIN)&&((Yl1*Yl2+Xl1*Xl2)>-MIN))
sign=2; //a=[90,180)
if (((sgn(D_tool)*(Yl2*Xl1-Yl1*Xl2))<-MIN)&&((Yl1*Yl2+Xl1*Xl2)<=-MIN))
sign=3; //a=(0,90)
return (sign);
}
float cal_vector (float x1,float y1,float x2,float y2,char xy)
{
float distance,unit;
distance=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
if (distance<MIN) deal_error (6);
if (xy=='X') unit=(x2-x1)/distance;
if (xy=='Y') unit=(y2-y1)/distance;
return (unit);
}