poj1269计算几何直线和直线的关系

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很简单直接暴力分类,类别也不是很多,有一个坑点就是double型的0乘负数会变成负0,太坑了!!这里放一下测试代码
#include<map>
#include<set>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 1000000007

using namespace std;

const int N=100005,maxn=100005,inf=0x3f3f3f3f3f;

int main()
{
    double x=0.0,y=x*(-1);
    printf("%.2f\n",y);
    if(y==0)y=fabs(y);
    printf("%.2f\n",y);
    return 0;
}
#include<map>
#include<set>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 1000000007

using namespace std;

const double eps=1e-8;
const int N=5,maxn=100005,inf=0x3f3f3f3f;

struct point{
    int x,y;
};
struct line{
   point a,b;
}l[N];

int main()
{
    int t;
    double x1,y1,x2,y2,x3,y3,x4,y4;
    cin>>t;
    cout<<"INTERSECTING LINES OUTPUT"<<endl;
    while(t--){
        cin>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4;
        if((y4-y3)*(x2-x1)==(y2-y1)*(x4-x3))
        {
            if((y3-y1)*(x2-x1)!=(y2-y1)*(x3-x1))
                cout<<"NONE"<<endl;
            else
                cout<<"LINE"<<endl;
        }
        else
        {
            double x,y;
            if(x2==x1)
            {
                x=x1;
                y=y3+(x-x3)*(y4-y3)/(x4-x3);
            }
            else if(x3==x4)
            {
                x=x3;
                y=y1+(x-x1)*(y2-y1)/(x2-x1);
            }
            else
            {
                x=(y3-y1+x1*(y2-y1)/(x2-x1)-x3*(y4-y3)/(x4-x3))/((y2-y1)/(x2-x1)-(y4-y3)/(x4-x3));
                y=(x-x1)*(y2-y1)/(x2-x1)+y1;
            }
            if(x==0)x=fabs(x);
            if(y==0)y=fabs(y);
            printf("POINT %.2f %.2f\n",x,y);
        }
    }
    cout<<"END OF OUTPUT"<<endl;
    return 0;
}

又看了一下网上的题解发现有更简单的叉积判断

首先判断斜率是非相同还是用公式直接来(x4-x3)*(y2-y1)==(y4-y3)*(x2-x1)

然后用叉积(x2-x1)*(y3-y1)==(y2-y1)*(x3-x1)判断x3是不是在x1,x2这条线上是的话就是LINE,否则就是NONE

最后叉积计算交点:

设交点(x0,y0)

(x2-x1)*(y0-y1)-(y2-y1)*(x0-x1)=0;

(x4-x3)*(y0-y3)-(y4-y3)*(x0-x3)=0;

化简可得:

(y1-y2)*x0+(x2-x1)*y0+x1*y2-x2*y1=0;

(y3-y4)*x0+(x4-x3)*y0+x3*y4-x4*y3=0;

建立二元一次方程:

a1*x0+b1*y0+c1=0;

a2*x0+b2*y0+c2=0;

解得:

x0=(c2*b1-c1*b2)/(b2*a1-b1*a2);

y0=(a2*c1-a1*c2)/(b2*a1-b1*a2);

带入就好了,以下是新方法 的ac代码:

#include<map>
#include<set>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 1000000007

using namespace std;

const double eps=1e-8;
const int N=5,maxn=100005,inf=0x3f3f3f3f;

struct point{
    double x,y;
};
struct line{
   point a,b;
}l[N];

int main()
{
    int t;
    double x1,x2,x3,x4,y1,y2,y3,y4;
    cin>>t;
    cout<<"INTERSECTING LINES OUTPUT"<<endl;
    while(t--){
        cin>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4;
        if((x4-x3)*(y2-y1)==(y4-y3)*(x2-x1))//斜率判断
        {
            if((x2-x1)*(y3-y1)==(y2-y1)*(x3-x1))cout<<"LINE"<<endl;//用叉积判断共线
            else cout<<"NONE"<<endl;
        }
        else
        {
            double a1=y1-y2,b1=x2-x1,c1=x1*y2-x2*y1;
            double a2=y3-y4,b2=x4-x3,c2=x3*y4-x4*y3;
            double x=(c2*b1-c1*b2)/(b2*a1-b1*a2);
            double y=(a2*c1-a1*c2)/(b2*a1-b1*a2);
            printf("POINT %.2f %.2f\n",x,y);
        }
    }
    cout<<"END OF OUTPUT"<<endl;
    return 0;
}
时间: 2024-10-13 15:19:23

poj1269计算几何直线和直线的关系的相关文章

uva 11178 Morley&amp;#39;s Theorem(计算几何-点和直线)

Problem D Morley's Theorem Input: Standard Input Output: Standard Output Morley's theorem states that that the lines trisecting the angles of an arbitrary plane triangle meet at the vertices of an equilateral triangle. For example in the figure below

uva 11796 Dog Distance (计算几何-点和直线)

C Dog Distance Input Standard Input Output Standard Output Two dogs, Ranga and Banga, are running randomly following two different paths. They both run for T seconds with different speeds. Ranga runs with a constant speed of R m/s, whereas Banga runs

uva 11178 Morley&#39;s Theorem(计算几何-点和直线)

Problem D Morley's Theorem Input: Standard Input Output: Standard Output Morley's theorem states that that the lines trisecting the angles of an arbitrary plane triangle meet at the vertices of an equilateral triangle. For example in the figure below

POJ 3304 Segments 【计算几何】【直线和线段的关系】

题目链接:http://poj.org/problem?id=3304 题目大意:T个case,每个case里面有N条线段,判断能否存在一条直线,使得所有的线段在这条直线上都能有公共点,如果存在输出Yes,否则输出No. 题目的意思可以变成,在N条直线的2*N个端点中选择两个点组成一条直线能否满足这个条件,暴力枚举即可,注意的一点是在枚举的2*n个点中每次选择的两个点要判断是不是重复的. #include<iostream> #include<stdio.h> #include&l

计算几何-----》两直线是否相交

POJ 1127 1 #include<set> 2 #include<map> 3 #include<stack> 4 #include<bitset> 5 #include<cmath> 6 #include<string> 7 #include<vector> 8 #include<cstdio> 9 #include<cstring> 10 #include<iostream>

POJ 1269 - Intersecting Lines 直线与直线相交

题意:    判断直线间位置关系: 相交,平行,重合 1 include <iostream> 2 #include <cstdio> 3 using namespace std; 4 struct Point 5 { 6 int x , y; 7 Point(int a = 0, int b = 0) :x(a), y(b) {} 8 }; 9 struct Line 10 { 11 Point s, e; 12 int a, b, c;//a>=0 13 Line() {

直线与直线相交 直线与线段相交 线段与线段相交

int sgn(double x) { if(fabs(x) < eps) return 0; return x < 0 ? -1:1; } struct Point { double x,y; Point() {} Point(double _x,double _y) { x = _x,y = _y; } Point operator -(const Point &b)const { return Point(x - b.x,y - b.y); } //叉积 double opera

poj 2318 TOYS(计算几何 点与线段的关系)

TOYS Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12015   Accepted: 5792 Description Calculate the number of toys that land in each bin of a partitioned toy box. Mom and dad have a problem - their child John never puts his toys away w

poj 2398 Toy Storage 【计算几何】【点和线的关系】

题目链接:http://poj.org/problem?id=2398 题目大意:这次的题目和前一道题目几乎是一样的,不同之处在于这次给出的线不是有顺序的,还有就是输出的时候有一个优化. 基本的分析见我上篇博客:http://blog.csdn.net/u010468553/article/details/39474007 注意sort函数中cmp的编写还有后期数据的整理 #include<iostream> #include<stdio.h> #include<cstrin