Intersecting Lines

http://poj.org/problem?id=1269

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 18897   Accepted: 8043

Description

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

先判断是否平行,平行的话再判断是否共线,否则把向量转换成方程,计算交点

直线的一般式方程AX+BY+C=0中,A B C分别等于:
A = Y2 - Y1
B = X1 - X2
C = X2*Y1 - X1*Y2
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#define esp 0.00000001
using namespace std; struct Vector{
double x,y;
}; struct Line{
Vector s,e;
}line[]; double Cross(Vector a,Vector b,Vector c){
return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
} int main(){
int n;
cin>>n;
Vector a,b,c,d;
double tmp;
cout<<"INTERSECTING LINES OUTPUT"<<endl;
for(int i=;i<=n;i++){
cin>>a.x>>a.y>>b.x>>b.y>>c.x>>c.y>>d.x>>d.y;
tmp=(a.x-b.x)*(c.y-d.y)-(a.y-b.y)*(c.x-d.x);
if(fabs(tmp)<esp&&fabs(Cross(a,b,d))<esp){
cout<<"LINE"<<endl;
}
else if(fabs(tmp)<esp){
cout<<"NONE"<<endl;
}
else{
double a1=a.y-b.y,b1=b.x-a.x,c1=a.x*b.y-b.x*a.y;//c是叉积
double a2=c.y-d.y,b2=d.x-c.x,c2=c.x*d.y-d.x*c.y;
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;
}

Intersecting Lines(叉积,方程)的更多相关文章

  1. poj 1269 Intersecting Lines——叉积求直线交点坐标

    题目:http://poj.org/problem?id=1269 相关知识: 叉积求面积:https://www.cnblogs.com/xiexinxinlove/p/3708147.html什么 ...

  2. Intersecting Lines - POJ 1269(判断平面上两条直线的关系)

    分析:有三种关系,共线,平行,还有相交,共线和平行都可以使用叉积来进行判断(其实和斜率一样),相交需要解方程....在纸上比划比划就出来了....   代码如下: ================== ...

  3. Intersecting Lines POJ 1269

    题目大意:给出两条直线,每个直线上的两点,求这两条直线的位置关系:共线,平行,或相交,相交输出交点. 题目思路:主要在于求交点 F0(X)=a0x+b0y+c0==0; F1(X)=a1x+b1y+c ...

  4. POJ P2318 TOYS与POJ P1269 Intersecting Lines——计算几何入门题两道

    rt,计算几何入门: TOYS Calculate the number of toys that land in each bin of a partitioned toy box. Mom and ...

  5. POJ1269:Intersecting Lines(判断两条直线的关系)

    题目:POJ1269 题意:给你两条直线的坐标,判断两条直线是否共线.平行.相交,若相交,求出交点. 思路:直线相交判断.如果相交求交点. 首先先判断是否共线,之后判断是否平行,如果都不是就直接求交点 ...

  6. POJ 1269 Intersecting Lines --计算几何

    题意: 二维平面,给两条线段,判断形成的直线是否重合,或是相交于一点,或是不相交. 解法: 简单几何. 重合: 叉积为0,且一条线段的一个端点到另一条直线的距离为0 不相交: 不满足重合的情况下叉积为 ...

  7. POJ 1269 Intersecting Lines【判断直线相交】

    题意:给两条直线,判断相交,重合或者平行 思路:判断重合可以用叉积,平行用斜率,其他情况即为相交. 求交点: 这里也用到叉积的原理.假设交点为p0(x0,y0).则有: (p1-p0)X(p2-p0) ...

  8. 简单几何(直线位置) POJ 1269 Intersecting Lines

    题目传送门 题意:判断两条直线的位置关系,共线或平行或相交 分析:先判断平行还是共线,最后就是相交.平行用叉积判断向量,共线的话也用叉积判断点,相交求交点 /********************* ...

  9. POJ 1269 Intersecting Lines(计算几何)

    题意:给定4个点的坐标,前2个点是一条线,后2个点是另一条线,求这两条线的关系,如果相交,就输出交点. 题解:先判断是否共线,我用的是叉积的性质,用了2遍就可以判断4个点是否共线了,在用斜率判断是否平 ...

随机推荐

  1. 协同过滤CF算法之入门

    数据规整 首先将评分数据从 ratings.dat 中读出到一个 DataFrame 里: >>> import pandas as pd In [2]: import pandas ...

  2. PHP使用FPDF pdf添加水印中文乱码问题 pdf合并版本问题

    ---恢复内容开始--- require_once('../fpdf/fpdf.php');require_once('../fpdi/fpdi.php'); 使用此插件 pdf 合并 并添加水印 期 ...

  3. POI实现EXCEL单元格合并及边框样式

    POI实现EXCEL单元格合并及边框样式     下面例子为创建产生一个excel,合并单元格,然后为合并后的单元格添加边框 package test; import java.io.FileOutp ...

  4. springTask任务调度

    1什么是任务调度 在企业级应用中,经常会制定一些“计划任务”,即在某个时间点做某件事情,核心是以时间为关注点,即在一个特定的时间点,系统执行指定的一个操作.常见的任务调度框架有Quartz和Sprin ...

  5. 《GPU高性能编程CUDA实战》附录三 关于book.h

    ▶ 本书中用到的公用函数放到了头文件book.h中 #ifndef __BOOK_H__ #define __BOOK_H__ #include <stdio.h> #include &l ...

  6. 5. jdk路径配置

    path , classpath 的配置及作用? 1) PATH环境变量.作用是指定命令搜索路径,在i命令行下面执行命令如javac编译java程序时,它会到PATH变量所指定的路径中查找看是否能找到 ...

  7. leetcode54

    class Solution { public: vector<int> spiralOrder(vector<vector<int>>& matrix) ...

  8. DevExpress ImageComboBoxEdit增加

    Combo_订单类型.Properties.Items.Clear() Select Case Combo_客户名称.EditValue Case "ABC" Combo_订单类型 ...

  9. UI5-文档-2.1-使用OpenUI5开发应用

    使用OpenUI5和您选择的开发环境(编辑器和Web服务器)开发应用程序.您可以下载所有的源代码,也可以参考OpenUI5的在线版本. 下载OpenUI5 下载和安装OpenUI5的默认方式是从htt ...

  10. javascript时间日期操作

    Js获取当前日期时间及其它操作 var myDate = new Date();myDate.getYear();        //获取当前年份(2位)myDate.getFullYear();   ...