Intersecting Lines POJ - 1269(思维 数学)

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
这个题浪费了好多时间,有一个情况没有考虑到,唉。wa了好几次。
代码比较长,但是挺好懂,嘿嘿。
代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define ll long long
using namespace std;

double sx1,sy1,ex1,ey1;
double sx2,sy2,ex2,ey2;

int main()
{
	int t;
	scanf("%d",&t);
	printf("INTERSECTING LINES OUTPUT\n");
	while(t--)
	{
		scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&sx1,&sy1,&ex1,&ey1,&sx2,&sy2,&ex2,&ey2);
		double a=ex1-sx1;
		double b=ey1-sy1;
		double c=ex2-sx2;
		double d=ey2-sy2;
		if((a==0&&c==0&&ex1!=ex2)||(b==0&&d==0&&ey1!=ey2))//都是横的或者都是竖的但是不共线
		{
			printf("NONE\n");
			continue;
		}
		if(((a==0&&c==0&&ex1==ex2)||(b==0&&d==0&&ey1==ey2)))//都是横的或者都是竖的但是共线
		{
			printf("LINE\n");
			continue;
		}
		if(a==0&&d==0&&b!=0&&c!=0)//十字交叉
		{
			printf("POINT %.2lf %.2lf\n",ex1,ey2);
			continue;
		}
		if(a!=0&&d!=0&&b==0&&c==0)//十字交叉
		{
			printf("POINT %.2lf %.2lf\n",ex2,ey1);
			continue;
		}
		if(b==0&&a!=0&&c!=0&&d!=0)
		{
			double k2=(double)(ey2-sy2)/(double)(ex2-sx2);
			double b2=(double)(ey2-k2*ex2);
			double x1=(double) (ey1-b2)/(double)k2;
			printf("POINT %.2lf %.2lf\n",x1,ey1);
			continue;
		}
		if(d==0&&a!=0&&c!=0&&b!=0)
		{
			double k1=(double)(ey1-sy1)/(double)(ex1-sx1);
			double b1=(double)(ey1-k1*ex1);
			double x2=(double) (ey2-b1)/(double)k1;
			printf("POINT %.2lf %.2lf\n",x2,ey2);
			continue;
		}
		if(a==0&&b!=0&&c!=0&&d!=0)
		{
			double k2=(double)(ey2-sy2)/(double)(ex2-sx2);
			double b2=(double)(ey2-k2*ex2);
			double y2=k2*ex1+b2;
			printf("POINT %.2lf %.2lf\n",ex1,y2);
			continue;
		}
		if(c==0&&b!=0&&a!=0&&d!=0)
		{
			double k1=(double)(ey1-sy1)/(double)(ex1-sx1);
			double b1=(double)(ey1-k1*ex1);
			double y1=k1*ex2+b1;
			printf("POINT %.2lf %.2lf\n",ex2,y1);
			continue;
		}
		//以下就是正常情况
		double k1=(double)(ey1-sy1)/(double)(ex1-sx1);
		double k2=(double)(ey2-sy2)/(double)(ex2-sx2);
		double b1=(double)(ey1-k1*ex1);
		double b2=(double)(ey2-k2*ex2);
		if(k1==k2&&b1==b2) printf("LINE\n");
		else if(k1==k2&&b1!=b2) printf("NONE\n");
		else
		{
			double x1=(b2-b1)/(k1-k2);
			double y1=k1*x1+b1;
			printf("POINT %.2lf %.2lf\n",x1,y1);
		}	
	}
	printf("END OF OUTPUT\n");
	return 0;
}
/*
1 2 1 3 2 543 2 654
312 1 543 1 32 2 543 2
1 2 1 3 32 2 543 2
43 2 534 2 1 2 1 3
-312 2 32 2 321 4345 13 54
213 35 87 89 -312 2 32 2
1 234 1 765 34 6546 867 987
354 654 -32 124 3 423 3 436
*/

代码比较长,就是按着正常思维来的,还有一种比较简单

#include <stdio.h>
struct Point
{
    double x,y;
};
struct Line
{
    Point a,b;
};
// 为0则平行,为1则同一条直线,为2则相交于一点.
int ispos;
// 交点坐标及斜率、斜距值
double p_x,p_y,k1,k2,b1,b2;
 
double abs(double x)
{
    return x<0?-x:x;
}
 
bool find_k(Line l,int num)
{
    if(l.a.x==l.b.x)    return false;
    if(num==1)
    {
       k1=(l.b.y-l.a.y)/(l.b.x-l.a.x);
       b1=l.a.y-k1*l.a.x;
    }
    else
    {
        k2=(l.b.y-l.a.y)/(l.b.x-l.a.x);
        b2=l.a.y-k2*l.a.x;
    }
    return true;
}
 
void judge(Line m,Line n)
{
    // 两条直线斜率K都不存在,即两直线都垂直于x轴
    // 这里用 & ,让两个函数务必都执行,这样k1,k2,b1,b2都算出来了,可以直接调用了
    if(!find_k(m,1) & !find_k(n,2))
    {
        if(m.a.x==n.a.x)    ispos=1;
        return;
    }
 
    // 有任何一条直线斜率K不存在
    if(!find_k(m,1))
    {
        p_x=m.a.x;
        p_y=k2*p_x+b2;
        ispos=2;
        return;
    }
    else if(!find_k(n,2))
    {
        p_x=n.a.x;
        p_y=k1*p_x+b1;
        ispos=2;
        return;
    }
 
    // 两条直线斜率k都存在
    if(k1==k2)
    {
        if(b1==b2)  ispos=1;
        return;
    }
    p_x=(b2-b1)/(k1-k2);
    p_y=k1*p_x+b1;
    ispos=2;
    return;
}
 
int main()
{
    int n;
    Line l1,l2;
    scanf("%d",&n);
    printf("INTERSECTING LINES OUTPUT\n");
    while(n--)
    {
        p_x=p_y=k1=k2=b1=b2=0;
        scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&l1.a.x,&l1.a.y,&l1.b.x,&l1.b.y,&l2.a.x,&l2.a.y,&l2.b.x,&l2.b.y);
        ispos=0;
        judge(l1,l2);
        if(!ispos)  printf("NONE\n");
        else if(ispos==1)   printf("LINE\n");
        else    printf("POINT %.2lf %.2lf\n",p_x,p_y);
    }
    printf("END OF OUTPUT\n");
    return 0;
}
/*
21 32 2 5 8 3 5 49
312 32 43 4 2 321 432 21
21 435 43 123 432 65  87 554
3 2 8 6 2 5  3 8
213 231 12 321 14 67 65
54 65 76 34 65 76 89 67
*/

努力加油a啊,(o)/~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值