Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
看一下我的思路有没有错误#include <stdio.h> int main() { double xstart,ystart,xend,yend,xleft,ytop,xright,ybottom; int t,d; double k,xmin,ymin,xmax,ymax,x,y; scanf("%d",&d); for(t=0;t<d;t++) { scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&xstart,&ystart,&xend,¥d,&xleft,&ytop,&xright,&ybottom); //判断长方形左上右下是否正确 if(xleft>xright) { x=xleft;xleft=xright;xright=x; y=ytop;ytop=ybottom;ybottom=y; } //若线段其中有一端点在长方形里面,则必相交 if(xleft<=xstart && xstart<=xright && ybottom<=ystart && ystart<=ytop) {printf("T\n");continue;} if(xleft<=xend && xend<=xright && ybottom<=yend && yend<=ytop) {printf("T\n");continue;} //用xmin,ymin保存线段的x,y的较小值,xmax,ymax保存线段的x,y的较大值 xmin=xstart>xend?xend:xstart; xmax=xstart>xend?xstart:xend; ymin=ystart>yend?yend:ystart; ymax=ystart>yend?ystart:yend; //若线段垂直于x轴 if(xstart==xend) { if((xleft<=xend && xend<=xright) && ((ymin<=ytop && ytop<=ymax) || (ymin<=ybottom && ybottom<=ymax))){ printf("T\n");continue; } printf("F\n");continue; } //若线段平行于x轴 if(ystart==yend) { if((ybottom<=yend && yend<=ytop) && ((xmin<=xleft && xleft<=xmax) || (xmin<=xright && xright<=xmax))){ printf("T\n");continue; } printf("F\n");continue; } //其它 k=1.0*(ystart-yend)/(xstart-xend); //若长方形上边可能与线段相交 if(ymin<=ytop && ytop<=ymax) { y=ytop; x=1.0*(y-ystart)/k+xstart; if(xleft<=x && x<=xright) {printf("T\n");continue;} printf("F\n");continue; } //若长方形下边可能与线段相交 if(ymin<=ybottom && ybottom<=ymax) { y=ybottom; x=1.0*(y-ystart)/k+xstart; if(xleft<=x && x<=xright) {printf("T\n");continue;} printf("F\n");continue; } //若长方形左边可能与线段相交 if(xmin<=xleft && xleft<=xmax) { x=xleft; y=1.0*k*(x-xstart)+ystart; if(ybottom<=y && y<=ytop) {printf("T\n");continue;} printf("F\n");continue; } //若长方形右边可能与线段相交 if(xmin<=xright && xright<=xmax) { x=xright; y=1.0*k*(x-xstart)+ystart; if(ybottom<=y && y<=ytop) {printf("T\n");continue;} printf("F\n");continue; } //长方形不可能与线段相交 printf("F\n"); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator