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

  1. 5
  2. 0 0 4 4 0 4 4 0
  3. 5 0 7 6 1 0 2 3
  4. 5 0 7 6 3 -6 4 -3
  5. 2 0 2 27 1 5 18 5
  6. 0 3 4 0 1 2 2 5

Sample Output

  1. INTERSECTING LINES OUTPUT
  2. POINT 2.00 2.00
  3. NONE
  4. LINE
  5. POINT 2.00 5.00
  6. POINT 1.07 2.20
  7. END OF OUTPUT
    很简单直接暴力分类,类别也不是很多,有一个坑点就是double型的0乘负数会变成负0,太坑了!!
    这里放一下测试代码
  1. #include<map>
  2. #include<set>
  3. #include<list>
  4. #include<cmath>
  5. #include<queue>
  6. #include<stack>
  7. #include<vector>
  8. #include<cstdio>
  9. #include<cstring>
  10. #include<iostream>
  11. #include<algorithm>
  12. #define pi acos(-1)
  13. #define ll long long
  14. #define mod 1000000007
  15.  
  16. using namespace std;
  17.  
  18. const int N=,maxn=,inf=0x3f3f3f3f3f;
  19.  
  20. int main()
  21. {
  22. double x=0.0,y=x*(-);
  23. printf("%.2f\n",y);
  24. if(y==)y=fabs(y);
  25. printf("%.2f\n",y);
  26. return ;
  27. }
  1. #include<map>
  2. #include<set>
  3. #include<list>
  4. #include<cmath>
  5. #include<queue>
  6. #include<stack>
  7. #include<vector>
  8. #include<cstdio>
  9. #include<cstring>
  10. #include<iostream>
  11. #include<algorithm>
  12. #define pi acos(-1)
  13. #define ll long long
  14. #define mod 1000000007
  15.  
  16. using namespace std;
  17.  
  18. const double eps=1e-;
  19. const int N=,maxn=,inf=0x3f3f3f3f;
  20.  
  21. struct point{
  22. int x,y;
  23. };
  24. struct line{
  25. point a,b;
  26. }l[N];
  27.  
  28. int main()
  29. {
  30. int t;
  31. double x1,y1,x2,y2,x3,y3,x4,y4;
  32. cin>>t;
  33. cout<<"INTERSECTING LINES OUTPUT"<<endl;
  34. while(t--){
  35. cin>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4;
  36. if((y4-y3)*(x2-x1)==(y2-y1)*(x4-x3))
  37. {
  38. if((y3-y1)*(x2-x1)!=(y2-y1)*(x3-x1))
  39. cout<<"NONE"<<endl;
  40. else
  41. cout<<"LINE"<<endl;
  42. }
  43. else
  44. {
  45. double x,y;
  46. if(x2==x1)
  47. {
  48. x=x1;
  49. y=y3+(x-x3)*(y4-y3)/(x4-x3);
  50. }
  51. else if(x3==x4)
  52. {
  53. x=x3;
  54. y=y1+(x-x1)*(y2-y1)/(x2-x1);
  55. }
  56. else
  57. {
  58. x=(y3-y1+x1*(y2-y1)/(x2-x1)-x3*(y4-y3)/(x4-x3))/((y2-y1)/(x2-x1)-(y4-y3)/(x4-x3));
  59. y=(x-x1)*(y2-y1)/(x2-x1)+y1;
  60. }
  61. if(x==)x=fabs(x);
  62. if(y==)y=fabs(y);
  63. printf("POINT %.2f %.2f\n",x,y);
  64. }
  65. }
  66. cout<<"END OF OUTPUT"<<endl;
  67. return ;
  68. }

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

首先判断斜率是非相同还是用公式直接来(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代码:

  1. #include<map>
  2. #include<set>
  3. #include<list>
  4. #include<cmath>
  5. #include<queue>
  6. #include<stack>
  7. #include<vector>
  8. #include<cstdio>
  9. #include<cstring>
  10. #include<iostream>
  11. #include<algorithm>
  12. #define pi acos(-1)
  13. #define ll long long
  14. #define mod 1000000007
  15.  
  16. using namespace std;
  17.  
  18. const double eps=1e-;
  19. const int N=,maxn=,inf=0x3f3f3f3f;
  20.  
  21. struct point{
  22. double x,y;
  23. };
  24. struct line{
  25. point a,b;
  26. }l[N];
  27.  
  28. int main()
  29. {
  30. int t;
  31. double x1,x2,x3,x4,y1,y2,y3,y4;
  32. cin>>t;
  33. cout<<"INTERSECTING LINES OUTPUT"<<endl;
  34. while(t--){
  35. cin>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4;
  36. if((x4-x3)*(y2-y1)==(y4-y3)*(x2-x1))//斜率判断
  37. {
  38. if((x2-x1)*(y3-y1)==(y2-y1)*(x3-x1))cout<<"LINE"<<endl;//用叉积判断共线
  39. else cout<<"NONE"<<endl;
  40. }
  41. else
  42. {
  43. double a1=y1-y2,b1=x2-x1,c1=x1*y2-x2*y1;
  44. double a2=y3-y4,b2=x4-x3,c2=x3*y4-x4*y3;
  45. double x=(c2*b1-c1*b2)/(b2*a1-b1*a2);
  46. double y=(a2*c1-a1*c2)/(b2*a1-b1*a2);
  47. printf("POINT %.2f %.2f\n",x,y);
  48. }
  49. }
  50. cout<<"END OF OUTPUT"<<endl;
  51. return ;
  52. }

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. iOS网络编程笔记——社交网络编程

    社交网络编程主要使用iOS提供的social框架,目前social框架主要分为两个类: (1)SLComposeViewController提供撰写社交信息(如微博信息)的视图控制器,由iOS系统提供 ...

  2. mysql---数据控制语言(用户及其权限管理)

    用户管理 用户数据所在位置: mysql中的所有用户,都存储在系统数据库(mysql)中的user 表中--不管哪个数据库的用户,都存储在这里.

  3. C++数据

    const :常量 ~x == -++x == -(x+1)   二进制数,1变为0,0变为1 ^                               相同为0,不同为1 &      ...

  4. 【SF】开源的.NET CORE 基础管理系统 - 安装篇

    [SF]开源的.NET CORE 基础管理系统 -系列导航 1.开发必备工具 IDE:VS2017 运行环境:netcoreapp1.1 数据库:SQL Server 2012+ 2.获取最新源代码 ...

  5. Cassandra-java操作——基本操作

    接着上篇博客,我们来谈谈java操作cassandra; 上篇博客的环境:jdk1.7 + python2.7.10 + cassandra2.2.8; 由于2.2.8没有对应的驱动文档,那么我们就用 ...

  6. iOS开发之Autolayout

    1.概述 在以前的iOS程序中,是如何设置布局UI界面的? (1)经常编写大量的坐标计算代码 (2)为了保证在3.5 inch和4.0 inch屏幕上都能有完美的UI界面效果,有时还需要分别为2种屏幕 ...

  7. Java单例模式再加强——按组多单例

    最近要使用alibaba的rocket mq(我们公司对其进行了封装,使其运行在dotNet平台上,Java还是和原生的差不多,涉及公司的内容本文不会提及),其中 在生产者组这一块,建议是用单例模式的 ...

  8. webpack使用总结

    我们可以在js中引入样式文件 require('myStyle.css') 这时我们便需要引入相应的webpack loader来帮助我们解析这段代码. 一般来说需要引入css-loader和styl ...

  9. BZOJ3224普通平衡树【Splay】

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 11751  Solved: 5013 Descriptio ...

  10. 聊天界面使用IQKeyboardManager导航栏及整个页面上移的解决方法

    问题: 使用第三方库IQKeyboardManager时会使整个页面上移,导航栏页偏移出了显示范围.在聊天界面就会使得上面的消息看不到. 解决方法: 首先说明:在聊天界面使用IQKeyboardMan ...