题意:给定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("
")
#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("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
",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
",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;
}