G - 浮点数运算(几何)
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
整体思路:
个人认为最好先别急着敲,先用笔求解一遍,然后再将思路转化为代码,
同时有几个点需要注意:
注意浮点数不能直接相等,只要二者的差的绝对值在一定范围内就表示二者相等。
还要考虑斜率不存在的情况。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
int f1,f2;
using namespace std;
class Point{
public:
float x,y;
};
void in(Point &a){
cin>>a.x;
cin>>a.y;
}
void out(Point &a){
//os<<a.x<<" "<<a.y;
printf("%.2f %.2f",a.x,a.y);
}
float qk(Point a,Point b){//求斜率k
float k;
if(b.x==a.x){
f1=1;
return 0;
}
k=(b.y-a.y)/(b.x-a.x);
return k;
}
float qk1(Point a,Point b){//求斜率k
float k;
if(b.x==a.x){
f2=1;
return 0;
}
k=(b.y-a.y)/(b.x-a.x);
return k;
}
float qb(Point a,Point b,float k){//求与y轴焦点b;
float b1;
b1=a.y-k*a.x;
return b1;
}
//Point qj1(Point a,Point b)
int main(){
int n;
cin>>n;
cout<<"INTERSECTING LINES OUTPUT"<<endl;
for(int i11=0;i11<n;i11++){
f1=0;
f2=0;
Point a1,a2,b1,b2;
in(a1);
in(a2);
in(b1);
in(b2);
float k1=0,k2=0;
k1=qk(a1,a2);
k2=qk1(b1,b2);
if(f1==1&&f2==1){
if(a1.x==b1.x){
cout<<"LINE"<<endl;
continue;
}
else{
cout<<"NONE"<<endl;
continue;
}
}
float yb1,yb2;//与y轴交点b;
if(f1!=1){
yb1=qb(a1,a2,k1);
}
if(f2!=1){
yb2=qb(b1,b2,k2);
}
Point jd;
if(f1==1){
jd.x=a1.x;
jd.y=k2*a1.x+yb2;
}
else if(f2==1){
jd.x=a2.x;
jd.y=k1*a2.x+yb1;
}
else{
float k3=fabs(k1-k2);
if(k3<0.0001){
float yb3=fabs(yb1-yb2);
if(yb3<0.0001){
cout<<"LINE"<<endl;
continue;
}
else{
cout<<"NONE"<<endl;
continue;
}
}
jd.x=(yb2-yb1)/(k1-k2);
jd.y=k1*jd.x+yb1;
}
cout<<"POINT ";
out(jd);
cout<<endl;
// cout<<a1.x<<endl;
// cout<<a1.y<<endl;;
// cout<<a1<<endl;
}
cout<<"END OF OUTPUT"<<endl;
}
上一篇: 与诸葛亮相比,为何司马懿总是能将数十万蜀汉大军拒之门外?
下一篇: 女同事露露