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=,maxn=,inf=0x3f3f3f3f3f; int main()
{
double x=0.0,y=x*(-);
printf("%.2f\n",y);
if(y==)y=fabs(y);
printf("%.2f\n",y);
return ;
}
#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-;
const int N=,maxn=,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==)x=fabs(x);
if(y==)y=fabs(y);
printf("POINT %.2f %.2f\n",x,y);
}
}
cout<<"END OF OUTPUT"<<endl;
return ;
}

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

首先判断斜率是非相同还是用公式直接来(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-;
const int N=,maxn=,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 ;
}

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

  1. POJ1269求两个直线的关系平行,重合,相交

    依旧是叉积的应用 判定重合:也就是判断给定的点是否共线的问题——叉积为0 if(!cross(p1,p2,p3) && !cross(p1,p2,p4))printf("LI ...

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

    Problem D Morley's Theorem Input: Standard Input Output: Standard Output Morley's theorem states tha ...

  3. 计算几何——线段和直线判交点poj3304

    #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #i ...

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

    题意:    判断直线间位置关系: 相交,平行,重合 include <iostream> #include <cstdio> using namespace std; str ...

  5. BZOJ 1007: [HNOI2008]水平可见直线 平面直线

    1007: [HNOI2008]水平可见直线 Description 在xoy直角坐标平面上有n条直线L1,L2,...Ln,若在y值为正无穷大处往下看,能见到Li的某个子线段,则称Li为可见的,否则 ...

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

    TOYS Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12015   Accepted: 5792 Description ...

  7. UVa 11437:Triangle Fun(计算几何综合应用,求直线交点,向量运算,求三角形面积)

    Problem ATriangle Fun Input: Standard Input Output: Standard Output In the picture below you can see ...

  8. hdu 2857:Mirror and Light(计算几何,点关于直线的对称点,求两线段交点坐标)

    Mirror and Light Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. Intersecting Lines (计算几何基础+判断两直线的位置关系)

    题目链接:http://poj.org/problem?id=1269 题面: Description We all know that a pair of distinct points on a ...

随机推荐

  1. Java 中的数组

    1.声明数组String [] arr;int arr1[];String[] array=new String[5];int score[]=new int[3]; 2.初始化数组://静态初始化i ...

  2. 简单的add函数的N种写法

    最近在学习es6,看到for-of这里,就想自己写着练习一下,于是就准备写一个小函数add来求和.函数很简单,如add(1,2,3)这样.于是我开始着手 一开始我是这么写的 function add( ...

  3. Python中的元祖Tuple

    Python中的元祖和list基本上一样 tuple = () # 表示一个空的元祖 tuple = (50, ) # 元组中只有一个元素的时候,必须在后面加上逗号 无关闭分隔符 任意无符号的对象,以 ...

  4. java根据HashMap中的值将其元素排序

    思路:HashMap或Map本身没有排序功能,若要进行较轻松的排序,可利用ArrayList中的sort方法 例子: import java.util.ArrayList; import java.u ...

  5. Grafana关键表结构分析

    Grafana默认使用SQLite存储数据表,默认数据库文件存储在/var/lib/grafana/grafana.db中,可以将文件拷贝到Widnows中,使用Navicat for SQLite进 ...

  6. OpenStack及其构成简介

    新的一年新的开始,突然想学习下Openstack,之前了解过很多,但是想系统的学习一下,第一次写博客,只想把学到的东西记录下来加深印象,如有写的不好的地方请多多见谅.下面开门见山. 1.What is ...

  7. Tcl与Design Compiler (十一)——其他的时序约束选项(二)

    本文如果有错,欢迎留言更正:此外,转载请标明出处 http://www.cnblogs.com/IClearner/  ,作者:IC_learner 前面介绍的设计都不算很复杂,都是使用时钟的默认行为 ...

  8. Java集合总结系列2:Collection接口

    Collection 接口是 Java 集合类的一个根接口,Java 在 Collection 接口中定义了许多通用的数据操作类方法以及判断类方法. 通过查看 API 文档或源码的方式,我们可以了解到 ...

  9. webService请求方式快速生成代码 (Postman)

    Postman 这个东西只能在外网下载,是Goole一个插件. 1.FQ到外网,这里就不具体介绍怎么FQ了 2.上到谷歌浏览器,找到更过工具--->扩张程序--->获取更多扩张程序 3.在 ...

  10. JDBC基础学习(六)—数据库连接池

    一.数据库连接池介绍 1.数据库连接池的缘由      对于一个简单的数据库应用,由于对于数据库的访问不是很频繁.这时可以简单地在需要访问数据库时,就新创建一个连接,用完后就关闭它,这样做也不会带来什 ...