题目:给了4个点,求两条线,判断两条线平行,同一条线,相交(给出交点)
反思:1 功能一块块的写,边写边测,更容易一次性AC
2 网上代码多是用 struct point 来定义坐标,下次可以试试
#include <cstdio>
#include <string.h>
#include <cstdlib>
#include <cmath>
#include <ctgmath>
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
//由两个点的坐标,求出所在直线方程,返回y=kx+b中的k、b
void func(double x1,double y1,double x2,double y2,double &k,double &b){
k = (y2-y1)/(x2-x1);
b = y1 - (k*x1);
}
int main()
{
//double x1,y1,x2,y2,k,b;
int num;
cin>>num;
cout<<"INTERSECTING LINES OUTPUT"<<endl;
while (num--) {
double x1,y1,x2,y2,x3,y3,x4,y4;
cin>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4;
//首先处理不垂直x轴的
if((x1!=x2) && (x3!=x4)){
double k1,b1,k2,b2;
func(x1, y1, x2, y2, k1, b1);
func(x3, y3, x4, y4, k2, b2);
//平行
if(k1==k2 && b1!=b2) cout<<"NONE"<<endl;
//一条线
else if(k1==k2 && b1==b2) cout<<"LINE"<<endl;
//相交
else {
double x,y;
x=(b2-b1)/(k1-k2);
y=k1*x + b1;
printf("POINT %.2lf %.2lf\n",x,y);
}
}
//处理垂直x轴的
else{
if (x1==x2 && x4==x3 && x1==x3) cout<<"LINE"<<endl;
else if(x1==x2 && x4==x3 && x1!=x3) cout<<"NONE"<<endl;
else{
if(x1==x2){
double k,b;
func(x3, y3, x4, y4, k, b);
double x = x1;
double y = k*x+b;
printf("POINT %.2lf %.2lf\n",x,y);
}
else{
double k,b;
func(x1, y1, x2, y2, k, b);
double x = x3;
double y = k*x+b;
printf("POINT %.2lf %.2lf\n",x,y);
}
}
}
}
cout<<"END OF OUTPUT"<<endl;
return 0;
}