POJ 1269 Intersecting Lines(计算几何)
2016-05-23 12:07
495 查看
D - Intersecting Lines
Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d
& %I64u
Submit Status
Description
We all know that a pair of distinct points on a plane defines a line and that a pair of lines on a plane will intersect in one of three ways: 1) no intersection because they are parallel, 2) intersect in a line because they are on top of one another (i.e. they
are the same line), 3) intersect in a point. In this problem you will use your algebraic knowledge to create a program that determines how and where two lines intersect.
Your program will repeatedly read in four points that define two lines in the x-y plane and determine how and where the lines intersect. All numbers required by this problem will be reasonable, say between -1000 and 1000.
Input
The first line contains an integer N between 1 and 10 describing how many pairs of lines are represented. The next N lines will each contain eight integers. These integers represent the coordinates of four points on the plane in the order x1y1x2y2x3y3x4y4.
Thus each of these input lines represents two lines on the plane: the line through (x1,y1) and (x2,y2) and the line through (x3,y3) and (x4,y4). The point (x1,y1) is always distinct from (x2,y2). Likewise with (x3,y3) and (x4,y4).
Output
There should be N+2 lines of output. The first line of output should read INTERSECTING LINES OUTPUT. There will then be one line of output for each pair of planar lines represented by a line of input, describing how the lines intersect: none, line, or point.
If the intersection is a point then your program should output the x and y coordinates of the point, correct to two decimal places. The final line of output should read "END OF OUTPUT".
Sample Input
Sample Output
题意:给定4个点的坐标,前2个点是一条线,后2个点是另一条线,求这两条线的关系,如果相交,就输出交点。
题解:先判断是否共线,我用的是叉积的性质,用了2遍就可以判断4个点是否共线了,在用斜率判断是否平行,最后就是相交了,求交点就好了。
求交点的过程和高中知识差不多,用y=kx+c来求,只不过要注意斜率不存在的时候特殊处理,还有就是求斜率的时候一定要强制转换,(坑爹的我,调试了一小时才找到这个bug
)
AC代码:
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long ll;
typedef unsigned long long ull;
#define prN printf("\n")
#define SI(N) scanf("%d",&(N))
#define SII(N,M) scanf("%d%d",&(N),&(M))
#define SIII(N,M,K) scanf("%d%d%d",&(N),&(M),&(K))
#define cle(a,val) memset(a,(val),sizeof(a))
#define rep(i,b) for(int i=0;i<(b);i++)
#define Rep(i,a,b) for(int i=(a);i<=(b);i++)
#define reRep(i,a,b) for(int i=(a);i>=(b);i--)
const int MAX_N= 1 ;
const int EPS= 1e-9 ;
int a[10][8],n;
int Multic(int x0,int y0,int x1,int y1,int x2,int y2)
{
int a1=x1-x0,b1=y1-y0;
int a2=x2-x0,b2=y2-y0;
return a1*b2-a2*b1;
}
void sol(int row)
{
int te1=Multic(a[row][0],a[row][1],a[row][4],a[row][5],a[row][2],a[row][3]);
int te2=Multic(a[row][0],a[row][1],a[row][6],a[row][7],a[row][2],a[row][3]);
if (te1==0&&te2==0)
{
puts
4000
("LINE");
return;
}
if ((a[row][1]-a[row][3])*(a[row][4]-a[row][6])==
(a[row][0]-a[row][2])*(a[row][5]-a[row][7]))
{
puts("NONE");
return;
}
if (a[row][0]-a[row][2]==0||(a[row][4]-a[row][6])==0)
{
if (a[row][0]-a[row][2]==0)
{
double ansx=a[row][0];
double k2=(a[row][5]-a[row][7])/(double)(a[row][4]-a[row][6]);
double c2=a[row][7]-k2*a[row][6];
double ansy=k2*ansx+c2;
printf("POINT %.2f %.2f\n",ansx,ansy);
}
if ((a[row][4]-a[row][6])==0)
{
double ansx=a[row][4];
double k1=(a[row][1]-a[row][3])/(double)(a[row][0]-a[row][2]);
double c1=a[row][1]-k1*a[row][0];
double ansy=k1*ansx+c1;
}
return;
}
double k1=(a[row][1]-a[row][3])/(double)(a[row][0]-a[row][2]);///一定要注意这 ,要强制类型转换!!!!!!!!!!!!
double k2=(a[row][5]-a[row][7])/(double)(a[row][4]-a[row][6]);///否则就是int除int了
double c1=a[row][1]-k1*a[row][0];
double c2=a[row][7]-k2*a[row][6];
double ansx=(c2-c1)/(k1-k2);
double ansy=k1*ansx+c1;
printf("POINT %.2f %.2f\n",ansx,ansy);
}
int main()
{
SI(n);
rep(i,n)
rep(j,8)
SI(a[i][j]);
puts("INTERSECTING LINES OUTPUT");
rep(i,n)
sol(i);
puts("END OF OUTPUT");
return 0;
}
Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d
& %I64u
Submit Status
Description
We all know that a pair of distinct points on a plane defines a line and that a pair of lines on a plane will intersect in one of three ways: 1) no intersection because they are parallel, 2) intersect in a line because they are on top of one another (i.e. they
are the same line), 3) intersect in a point. In this problem you will use your algebraic knowledge to create a program that determines how and where two lines intersect.
Your program will repeatedly read in four points that define two lines in the x-y plane and determine how and where the lines intersect. All numbers required by this problem will be reasonable, say between -1000 and 1000.
Input
The first line contains an integer N between 1 and 10 describing how many pairs of lines are represented. The next N lines will each contain eight integers. These integers represent the coordinates of four points on the plane in the order x1y1x2y2x3y3x4y4.
Thus each of these input lines represents two lines on the plane: the line through (x1,y1) and (x2,y2) and the line through (x3,y3) and (x4,y4). The point (x1,y1) is always distinct from (x2,y2). Likewise with (x3,y3) and (x4,y4).
Output
There should be N+2 lines of output. The first line of output should read INTERSECTING LINES OUTPUT. There will then be one line of output for each pair of planar lines represented by a line of input, describing how the lines intersect: none, line, or point.
If the intersection is a point then your program should output the x and y coordinates of the point, correct to two decimal places. The final line of output should read "END OF 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个点的坐标,前2个点是一条线,后2个点是另一条线,求这两条线的关系,如果相交,就输出交点。
题解:先判断是否共线,我用的是叉积的性质,用了2遍就可以判断4个点是否共线了,在用斜率判断是否平行,最后就是相交了,求交点就好了。
求交点的过程和高中知识差不多,用y=kx+c来求,只不过要注意斜率不存在的时候特殊处理,还有就是求斜率的时候一定要强制转换,(坑爹的我,调试了一小时才找到这个bug
)
AC代码:
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long ll;
typedef unsigned long long ull;
#define prN printf("\n")
#define SI(N) scanf("%d",&(N))
#define SII(N,M) scanf("%d%d",&(N),&(M))
#define SIII(N,M,K) scanf("%d%d%d",&(N),&(M),&(K))
#define cle(a,val) memset(a,(val),sizeof(a))
#define rep(i,b) for(int i=0;i<(b);i++)
#define Rep(i,a,b) for(int i=(a);i<=(b);i++)
#define reRep(i,a,b) for(int i=(a);i>=(b);i--)
const int MAX_N= 1 ;
const int EPS= 1e-9 ;
int a[10][8],n;
int Multic(int x0,int y0,int x1,int y1,int x2,int y2)
{
int a1=x1-x0,b1=y1-y0;
int a2=x2-x0,b2=y2-y0;
return a1*b2-a2*b1;
}
void sol(int row)
{
int te1=Multic(a[row][0],a[row][1],a[row][4],a[row][5],a[row][2],a[row][3]);
int te2=Multic(a[row][0],a[row][1],a[row][6],a[row][7],a[row][2],a[row][3]);
if (te1==0&&te2==0)
{
puts
4000
("LINE");
return;
}
if ((a[row][1]-a[row][3])*(a[row][4]-a[row][6])==
(a[row][0]-a[row][2])*(a[row][5]-a[row][7]))
{
puts("NONE");
return;
}
if (a[row][0]-a[row][2]==0||(a[row][4]-a[row][6])==0)
{
if (a[row][0]-a[row][2]==0)
{
double ansx=a[row][0];
double k2=(a[row][5]-a[row][7])/(double)(a[row][4]-a[row][6]);
double c2=a[row][7]-k2*a[row][6];
double ansy=k2*ansx+c2;
printf("POINT %.2f %.2f\n",ansx,ansy);
}
if ((a[row][4]-a[row][6])==0)
{
double ansx=a[row][4];
double k1=(a[row][1]-a[row][3])/(double)(a[row][0]-a[row][2]);
double c1=a[row][1]-k1*a[row][0];
double ansy=k1*ansx+c1;
}
return;
}
double k1=(a[row][1]-a[row][3])/(double)(a[row][0]-a[row][2]);///一定要注意这 ,要强制类型转换!!!!!!!!!!!!
double k2=(a[row][5]-a[row][7])/(double)(a[row][4]-a[row][6]);///否则就是int除int了
double c1=a[row][1]-k1*a[row][0];
double c2=a[row][7]-k2*a[row][6];
double ansx=(c2-c1)/(k1-k2);
double ansy=k1*ansx+c1;
printf("POINT %.2f %.2f\n",ansx,ansy);
}
int main()
{
SI(n);
rep(i,n)
rep(j,8)
SI(a[i][j]);
puts("INTERSECTING LINES OUTPUT");
rep(i,n)
sol(i);
puts("END OF OUTPUT");
return 0;
}
相关文章推荐
- 简单的四则运算
- 数的奇偶性
- ACMer博客瀑布流分析
- ACM程序设计大赛题目分类
- 计算字符串最后一个单词长度
- ACM网址
- 1272 小希的迷宫
- 1272 小希的迷宫
- hdu 1250 大数相加并用数组储存
- 矩阵的乘法操作
- 蚂蚁爬行问题
- 蚂蚁爬行问题
- 求两个数的最大公约数【ACM基础题】
- 打印出二进制中所有1的位置
- 杭电题目---一只小蜜蜂
- HDOJ 1002 A + B Problem II (Big Numbers Addition)
- 初学ACM - 半数集(Half Set)问题 NOJ 1010 / FOJ 1207
- 初学算法 - 求凸包的Garham's Scan算法的C++实现
- 初学ACM - 组合数学基础题目PKU 1833
- ACM题库以及培养策略