level 8
云中燕谁
楼主
思路:画出一个矩形框
#include"graphics.h"
#include"stdio.h"
#define Top 8
#define Bottom 4
#define Left 1
#define Right 2
/*该函数用于判断给出点在的位置
左上为1001,正上为1000,右上为1010
左边0001 ,正中为0000,右边 0010
左下0101,正下为0100,右下0110
*/
int station(int x,int y,int a,int b,int c,int d)
{
if((x>=a&&x<=b)&&(y>=c&&y<=d)) return 0;
if((x>=a&&x<=b)&&y<c) return 8;
if((x>=a&&x<=b)&&y>d) return 4;
if(x<a&&y<c) return 9;
if(x<a&&(y>=c&&y<=d)) return 1;
if(x<a&&y>=d) return 5;
if(x>b&&y<c) return 10;
if(x>b&&(y>=c&&y<=d)) return 2;
if(x>b&&y>d) return 6;
}
main()
{ int gr=DETECT,gm,x1,y1,x2,y2,temp1,temp2,temp,x,y;
int Wxl=200; int Wxr=400; int Wyt=150; int Wyb=350; int draw=0; printf("Please input x1,y1,x2,y2:");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
initgraph(&gr,&gm,"");
cleardevice();
/*根据上面给定的边框的位置,还有输入的两坐标值,画出边框并判断两端点的位置*/
rectangle(Wxl,Wyt,Wxr,Wyb);
temp1=station(x1,y1,Wxl,Wxr,Wyt,Wyb);
temp2=station(x2,y2,Wxl,Wxr,Wyt,Wyb);
while(1)
{
/*设第一个点为A,另一点为B,如果两点的位置都在矩形框内部则画线*/
if(temp1==0&&temp2==0)
{ draw=1; printf("both A and B are in Arctangle\n"); break; }
/*都不在则不画*/
if((temp1&temp2)!=0)
{ printf("both A and B aren't in Arctangle\n"); break;
}
/*如果A点不在则求出A与所交的边的交点,
如果B点不在则求出B与所交的边的交点
如果都不在则先求A与的交点,待处理玩A后在处理B的问题
*/
if(temp1!=0)
{ temp=temp1; printf("A isn't in Arctangle\n"); }
else
{ temp=temp2; printf(" B isn't in Arctangle\n");
}
if((temp&Left)!=0)
{ x=Wxl;
y=(y2-y1)/(x2-x1)*(x-x1)+y1;
printf("yu left you jiao dian\n ");
}
if((temp&Right)!=0)
{ x=Wxr;
y=(y2-y1)/(x2-x1)*(x-x1)+y1;
printf("y=%d\n",y);
printf("yu right you jiao dian\n ");
}
if((temp&Top)!=0)
{ y=Wyt;
x=(x2-x1)/(y2-y1)*(y-y1)+x1;
printf("yu top you jiao dian ");
}
if((temp&Bottom)!=0)
{ x=(x2-x1)/(y2-y1)*(Wyb-y1)+x1;
y=Wyb; printf("x=%d",x);
printf("yu bottom you jiao dian ");
}
/*把不在的点移到交点上
执行完后再对新的线段进行判读
知道都在退出循环
*/
if(temp1==temp)
{ x1=x;
y1=y;
printf("x1=%d,y1=%d",x1,y1);
temp1=station(x1,y1,Wxl,Wxr,Wyt,Wyb);
}
if(temp2==temp)
{ x2=x;
y2=y;
printf("x2=%d,y2=%d",x,y);
temp2=station(x2,y2,Wxl,Wxr,Wyt,Wyb); }
}
/*画线*/
if(draw)
{ printf("x2=%d,y2=%d",x2,y2);
line(x1,y1,x2,y2);
}
getch();
closegraph();
}
问题:在WinTC的环境下编辑,对于第一点在框内,第二点不在里面的情况,不正确,
2012年12月01日 03点12分
1
#include"graphics.h"
#include"stdio.h"
#define Top 8
#define Bottom 4
#define Left 1
#define Right 2
/*该函数用于判断给出点在的位置
左上为1001,正上为1000,右上为1010
左边0001 ,正中为0000,右边 0010
左下0101,正下为0100,右下0110
*/
int station(int x,int y,int a,int b,int c,int d)
{
if((x>=a&&x<=b)&&(y>=c&&y<=d)) return 0;
if((x>=a&&x<=b)&&y<c) return 8;
if((x>=a&&x<=b)&&y>d) return 4;
if(x<a&&y<c) return 9;
if(x<a&&(y>=c&&y<=d)) return 1;
if(x<a&&y>=d) return 5;
if(x>b&&y<c) return 10;
if(x>b&&(y>=c&&y<=d)) return 2;
if(x>b&&y>d) return 6;
}
main()
{ int gr=DETECT,gm,x1,y1,x2,y2,temp1,temp2,temp,x,y;
int Wxl=200; int Wxr=400; int Wyt=150; int Wyb=350; int draw=0; printf("Please input x1,y1,x2,y2:");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
initgraph(&gr,&gm,"");
cleardevice();
/*根据上面给定的边框的位置,还有输入的两坐标值,画出边框并判断两端点的位置*/
rectangle(Wxl,Wyt,Wxr,Wyb);
temp1=station(x1,y1,Wxl,Wxr,Wyt,Wyb);
temp2=station(x2,y2,Wxl,Wxr,Wyt,Wyb);
while(1)
{
/*设第一个点为A,另一点为B,如果两点的位置都在矩形框内部则画线*/
if(temp1==0&&temp2==0)
{ draw=1; printf("both A and B are in Arctangle\n"); break; }
/*都不在则不画*/
if((temp1&temp2)!=0)
{ printf("both A and B aren't in Arctangle\n"); break;
}
/*如果A点不在则求出A与所交的边的交点,
如果B点不在则求出B与所交的边的交点
如果都不在则先求A与的交点,待处理玩A后在处理B的问题
*/
if(temp1!=0)
{ temp=temp1; printf("A isn't in Arctangle\n"); }
else
{ temp=temp2; printf(" B isn't in Arctangle\n");
}
if((temp&Left)!=0)
{ x=Wxl;
y=(y2-y1)/(x2-x1)*(x-x1)+y1;
printf("yu left you jiao dian\n ");
}
if((temp&Right)!=0)
{ x=Wxr;
y=(y2-y1)/(x2-x1)*(x-x1)+y1;
printf("y=%d\n",y);
printf("yu right you jiao dian\n ");
}
if((temp&Top)!=0)
{ y=Wyt;
x=(x2-x1)/(y2-y1)*(y-y1)+x1;
printf("yu top you jiao dian ");
}
if((temp&Bottom)!=0)
{ x=(x2-x1)/(y2-y1)*(Wyb-y1)+x1;
y=Wyb; printf("x=%d",x);
printf("yu bottom you jiao dian ");
}
/*把不在的点移到交点上
执行完后再对新的线段进行判读
知道都在退出循环
*/
if(temp1==temp)
{ x1=x;
y1=y;
printf("x1=%d,y1=%d",x1,y1);
temp1=station(x1,y1,Wxl,Wxr,Wyt,Wyb);
}
if(temp2==temp)
{ x2=x;
y2=y;
printf("x2=%d,y2=%d",x,y);
temp2=station(x2,y2,Wxl,Wxr,Wyt,Wyb); }
}
/*画线*/
if(draw)
{ printf("x2=%d,y2=%d",x2,y2);
line(x1,y1,x2,y2);
}
getch();
closegraph();
}
问题:在WinTC的环境下编辑,对于第一点在框内,第二点不在里面的情况,不正确,