poj Intersection 1410 (数学几何 求线段方程)
2015-10-29 21:14
337 查看
Intersection
Description
You are to write a program that has to decide whether a given line segment intersects a given rectangle.
An example:
line: start point: (4,9)
end point: (11,2)
rectangle: left-top: (1,5)
right-bottom: (7,1)
Figure 1: Line segment does not intersect rectangle
The line is said to intersect the rectangle if the line and the rectangle have at least one point in common. The rectangle consists of four straight lines and the area in between. Although all input values are integer numbers, valid intersection points do not
have to lay on the integer grid.
Input
The input consists of n test cases. The first line of the input file contains the number n. Each following line contains one test case of the format:
xstart ystart xend yend xleft ytop xright ybottom
where (xstart, ystart) is the start and (xend, yend) the end point of the line and (xleft, ytop) the top left and (xright, ybottom) the bottom right corner of the rectangle. The eight numbers are separated by a blank. The terms top left and bottom right do
not imply any ordering of coordinates.
Output
For each test case in the input file, the output file should contain a line consisting either of the letter "T" if the line segment intersects the rectangle or the letter "F" if the line segment does not intersect the rectangle.
Sample Input
Sample Output
F
题意
给定一条线段的起点和终点,再给定一个矩形的起点和终点(对角线两点),问线段是否和矩形所包含的区域有交点。
思路:
先求出线段所在直线的方程a*x+b*y+c=0;(用到最原始的求直线方程的公式(可能是小学或者初中学的,但当时就是不知道咋写.......))。
然后判断线段是否与矩形所在区域有交点。具体看代码。。。
#include<stdio.h>
#include<string.h>
struct zz
{
int x;
int y;
}lb,le,sb,se;
int main()
{
int t,num1,num2;
int a,b,c,s;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d%d%d%d%d%d",&lb.x,&lb.y,&le.x,&le.y,&sb.x,&sb.y,&se.x,&se.y);
{
a=lb.y-le.y;
b=le.x-lb.x;
c=le.y*lb.x-lb.y*le.x;
if(sb.x>se.x)
{
s=sb.x;sb.x=se.x;se.x=s;
}
if(sb.y<se.y)
{
s=sb.y;sb.y=se.y;se.y=s;
}
num1=(a*sb.x+b*sb.y+c)*(a*se.x+b*se.y+c);
num2=(a*sb.x+b*se.y+c)*(a*se.x+b*sb.y+c);
if(num1>0&&num2>0)
{
printf("F\n");
continue;
}
if((lb.x<sb.x&&le.x<sb.x)||(lb.x>se.x&&le.x>se.x)||(lb.y>sb.y&&le.y>sb.y)||(lb.y<se.y&&le.y<se.y))
printf("F\n");
else
printf("T\n");
}
}
return 0;
}
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 13147 | Accepted: 3426 |
You are to write a program that has to decide whether a given line segment intersects a given rectangle.
An example:
line: start point: (4,9)
end point: (11,2)
rectangle: left-top: (1,5)
right-bottom: (7,1)
Figure 1: Line segment does not intersect rectangle
The line is said to intersect the rectangle if the line and the rectangle have at least one point in common. The rectangle consists of four straight lines and the area in between. Although all input values are integer numbers, valid intersection points do not
have to lay on the integer grid.
Input
The input consists of n test cases. The first line of the input file contains the number n. Each following line contains one test case of the format:
xstart ystart xend yend xleft ytop xright ybottom
where (xstart, ystart) is the start and (xend, yend) the end point of the line and (xleft, ytop) the top left and (xright, ybottom) the bottom right corner of the rectangle. The eight numbers are separated by a blank. The terms top left and bottom right do
not imply any ordering of coordinates.
Output
For each test case in the input file, the output file should contain a line consisting either of the letter "T" if the line segment intersects the rectangle or the letter "F" if the line segment does not intersect the rectangle.
Sample Input
1 4 9 11 2 1 5 7 1
Sample Output
F
题意
给定一条线段的起点和终点,再给定一个矩形的起点和终点(对角线两点),问线段是否和矩形所包含的区域有交点。
思路:
先求出线段所在直线的方程a*x+b*y+c=0;(用到最原始的求直线方程的公式(可能是小学或者初中学的,但当时就是不知道咋写.......))。
然后判断线段是否与矩形所在区域有交点。具体看代码。。。
#include<stdio.h>
#include<string.h>
struct zz
{
int x;
int y;
}lb,le,sb,se;
int main()
{
int t,num1,num2;
int a,b,c,s;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d%d%d%d%d%d",&lb.x,&lb.y,&le.x,&le.y,&sb.x,&sb.y,&se.x,&se.y);
{
a=lb.y-le.y;
b=le.x-lb.x;
c=le.y*lb.x-lb.y*le.x;
if(sb.x>se.x)
{
s=sb.x;sb.x=se.x;se.x=s;
}
if(sb.y<se.y)
{
s=sb.y;sb.y=se.y;se.y=s;
}
num1=(a*sb.x+b*sb.y+c)*(a*se.x+b*se.y+c);
num2=(a*sb.x+b*se.y+c)*(a*se.x+b*sb.y+c);
if(num1>0&&num2>0)
{
printf("F\n");
continue;
}
if((lb.x<sb.x&&le.x<sb.x)||(lb.x>se.x&&le.x>se.x)||(lb.y>sb.y&&le.y>sb.y)||(lb.y<se.y&&le.y<se.y))
printf("F\n");
else
printf("T\n");
}
}
return 0;
}
相关文章推荐
- 第5次作业答案跟帖
- 详解哈希算法、哈希表
- 测试java连接mysql的语句
- webApplicationContext 与servletContext
- cmd常用命令
- MFC修改空间ID值
- 每天学一点flex(5):DividedBox和拖拽监听
- java基础之继承extends,以及子类实例化过程
- 4Sum
- CentOS系统下xinetd启动问题
- IOS开发笔记27-应用管理之MVC模式
- Xen的安装、编译和使用(二)
- R语言学习之包的加载和日期包的使用
- 矩阵乘法+快速幂求斐波那契
- 基于saltstack svn的一个发布系统
- 二分图的最大匹配
- hdoj--1251--统计难题(字典树)
- 挖掘频繁模、关联和相关性(2)
- android studio使用jcenter上的开源库
- SOM-TL28335核心板规格书