标签:des style blog io strong 数据 for ar
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 10942 | Accepted: 4917 |
Description
Input
Output
Sample Input
5 0 0 4 4 0 4 4 0 5 0 7 6 1 0 2 3 5 0 7 6 3 -6 4 -3 2 0 2 27 1 5 18 5 0 3 4 0 1 2 2 5
Sample Output
INTERSECTING LINES OUTPUT POINT 2.00 2.00 NONE LINE POINT 2.00 5.00 POINT 1.07 2.20 END OF OUTPUT
算法分析:
注意此题是基于 直线 运行的测试的!
每组数据包括4个点,也就是两条直线,请输出这两条直线的关系。
1)相交的话 输出交点(保留2位小数);
2) 平行不相交 输出: NONE ;
3) 平行重合 输出: LINE
利用数学向量的叉积 判断:两条直线是否相交
(1)若不相交:利用 叉积判断 3点是否共线, 若共线则 重合; 否则 平行不相交
(2)若相交 :利用如下公式计算交点
P.y = A.y - (A.y-B.y)*t;
Accepted:
#include <stdio.h>
#include <string.h>
struct N
{
double x, y;
}s[10], p ;
int main()
{
int n;
int i;
scanf("%d", &n);
printf("INTERSECTING LINES OUTPUT\n");
for(i=0; i<n; i++)
{
scanf("%lf %lf %lf %lf", &s[0].x, &s[0].y, &s[1].x, &s[1].y );
scanf("%lf %lf %lf %lf", &s[2].x, &s[2].y, &s[3].x, &s[3].y );
//向量法判断是否相交
//向量法求直线交点
double t, d, f;
double dd = ( (s[1].x-s[0].x)*(s[3].y-s[2].y) - (s[1].y-s[0].y)*(s[3].x-s[2].x) );
if( dd==0 ) // 区分平行与重合
{
double ff=( (s[2].x-s[0].x)*(s[3].y-s[0].y) - (s[2].y-s[0].y)*(s[3].x-s[0].x) );
if(ff==0)
printf("LINE\n");
else
printf("NONE\n");
}
else //相交的情况
{
d = ( (s[2].x-s[0].x)*(s[3].y-s[2].y) - (s[2].y-s[0].y)*(s[3].x-s[2].x ) );
f = ( (s[1].x-s[0].x)*(s[3].y-s[2].y) - (s[1].y-s[0].y)*(s[3].x-s[2].x ) );
t = d/f;
p.x = s[0].x - (s[0].x-s[1].x)*t;
p.y = s[0].y - (s[0].y-s[1].y)*t;
printf("POINT %.2lf %.2lf\n", p.x, p.y );
}
}
printf("END OF OUTPUT\n");
return 0;
}
POJ 1269 Intersecting Lines,布布扣,bubuko.com
标签:des style blog io strong 数据 for ar
原文地址:http://www.cnblogs.com/yspworld/p/3913488.html