题目链接:http://poj.org/problem?id=1269

题面:

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
思路:本题求的就是两条直线之间的位置关系,如果平行输出“NONE”,相交输出“POINT”和交点坐标,重合就输出“LINE”。判断两条直线是否平行则判断两条直线的单位方向向量是否相等或相反(即斜率是否相等),如果满足则是平行或重合,否则就是相交,相交就调用求交点的函数求出交点即可;而判断是否重合只需判断一条直线上的某一点是否在另一条直线上即可。
代码实现如下:
 #include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std; struct Point {
double x, y;
Point (double x = , double y = ) : x(x), y(y) {}
}; typedef Point Vector; int n;
Point A, B, C, D; Vector operator + (Vector A, Vector B) {
return Vector(A.x + B.x, A.y + B.y);
} Vector operator - (Vector A, Vector B) {
return Vector(A.x - B.x, A.y - B.y);
} Vector operator * (Vector A, double p) {
return Vector(A.x * p, A.y * p);
} bool operator < (const Point& a, const Point& b) {
return a.x < b.x || (a.x == b.x && a.y < b.y);
} const double eps = 1e-;
int dcmp(double x) {
if(fabs(x) < eps)
return ;
else
return x < ? - : ;
} bool operator == (const Point& a, const Point& b) {
return dcmp(a.x - b.x) == && dcmp(a.y - b.y) == ;
} double Dot(Vector A, Vector B) {
return A.x * B.x + A.y * B.y;
} double Length(Vector A) {
return sqrt(Dot(A, A));
} double Cross(Vector A, Vector B) {
return A.x * B.y - A.y * B.x;
} //求单位方向向量
Vector Unit_direction_vector(Vector w) {
return Vector(w.x / Length(w), w.y / Length(w));
} //判断两直线是否不相交
bool isIntersection(Vector A, Vector B) {
return Unit_direction_vector(A) == Unit_direction_vector(B) || Unit_direction_vector(Vector(- A.x, - A.y)) == Unit_direction_vector(B);
} Point GetLineIntersection(Point P, Vector v, Point Q, Vector w) {
Vector u = P - Q;
double t = Cross (w, u) / Cross(v, w);
return P + v * t;
} //判断两直线是否重合只要判断是否有公共点即可
bool OnLine(Point p, Point a1, Point a2) {
return dcmp(Cross(a1 - p, a2 - p)) == ;
} int main() {
while(~scanf("%d", &n)) {
printf("INTERSECTING LINES OUTPUT\n");
while(n--) {
scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &A.x, &A.y, &B.x, &B.y, &C.x, &C.y, &D.x, &D.y);
if(isIntersection(A - B, C - D)) {
if(OnLine(A, C, D)) {
printf("LINE\n");
} else {
printf("NONE\n");
}
} else {
Point P = GetLineIntersection(A, A - B, C, C - D);
printf("POINT %.2f %.2f\n", P.x, P.y);
}
}
printf("END OF OUTPUT\n");
}
}

Intersecting Lines (计算几何基础+判断两直线的位置关系)的更多相关文章

  1. TOYS(计算几何基础+点与直线的位置关系)

    题目链接:http://poj.org/problem?id=2318 题面: TOYS Time Limit: 2000MS   Memory Limit: 65536K Total Submiss ...

  2. Intersecting Lines---poj1269(求两直线的位置关系)

    题目链接:http://poj.org/problem?id=1269 题意:给你两条直线上的任意不同的两点,然后求两条直线的位置关系,如果相交于一点输出该点坐标; #include<iostr ...

  3. 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 ...

  4. POJ 1269 Intersecting Lines(判断两直线位置关系)

    题目传送门:POJ 1269 Intersecting Lines Description We all know that a pair of distinct points on a plane ...

  5. poj 1269 判断直线的位置关系

    题目链接 题意 判断两条直线的位置关系,重合/平行/相交(求交点). 直线以其上两点的形式给出(点坐标为整点). 思路 写出直线的一般式方程(用\(gcd\)化为最简), 计算\(\begin{vma ...

  6. 判断两条直线的位置关系 POJ 1269 Intersecting Lines

    两条直线可能有三种关系:1.共线     2.平行(不包括共线)    3.相交. 那给定两条直线怎么判断他们的位置关系呢.还是用到向量的叉积 例题:POJ 1269 题意:这道题是给定四个点p1, ...

  7. POJ 1269 /// 判断两条直线的位置关系

    题目大意: t个测试用例 每次给出一对直线的两点 判断直线的相对关系 平行输出NODE 重合输出LINE 相交输出POINT和交点坐标 1.直线平行 两向量叉积为0 2.求两直线ab与cd交点 设直线 ...

  8. 【POJ 1269】判断两直线相交

    题 利用叉积解方程 #include <cstdio> #define MAX 1<<31 #define dd double int xmult(dd x1,dd y1,dd ...

  9. [置顶] 如何判断两个IP大小关系及是否在同一个网段中

    功能点  判断某个IP地址是否合法 判断两个IP地址是否在同一个网段中 判断两个IP地址的大小关系 知识准备 IP协议 子网掩码 Java 正则表达式 基本原理 IP地址范围 0.0.0.0- 255 ...

随机推荐

  1. LintCode-8.旋转字符串

    旋转字符串 给定一个字符串和一个偏移量,根据偏移量旋转字符串(从左向右旋转) 样例 对于字符串 "abcdefg". offset=0 => "abcdefg&qu ...

  2. iOS 出现错误reason: image not found的解决方案

    在制作framework时遇到真机运行时导致的reason: image not found允许崩溃的问题,下面是我的解决方案: 首先我们分析一下出现这种情况的原因,原因就是framework找不到镜 ...

  3. 3ds Max学习日记(五)

      把实验室要用的小工具做了出来后,忙里偷闲,把第四章没看完的视频看完了.修改器(modifier)什么的还是挺好玩的.   FFD,车削,倒角,倒角剖面,对称,挤出,晶格,扭曲,融化,弯曲,网格平滑 ...

  4. getline读取整行文本// isprint

    getline——读取整行文本 这个函数接受两个参数:一个输入流对象和一个string对象.getline函数从输入流的下一行读取,并保存读取的内容到string中,但不包括换行符.和输入操作符不一样 ...

  5. BZOJ 2208 连通数(强连通分量)

    先缩点,对于缩完点后的DAG,可以直接在每个scc dfs一次就可以求出终点是这个scc的点的点对个数. # include <cstdio> # include <cstring& ...

  6. 【刷题】BZOJ 1003 [ZJOI2006]物流运输

    Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格 ...

  7. BZOJ1566:[NOI2009]管道取珠——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=1566 https://www.luogu.org/problemnew/show/P1758 题目 ...

  8. BZOJ3675 & 洛谷3648 & UOJ104:[Apio2014]序列分割——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=3675 https://www.luogu.org/problemnew/show/P3648 ht ...

  9. 信息工程学院技能大赛 计算机程序设计(Java)大赛试题

    前期准备与后期上传工作: (1)必须先建立项目和包,项目名为"JavaContest",包结构为:"contest.c+序号+姓名",其中序号为两位为本人大赛报 ...

  10. 从零开始学Linux系统(四)之Vi/Vim操作指令

    模式切换: 编辑模式 <-- [:]<--命令模式 -->[a.i.o A.I.O]-->  插入模式 编辑模式操作: 设置行号  :set nu   :set nonu 复制 ...