Posted: Mon Jul 08, 2002 2:17 pm
How about:
4 9 11 2 12 5 1 5
4 9 11 2 4 9 4 9
result:
T
T
4 9 11 2 12 5 1 5
4 9 11 2 4 9 4 9
result:
T
T
Code: Select all
#include<stdio.h>
void func(int xstart,int ystart,int xend,int yend,int x1,int y1,int x2,int y3);
int point_in_linesegment(float x,float y,int xstart,int ystart,int xend,int yend);
float value_y_given_x(int x,int xstart,int ystart,int xend,int yend);
float value_x_given_y(int y,int xstart,int ystart,int xend,int yend);
main()
{int xstart,ystart,xend,yend,topleft1,topleft2,bottomright1,bottomright2,n,x1,y1,x2,y3,i;
float x,y;
int z;
scanf("%d",&n);
for(i=0;i<n;i++)
{ z =0;
scanf("%d",&xstart);
scanf("%d",&ystart);
scanf("%d",&xend);
scanf("%d",¥d);
scanf("%d",&topleft1);
scanf("%d",&topleft2);
scanf("%d",&bottomright1);
scanf("%d",&bottomright2);
if( (topleft1 < bottomright1)&&( topleft2 > bottomright2) )
{ x1 = topleft1;
y1 = topleft2;
x2 = bottomright1;
y3 = bottomright2;
}
if( (topleft1 < bottomright1)&&( topleft2 < bottomright2) )
{ x1 = topleft1;
y3 = topleft2;
x2 = bottomright1;
y1 = bottomright2;
}
if( (topleft1 > bottomright1)&&( topleft2 > bottomright2) )
{ x1 = bottomright1;
y3 = bottomright2;
x2 = topleft1;
y1 = topleft2;
}
if( (topleft1 > bottomright1)&&( topleft2 < bottomright2) )
{ x2 = topleft1;
y3 = topleft2;
x1 = bottomright1;
y1 = bottomright2;
}
if( (topleft1 == bottomright1) && (topleft2 != bottomright2) )
{/* when rect is actually a vertical line */
x = topleft1;
y = value_y_given_x(x,xstart, ystart, xend, yend);
if( point_in_linesegment(x,y,xstart,ystart,xend,yend) == 1)
printf("T\n");
else
printf("F\n");
z =1;/* special case has applied */
}
if( (topleft1 != bottomright1) && (topleft2 == bottomright2) )
{/* when rect is actually a horizontal line */
y = topleft2;
x = value_x_given_y(y,xstart, ystart, xend, yend);
if( point_in_linesegment(x,y,xstart,ystart,xend,yend) == 1)
printf("T\n");
else
printf("F\n");
z =1;
}
if( (topleft1 == bottomright1) && (topleft2 == bottomright2) )
{/* when rect is actually a point */
x = topleft1;
y = topleft2;
if( point_in_linesegment(x,y,xstart,ystart,xend,yend) == 1)
printf("T\n");
else
printf("F\n");
z =1;
}
/* printf(" %d %d %d %d %d %d %d %d",xstart,ystart,xend,yend,x1,y1,x2,y3);exit(1); */
if(z != 1)/* special cases didn't worked */
func(xstart,ystart,xend,yend,x1,y1,x2,y3);
}
}
void func(int xstart,int ystart,int xend,int yend,int x1,int y1,int x2,int y3)
{ float x,y;
if(ystart == yend)/* IF #1 *//* line segment is horizontal */
{/* Checking if any of end pt of the line segment is any of the horizontal lines of rect */
if( (ystart == y1)||(ystart == y3) )
if( ((xstart >= x1)&&(xstart <= x2))|| ((xend >= x1)&&(xend <= x2))|| ((xstart <=x1)&&(xend >=x2))|| ((xstart >= x2)&&(xend <=x1)) )
{ printf("T\n");
return;
}
else
{ printf("F\n");
return;
}
if( (ystart > y1)||(ystart < y3) )
{ printf("F\n");
return;
}/* Line is either above or below the rectangle */
if( xstart > x2 && xend > x2)
{ printf("F\n");
return;
}/* line segm is entirely right of rectangle */
if( xstart < x1 && xend < x1)
{ printf("F\n");
return;
}/* line segm is entirely left of rectangle */
if( xstart < x2 && xend < x2 && xstart > x1 && xend > x1 )
{ printf("F\n");
return;
}/* horizontal line segm is entirely inside rectangle */
printf("T\n");
return;
}/*END of IF #1 */
/* checked when line segment is horizontal */
if(xstart == xend)/* IF #2 *//* the line segm is vertical */
{/* Checking if any of end pt of the line segment is on any of the vertical lines of rect */
if( (xstart == x1)||(xstart == x2) )
if( ((ystart >= y3)&&(ystart <= y1))|| ((yend >= y3)&&(yend <= y1))|| (( ystart <= y3)&&(yend >= y1))|| (( ystart >= y1)&&(yend <= y3)) )
{ printf("T\n");
return;
}
else
{ printf("F\n");
return;
}
if( (xstart < x1)||(xstart > x2) )
{ printf("F\n");
return;
}
if( ystart > y1 && yend > y1)
{ printf("F\n");
return;
}/* vertical line segm is entirely above rectangle */
if( ystart < y3 && yend < y3)
{ printf("F\n");
return;
}/* vertical line segm is entirely below rectangle */
if( ystart < y1 && yend < y1 && ystart > y3 && yend > y3 )
{ printf("F\n");
return;
}/* vertical line segm is entirely inside rectangle */
printf("T\n");
return;
}/*END of IF #2 */
/* checked when line segment is vertical */
/* after checking that line segment is not parallel to any side of retangle */
/* checking the top line of rectangle */
x = value_x_given_y(y1,xstart, ystart, xend, yend);/* this gives the value of x if line intersects top line of rectangle*/
/* printf("\n At 7 x= %f y = %d\n",x,y1); */
if( x>=x1 && x<=x2 && (point_in_linesegment(x,y1,xstart, ystart, xend, yend) == 1) )
{ printf("T\n");
return;
}
/* checking the bottom line of rectangle */
x = value_x_given_y(y3,xstart, ystart, xend, yend);
if( x>=x1 && x<=x2 && (point_in_linesegment(x,y3,xstart, ystart, xend, yend) == 1) )
{ printf("T\n");
return;
}
/* checking the left line of rectangle */
y = value_y_given_x(x1,xstart, ystart, xend, yend);
if( y>=y3 && y<=y1 && (point_in_linesegment(x1,y,xstart, ystart, xend, yend) == 1) )
{ printf("T\n");
return;
}
/* checking the right line of rectangle */
y = value_y_given_x(x2,xstart, ystart, xend, yend);
if( y>=y3 && y<=y1 && (point_in_linesegment(x2,y,xstart, ystart, xend, yend) == 1) )
{ printf("T\n");
return;
}
/* When non of the cases appeared */
printf("F\n");
return;
}
int point_in_linesegment(float x,float y,int xstart,int ystart,int xend,int yend)
{float a,b,len_sqr;
if( (( x== xend)&&(y == yend) )||( (x == xstart)&&(y == ystart) ) )
return(1);/* 1 means the point is on the line segment */
len_sqr = (xstart - xend)*(xstart - xend) + (ystart - yend)*(ystart - yend);
a = (x - xend)*(x - xend) + (y - yend)*(y - yend);
b = (xstart - x)*(xstart - x) + (ystart - y)*(ystart - y);
/* a = (y - yend)/(x - xend);
b = (y - ystart)/(x - xstart); */
/* if( ( a>0 && b>0)||(a<0 && b<0) ) */
if( a <= len_sqr && b <= len_sqr)
return(1);
else
return(0);
}
float value_y_given_x(int x,int xstart,int ystart,int xend,int yend)
{float a;
a= ystart + (x - xstart)*(yend - ystart)/(xend - xstart);
return(a);
}
float value_x_given_y(int y,int xstart,int ystart,int xend,int yend)
{float b;
b = xstart + (y - ystart)*(xend - xstart)/(yend - ystart);
return(b);
}
Code: Select all
#include<stdio.h>
#include<math.h>
void func(int xstart,int ystart,int xend,int yend,int x1,int y1,int x2,int y3);
int point_in_linesegment(float x,float y,int xstart,int ystart,int xend,int yend);
float value_y_given_x(int x,int xstart,int ystart,int xend,int yend);
float value_x_given_y(int y,int xstart,int ystart,int xend,int yend);
main()
{int xstart,ystart,xend,yend,topleft1,topleft2,bottomright1,bottomright2,n,x1,y1,x2,y3,i;
float x,y;
int z;
scanf("%d",&n);
for(i=0;i<n;i++)
{ z =0;
scanf("%d",&xstart);
scanf("%d",&ystart);
scanf("%d",&xend);
scanf("%d",¥d);
scanf("%d",&topleft1);
scanf("%d",&topleft2);
scanf("%d",&bottomright1);
scanf("%d",&bottomright2);
if( (topleft1 < bottomright1)&&( topleft2 > bottomright2) )
{ x1 = topleft1;
y1 = topleft2;
x2 = bottomright1;
y3 = bottomright2;
}
if( (topleft1 < bottomright1)&&( topleft2 < bottomright2) )
{ x1 = topleft1;
y3 = topleft2;
x2 = bottomright1;
y1 = bottomright2;
}
if( (topleft1 > bottomright1)&&( topleft2 > bottomright2) )
{ x1 = bottomright1;
y3 = bottomright2;
x2 = topleft1;
y1 = topleft2;
}
if( (topleft1 > bottomright1)&&( topleft2 < bottomright2) )
{ x2 = topleft1;
y3 = topleft2;
x1 = bottomright1;
y1 = bottomright2;
}
if( (topleft1 == bottomright1) && (topleft2 != bottomright2) )
{/* when rect is actually a vertical line */
x = topleft1;
y = value_y_given_x(x,xstart, ystart, xend, yend);
if( ( y<= topleft2 && y>= bottomright2 )||( y>= topleft2 && y<= bottomright2 ) )
printf("T\n");
else
printf("F\n");
z =1;/* special case has applied */
}
if( (topleft1 != bottomright1) && (topleft2 == bottomright2) )
{/* when rect is actually a horizontal line */
y = topleft2;
x = value_x_given_y(y,xstart, ystart, xend, yend);
/* printf("x = %f y = %f ",x,y); */
if( ( x<= topleft1 && x>= bottomright1 )||( x>= topleft1 && x<= bottomright1 ) )
printf("T\n");
else
printf("F\n");
z =1;
}
if( (topleft1 == bottomright1) && (topleft2 == bottomright2) )
{/* when rect is actually a point */
x = topleft1;
y = topleft2;
if( point_in_linesegment(x,y,xstart,ystart,xend,yend) == 1)
printf("T\n");
else
printf("F\n");
z =1;
}
/* printf(" %d %d %d %d %d %d %d %d",xstart,ystart,xend,yend,x1,y1,x2,y3);exit(1); */
if(z != 1)/* special cases didn't worked */
func(xstart,ystart,xend,yend,x1,y1,x2,y3);
}
}
void func(int xstart,int ystart,int xend,int yend,int x1,int y1,int x2,int y3)
{ float x,y;
if(ystart == yend)/* IF #1 *//* line segment is horizontal */
{/* Checking if any of end pt of the line segment is any of the horizontal lines of rect */
if( (ystart == y1)||(ystart == y3) )
if( ((xstart >= x1)&&(xstart <= x2))|| ((xend >= x1)&&(xend <= x2))|| ((xstart <=x1)&&(xend >=x2))|| ((xstart >= x2)&&(xend <=x1)) )
{ printf("T\n");
return;
}
else
{ printf("F\n");
return;
}
if( (ystart > y1)||(ystart < y3) )
{ printf("F\n");
return;
}/* Line is either above or below the rectangle */
if( xstart > x2 && xend > x2)
{ printf("F\n");
return;
}/* line segm is entirely right of rectangle */
if( xstart < x1 && xend < x1)
{ printf("F\n");
return;
}/* line segm is entirely left of rectangle */
if( xstart < x2 && xend < x2 && xstart > x1 && xend > x1 )
{ printf("F\n");
return;
}/* horizontal line segm is entirely inside rectangle */
printf("T\n");
return;
}/*END of IF #1 */
/* checked when line segment is horizontal */
if(xstart == xend)/* IF #2 *//* the line segm is vertical */
{/* Checking if any of end pt of the line segment is on any of the vertical lines of rect */
if( (xstart == x1)||(xstart == x2) )
if( ((ystart >= y3)&&(ystart <= y1))|| ((yend >= y3)&&(yend <= y1))|| (( ystart <= y3)&&(yend >= y1))|| (( ystart >= y1)&&(yend <= y3)) )
{ printf("T\n");
return;
}
else
{ printf("F\n");
return;
}
if( (xstart < x1)||(xstart > x2) )
{ printf("F\n");
return;
}
if( ystart > y1 && yend > y1)
{ printf("F\n");
return;
}/* vertical line segm is entirely above rectangle */
if( ystart < y3 && yend < y3)
{ printf("F\n");
return;
}/* vertical line segm is entirely below rectangle */
if( ystart < y1 && yend < y1 && ystart > y3 && yend > y3 )
{ printf("F\n");
return;
}/* vertical line segm is entirely inside rectangle */
printf("T\n");
return;
}/*END of IF #2 */
/* checked when line segment is vertical */
/* after checking that line segment is not parallel to any side of retangle */
/* checking the top line of rectangle */
x = value_x_given_y(y1,xstart, ystart, xend, yend);/* this gives the value of x if line intersects top line of rectangle*/
/* printf("\n At 7 x= %f y = %d\n",x,y1); */
if( x>=x1 && x<=x2 )
/* && (point_in_linesegment(x,y1,xstart, ystart, xend, yend) == 1) ) */
{ printf("T\n");
return;
}
/* checking the bottom line of rectangle */
x = value_x_given_y(y3,xstart, ystart, xend, yend);
if( x>=x1 && x<=x2 )
/* && (point_in_linesegment(x,y3,xstart, ystart, xend, yend) == 1) ) */
{ printf("T\n");
return;
}
/* checking the left line of rectangle */
y = value_y_given_x(x1,xstart, ystart, xend, yend);
if( y>=y3 && y<=y1 )
/* && (point_in_linesegment(x1,y,xstart, ystart, xend, yend) == 1) ) */
{ printf("T\n");
return;
}
/* checking the right line of rectangle */
y = value_y_given_x(x2,xstart, ystart, xend, yend);
if( y>=y3 && y<=y1 )
/* && (point_in_linesegment(x2,y,xstart, ystart, xend, yend) == 1) ) */
{ printf("T\n");
return;
}
/* When non of the cases appeared */
printf("F\n");
return;
}
int point_in_linesegment(float x,float y,int xstart,int ystart,int xend,int yend)
{float a,b,len_sqr,a1,b1,len;
if( (( x== xend)&&(y == yend) )||( (x == xstart)&&(y == ystart) ) )
return(1);/* 1 means the point is on the line segment */
len_sqr = (xstart - xend)*(xstart - xend) + (ystart - yend)*(ystart - yend);
a = (x - xend)*(x - xend) + (y - yend)*(y - yend);
b = (xstart - x)*(xstart - x) + (ystart - y)*(ystart - y);
len = sqrt(len_sqr);
a1 = sqrt(a);
b1 = sqrt(b);
/* a = (y - yend)/(x - xend);
b = (y - ystart)/(x - xstart); */
/* if( ( a>0 && b>0)||(a<0 && b<0) ) */
if( a1+b1 == len)
return(1);
else
return(0);
}
float value_y_given_x(int x,int xstart,int ystart,int xend,int yend)
{float a;
a= ystart + (x - xstart)*(yend - ystart)/(xend - xstart);
return(a);
}
float value_x_given_y(int y,int xstart,int ystart,int xend,int yend)
{float b;
b = xstart + (y - ystart)*(xend - xstart)/(yend - ystart);
return(b);
}
Code: Select all
#include<stdio.h>
#include<math.h>
void func(int xstart,int ystart,int xend,int yend,int x1,int y1,int x2,int y3);
int point_in_linesegment(float x,float y,int xstart,int ystart,int xend,int yend);
float value_y_given_x(int x,int xstart,int ystart,int xend,int yend);
float value_x_given_y(int y,int xstart,int ystart,int xend,int yend);
main()
{int xstart,ystart,xend,yend,topleft1,topleft2,bottomright1,bottomright2,n,x1,y1,x2,y3,i;
float x,y;
int z;
scanf("%d",&n);
for(i=0;i<n;i++)
{ z =0;
scanf("%d",&xstart);
scanf("%d",&ystart);
scanf("%d",&xend);
scanf("%d",¥d);
scanf("%d",&topleft1);
scanf("%d",&topleft2);
scanf("%d",&bottomright1);
scanf("%d",&bottomright2);
if( (xstart == xend)&&(ystart == yend) )/* the line is itself a point */
{/* printf("line is a pt \n"); */
if( (topleft1 == bottomright1)&&(topleft2 == bottomright2) )/* rect is also a point */
{ if((xstart == topleft1)&&(ystart == topleft2) )
printf("T\n");
else
printf("F\n");
continue;
}
/* when rectangle is not a point */
if( ( ( (xstart <= topleft1)&&(xstart >= bottomright1) )||
( (xstart >= topleft1)&&(xstart <= bottomright1) ) )&&
( ( (ystart <= topleft2)&&(ystart >= bottomright2) )||
( (ystart >= topleft2)&&(ystart <= bottomright2) ) ) )
/* the point is checked to be in or on the rectangle */
{ printf("T\n");
continue;
}
printf("F\n");
continue;
}/* END of if */
/* verified the case when the line is actually a point */
if( (topleft1 == bottomright1)&&(topleft2 == bottomright2) )
{/* rectangle is a point but the line is not a point */
if( point_in_linesegment(topleft1,topleft2,xstart, ystart, xend,yend) == 1)
printf("T\n");
else
printf("F\n");
continue;
}
/* special cases when either or both line or rect are point has been verified */
if( (topleft1 < bottomright1)&&( topleft2 > bottomright2) )
{ x1 = topleft1;
y1 = topleft2;
x2 = bottomright1;
y3 = bottomright2;
}
if( (topleft1 < bottomright1)&&( topleft2 < bottomright2) )
{ x1 = topleft1;
y3 = topleft2;
x2 = bottomright1;
y1 = bottomright2;
}
if( (topleft1 > bottomright1)&&( topleft2 > bottomright2) )
{ x1 = bottomright1;
y3 = bottomright2;
x2 = topleft1;
y1 = topleft2;
}
if( (topleft1 > bottomright1)&&( topleft2 < bottomright2) )
{ x2 = topleft1;
y3 = topleft2;
x1 = bottomright1;
y1 = bottomright2;
}
if( (topleft1 == bottomright1) && (topleft2 != bottomright2) )
{/* when rect is actually a vertical line */
if( xstart == xend)/* line segm is also vertical */
{ if(xstart != topleft1)
{printf("F\n");
continue;
}
if( point_in_linesegment(xstart,ystart,topleft1,topleft2,bottomright1,bottomright2) == 1 )
{printf("T\n");
continue;
}
if( point_in_linesegment(xend,yend,topleft1,topleft2,bottomright1,bottomright2) == 1 )
{printf("T\n");
continue;
}
if( point_in_linesegment(topleft1,topleft2,xstart,ystart,xend,yend) == 1 )
{printf("T\n");
continue;
}
if( point_in_linesegment(bottomright1,bottomright2,xstart,ystart,xend,yend) == 1 )
{printf("T\n");
continue;
}
}/* checked case when line segm is also vertical */
x = topleft1;
y = value_y_given_x(x,xstart, ystart, xend, yend);
if( ( y<= topleft2 && y>= bottomright2 )||( y>= topleft2 && y<= bottomright2 ) )
printf("T\n");
else
printf("F\n");
continue;
}
if( (topleft1 != bottomright1) && (topleft2 == bottomright2) )
{/* when rect is actually a horizontal line */
if( ystart == yend)/* line segm is also horiz */
{ if(ystart != topleft2)
{printf("F\n");
continue;
}
if( point_in_linesegment(xstart,ystart,topleft1,topleft2,bottomright1,bottomright2) == 1 )
{printf("T\n");
continue;
}
if( point_in_linesegment(xend,yend,topleft1,topleft2,bottomright1,bottomright2) == 1 )
{printf("T\n");
continue;
}
if( point_in_linesegment(topleft1,topleft2,xstart,ystart,xend,yend) == 1 )
{printf("T\n");
continue;
}
if( point_in_linesegment(bottomright1,bottomright2,xstart,ystart,xend,yend) == 1 )
{printf("T\n");
continue;
}
}/* checked case when line segm is also vertical */
y = topleft2;
x = value_x_given_y(y,xstart, ystart, xend, yend);
/* printf("x = %f y = %f ",x,y); */
if( ( x<= topleft1 && x>= bottomright1 )||( x>= topleft1 && x<= bottomright1 ) )
printf("T\n");
else
printf("F\n");
continue;
}
/* printf(" %d %d %d %d %d %d %d %d",xstart,ystart,xend,yend,x1,y1,x2,y3);exit(1); */
/* now if the line and rectangle are as per convention the prog has reached here */
func(xstart,ystart,xend,yend,x1,y1,x2,y3);
}
}
void func(int xstart,int ystart,int xend,int yend,int x1,int y1,int x2,int y3)
{ float x,y;
if(ystart == yend)/* IF #1 *//* line segment is horizontal */
{
if( (ystart > y1)||(ystart < y3) )
{ printf("F\n");
return;
}/* Line is either above or below the rectangle */
if( xstart > x2 && xend > x2)
{ printf("F\n");
return;
}/* line segm is entirely right of rectangle */
if( xstart < x1 && xend < x1)
{ printf("F\n");
return;
}/* line segm is entirely left of rectangle */
printf("T\n");
return;
}/*END of IF #1 */
/* checked when line segment is horizontal */
if(xstart == xend)/* IF #2 *//* the line segm is vertical */
{
if( (xstart < x1)||(xstart > x2) )
{ printf("F\n");
return;
}
if( ystart > y1 && yend > y1)
{ printf("F\n");
return;
}/* vertical line segm is entirely above rectangle */
if( ystart < y3 && yend < y3)
{ printf("F\n");
return;
}/* vertical line segm is entirely below rectangle */
printf("T\n");
return;
}/*END of IF #2 */
/* checked when line segment is vertical */
/* after checking that line segment is not parallel to any side of retangle */
/* checking if any of the end pt of line segment is inside rectangle */
if( (xstart >= x1)&&(xstart <= x2)&&(ystart >= y3)&&(ystart <= y1) )
printf("T\n");
if( (xend >= x1)&&(xend <= x2)&&(yend >= y3)&&(yend <= y1) )
printf("T\n");
/* checking the top line of rectangle */
x = value_x_given_y(y1,xstart, ystart, xend, yend);
/* this gives the value of x if line intersects top line of rectangle*/
/* printf("\n At 7 x= %f y = %d\n",x,y1); */
if( x>=x1 && x<=x2 )
/* && (point_in_linesegment(x,y1,xstart, ystart, xend, yend) == 1) ) */
{ printf("T\n");
return;
}
/* checking the bottom line of rectangle */
x = value_x_given_y(y3,xstart, ystart, xend, yend);
if( x>=x1 && x<=x2 )
/* && (point_in_linesegment(x,y3,xstart, ystart, xend, yend) == 1) ) */
{ printf("T\n");
return;
}
/* checking the left line of rectangle */
y = value_y_given_x(x1,xstart, ystart, xend, yend);
if( y>=y3 && y<=y1 )
/* && (point_in_linesegment(x1,y,xstart, ystart, xend, yend) == 1) ) */
{ printf("T\n");
return;
}
/* checking the right line of rectangle */
y = value_y_given_x(x2,xstart, ystart, xend, yend);
if( y>=y3 && y<=y1 )
/* && (point_in_linesegment(x2,y,xstart, ystart, xend, yend) == 1) ) */
{ printf("T\n");
return;
}
/* When non of the cases appeared */
printf("F\n");
return;
}
int point_in_linesegment(float x,float y,int xstart,int ystart,int xend,int yend)
{float a,b,len_sqr,a1,b1,len;
if( (( x== xend)&&(y == yend) )||( (x == xstart)&&(y == ystart) ) )
return(1);/* 1 means the point is on the line segment */
len_sqr = (xstart - xend)*(xstart - xend) + (ystart - yend)*(ystart - yend);
a = (x - xend)*(x - xend) + (y - yend)*(y - yend);
b = (xstart - x)*(xstart - x) + (ystart - y)*(ystart - y);
len = sqrt(len_sqr);
a1 = sqrt(a);
b1 = sqrt(b);
/* a = (y - yend)/(x - xend);
b = (y - ystart)/(x - xstart); */
/* if( ( a>0 && b>0)||(a<0 && b<0) ) */
if( a1+b1 == len)
return(1);
else
return(0);
}
float value_y_given_x(int x,int xstart,int ystart,int xend,int yend)
{float a;
a= ystart + (x - xstart)*(yend - ystart)/(xend - xstart);
return(a);
}
float value_x_given_y(int y,int xstart,int ystart,int xend,int yend)
{float b;
b = xstart + (y - ystart)*(xend - xstart)/(yend - ystart);
return(b);
}
Code: Select all
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
void func(int xstart,int ystart,int xend,int yend,int x1,int y1,int x2,int y3);
int point_in_linesegment(float x,float y,int xstart,int ystart,int xend,int yend);
float value_y_given_x(int x,int xstart,int ystart,int xend,int yend);
float value_x_given_y(int y,int xstart,int ystart,int xend,int yend);
main()
{int xstart,ystart,xend,yend,topleft1,topleft2,bottomright1,bottomright2,n,x1,y1,x2,y3,i;
float x,y;
int z;
scanf("%d",&n);
for(i=0;i<n;i++)
{ z =0;
scanf("%d",&xstart);
scanf("%d",&ystart);
scanf("%d",&xend);
scanf("%d",¥d);
scanf("%d",&topleft1);
scanf("%d",&topleft2);
scanf("%d",&bottomright1);
scanf("%d",&bottomright2);
if( (xstart == xend)&&(ystart == yend) )/* the line is itself a point */
{/* printf("line is a pt \n"); */
if( (topleft1 == bottomright1)&&(topleft2 == bottomright2) )/* rect is also a point */
{ if((xstart == topleft1)&&(ystart == topleft2) )
printf("T\n");
else
printf("F\n");
continue;
}
/* when rectangle is not a point */
if( ( ( (xstart <= topleft1)&&(xstart >= bottomright1) )||
( (xstart >= topleft1)&&(xstart <= bottomright1) ) )&&
( ( (ystart <= topleft2)&&(ystart >= bottomright2) )||
( (ystart >= topleft2)&&(ystart <= bottomright2) ) ) )
/* the point is checked to be in or on the rectangle */
{ printf("T\n");
continue;
}
printf("F\n");
continue;
}/* END of if */
/* verified the case when the line is actually a point */
if( (topleft1 == bottomright1)&&(topleft2 == bottomright2) )
{/* rectangle is a point but the line is not a point */
if( point_in_linesegment(topleft1,topleft2,xstart, ystart, xend,yend) == 1)
printf("T\n");
else
printf("F\n");
continue;
}
/* special cases when either or both line or rect are point has been verified */
if( (topleft1 < bottomright1)&&( topleft2 > bottomright2) )
{ x1 = topleft1;
y1 = topleft2;
x2 = bottomright1;
y3 = bottomright2;
}
if( (topleft1 < bottomright1)&&( topleft2 < bottomright2) )
{ x1 = topleft1;
y3 = topleft2;
x2 = bottomright1;
y1 = bottomright2;
}
if( (topleft1 > bottomright1)&&( topleft2 > bottomright2) )
{ x1 = bottomright1;
y3 = bottomright2;
x2 = topleft1;
y1 = topleft2;
}
if( (topleft1 > bottomright1)&&( topleft2 < bottomright2) )
{ x2 = topleft1;
y3 = topleft2;
x1 = bottomright1;
y1 = bottomright2;
}
if( (topleft1 == bottomright1) && (topleft2 != bottomright2) )
{/* when rect is actually a vertical line */
if( xstart == xend)/* line segm is also vertical */
{ if(xstart != topleft1)
{printf("F\n");
continue;
}
if( point_in_linesegment(xstart,ystart,topleft1,topleft2,bottomright1,bottomright2) == 1 )
{printf("T\n");
continue;
}
if( point_in_linesegment(xend,yend,topleft1,topleft2,bottomright1,bottomright2) == 1 )
{printf("T\n");
continue;
}
if( point_in_linesegment(topleft1,topleft2,xstart,ystart,xend,yend) == 1 )
{printf("T\n");
continue;
}
if( point_in_linesegment(bottomright1,bottomright2,xstart,ystart,xend,yend) == 1 )
{printf("T\n");
continue;
}
printf("F\n");
continue;
}/* checked case when line segm is also vertical */
x = topleft1;
y = value_y_given_x(x,xstart, ystart, xend, yend);
if( ( y<= topleft2 && y>= bottomright2 )||( y>= topleft2 && y<= bottomright2 ) )
printf("T\n");
else
printf("F\n");
continue;
}/* VERIFIED when rect is actually a vertical line */
if( (topleft1 != bottomright1) && (topleft2 == bottomright2) )
{/* when rect is actually a horizontal line */
if( ystart == yend)/* line segm is also horiz */
{ if(ystart != topleft2)
{printf("F\n");
continue;
}
if( point_in_linesegment(xstart,ystart,topleft1,topleft2,bottomright1,bottomright2) == 1 )
{printf("T\n");
continue;
}
if( point_in_linesegment(xend,yend,topleft1,topleft2,bottomright1,bottomright2) == 1 )
{printf("T\n");
continue;
}
if( point_in_linesegment(topleft1,topleft2,xstart,ystart,xend,yend) == 1 )
{printf("T\n");
continue;
}
if( point_in_linesegment(bottomright1,bottomright2,xstart,ystart,xend,yend) == 1 )
{printf("T\n");
continue;
}
printf("F\n");
continue;
}/* checked case when line segm is also vertical */
y = topleft2;
x = value_x_given_y(y,xstart, ystart, xend, yend);
/* printf("x = %f y = %f ",x,y); */
if( ( x<= topleft1 && x>= bottomright1 )||( x>= topleft1 && x<= bottomright1 ) )
printf("T\n");
else
printf("F\n");
continue;
}
/* printf(" %d %d %d %d %d %d %d %d",xstart,ystart,xend,yend,x1,y1,x2,y3);exit(1); */
/* now if the line and rectangle are as per convention the prog has reached here */
func(xstart,ystart,xend,yend,x1,y1,x2,y3);
}
}
void func(int xstart,int ystart,int xend,int yend,int x1,int y1,int x2,int y3)
{ float x,y;
if(ystart == yend)/* IF #1 *//* line segment is horizontal */
{
if( (ystart > y1)||(ystart < y3) )
{ printf("F\n");
return;
}/* Line is either above or below the rectangle */
if( xstart > x2 && xend > x2)
{ printf("F\n");
return;
}/* line segm is entirely right of rectangle */
if( xstart < x1 && xend < x1)
{ printf("F\n");
return;
}/* line segm is entirely left of rectangle */
printf("T\n");
return;
}/*END of IF #1 */
/* checked when line segment is horizontal */
if(xstart == xend)/* IF #2 *//* the line segm is vertical */
{
if( (xstart < x1)||(xstart > x2) )
{ printf("F\n");
return;
}
if( ystart > y1 && yend > y1)
{ printf("F\n");
return;
}/* vertical line segm is entirely above rectangle */
if( ystart < y3 && yend < y3)
{ printf("F\n");
return;
}/* vertical line segm is entirely below rectangle */
printf("T\n");
return;
}/*END of IF #2 */
/* checked when line segment is vertical */
/* after checking that line segment is not parallel to any side of retangle */
/* checking if any of the end pt of line segment is inside rectangle */
if( (xstart >= x1)&&(xstart <= x2)&&(ystart >= y3)&&(ystart <= y1) )
{ printf("T\n");
return;
}
if( (xend >= x1)&&(xend <= x2)&&(yend >= y3)&&(yend <= y1) )
{ printf("T\n");
return;
}
/* checking the top line of rectangle */
x = value_x_given_y(y1,xstart, ystart, xend, yend);
/* this gives the value of x if line intersects top line of rectangle*/
/* printf("\n At 7 x= %f y = %d\n",x,y1); */
if( x>=x1 && x<=x2 )
/* && (point_in_linesegment(x,y1,xstart, ystart, xend, yend) == 1) ) */
{ printf("T\n");
return;
}
/* checking the bottom line of rectangle */
x = value_x_given_y(y3,xstart, ystart, xend, yend);
if( x>=x1 && x<=x2 )
/* && (point_in_linesegment(x,y3,xstart, ystart, xend, yend) == 1) ) */
{ printf("T\n");
return;
}
/* checking the left line of rectangle */
y = value_y_given_x(x1,xstart, ystart, xend, yend);
if( y>=y3 && y<=y1 )
/* && (point_in_linesegment(x1,y,xstart, ystart, xend, yend) == 1) ) */
{ printf("T\n");
return;
}
/* checking the right line of rectangle */
y = value_y_given_x(x2,xstart, ystart, xend, yend);
if( y>=y3 && y<=y1 )
/* && (point_in_linesegment(x2,y,xstart, ystart, xend, yend) == 1) ) */
{ printf("T\n");
return;
}
/* When non of the cases appeared */
printf("F\n");
return;
}
int point_in_linesegment(float x,float y,int xstart,int ystart,int xend,int yend)
{float a,b,len_sqr,a1,b1,len;
/* we are assuming that line segm is actually a line */
if( (( x== xend)&&(y == yend) )||( (x == xstart)&&(y == ystart) ) )
return(1);/* 1 means the point is on the line segment */
if( xstart == xend )/* vertical line */
{ if(x != xstart)
return(0);
if( y >= ystart && y <= yend )
return(1);
if( y <= ystart && y >= yend )
return(1);
return(0);
}
if( ystart == yend )/* horizontal line */
{ if(y != ystart)
return(0);
if( x >= xstart && x <= xend )
return(1);
if( x <= xstart && x >= xend )
return(1);
return(0);
}
if(x == xstart && y != ystart)
return(0);
if(x == xend && y != yend)
return(0);
if(x != xstart && y == ystart)
return(0);
if(x != xend && y == yend)
return(0);
a = (y - ystart)/(x - xstart);
b = (y - yend)/(x - xend);
if( a != b)
return(0);
else{
if( (y > ystart && y < yend)||(y < ystart && y > yend) )
return(1);
else
return(0);
}
}
float value_y_given_x(int x,int xstart,int ystart,int xend,int yend)
{float a;
a= ystart + (x - xstart)*(yend - ystart)/(xend - xstart);
return(a);
}
float value_x_given_y(int y,int xstart,int ystart,int xend,int yend)
{float b;
b = xstart + (y - ystart)*(xend - xstart)/(yend - ystart);
return(b);
}