POJ_1269_Intersecting_Lines_(计算几何基础)

描述



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

给出两条直线,判断它们是平行,重合,还是相交,如果相交,求出交点.

分析



比较裸的一道题.学习了直线的写法(参数方程)

 1 #include <cstdio>
 2 #include <cmath>
 3 using namespace std;
 4
 5 const double eps=1e-8;
 6
 7 struct pt{ double x,y; pt(double x=0,double y=0):x(x),y(y){} };
 8 typedef pt vt;
 9 int dcmp(double x){ if(fabs(x)<eps) return 0; return x>0?1:-1; }
10 vt operator + (vt a,vt b){ return vt(a.x+b.x,a.y+b.y); }
11 vt operator - (vt a,vt b){ return vt(a.x-b.x,a.y-b.y); }
12 vt operator * (vt a,double p){ return vt(a.x*p,a.y*p); }
13 double cross(vt a,vt b){ return a.x*b.y-a.y*b.x; }
14 struct line{
15     pt p; vt v;
16     line(){}
17     line(pt a,pt b){ p=a; v=b-a; }
18 };
19 int line_intersection(line A,line B){
20     if(dcmp(cross(A.v,B.v)!=0)) return -1;
21     return dcmp(cross(A.v,A.p-B.p))==0;
22 }
23 pt get_line_intersection(line A,line B){
24     vt v=A.v,w=B.v,u=A.p-B.p;
25     double t=cross(w,u)/cross(v,w);
26     return A.p+v*t;
27 }
28 int main(){
29     int n;
30     scanf("%d",&n);
31     puts("INTERSECTING LINES OUTPUT");
32     while(n--){
33         pt p[4]; line l[2];
34         for(int i=0;i<4;i++) scanf("%lf%lf",&p[i].x,&p[i].y);
35         l[0]=line(p[0],p[1]);
36         l[1]=line(p[2],p[3]);
37         int t=line_intersection(l[0],l[1]);
38         if(t==-1){
39             pt x=get_line_intersection(l[0],l[1]);
40             printf("POINT %.2lf %.2lf\n",x.x,x.y);
41         }
42         else if(t==1) puts("LINE");
43         else puts("NONE");
44     }
45     puts("END OF OUTPUT");
46     return 0;
47 }

Intersecting Lines

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 13622   Accepted: 6060

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

Source

Mid-Atlantic 1996

时间: 2024-10-24 11:05:17

POJ_1269_Intersecting_Lines_(计算几何基础)的相关文章

nyis oj 68 三点顺序 (计算几何基础)

三点顺序 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描写叙述 如今给你不共线的三个点A,B,C的坐标,它们一定能组成一个三角形,如今让你推断A,B,C是顺时针给出的还是逆时针给出的? 如: 图1:顺时针给出 图2:逆时针给出 <图1>                   <图2> 输入 每行是一组測试数据,有6个整数x1,y1,x2,y2,x3,y3分别表示A,B,C三个点的横纵坐标.(坐标值都在0到10000之间) 输入0 0 0 0 0 0表示输入

计算几何基础——【点积和叉积的用处】

计算几何是算法竞赛的一大块,而叉积是计算机和的基础. 首先叉积是计算说向量之间的叉积,那么我们可以这样定义向量,以及向量的运算符重载. struct Point { double x,y; Point(double x=0,double y=0):x(x),y(y) {} }; typedef Point Vector; Vector operator + (Vector A,Vector B) { return Vector(A.x+B.x,A.y+B.y); } Vector operato

知识点 - 计算几何基础

知识点 - 计算几何基础 讲义 点 我们把点 \(\mathbf r\) 看成从 \(\mathbf 0\) 到 \(\mathbf r\)的向量 \(\vec{\mathbf r}\) #define ftype long double struct point2d { ftype x, y; point2d() {} point2d(ftype x, ftype y): x(x), y(y) {} point2d& operator+=(const point2d &t) { x +=

【kuangbin专题】计算几何基础(极角相关)

[POJ 1696] Space Ants [题目大意] 给定多个点,对他们按照下面的规则排序,每个都在前一个点组成的左边,并且连线不相交(典型如图) [题目分析] 不断进行极角排序,不断选取一定区域内最符合要求的解 [代码] 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #define ll double 5 using namespace std; 6 const double eps

BZOJ_1610_[Usaco2008_Feb]_Line连线游戏_(计算几何基础+暴力)

描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1610 给出n个点,问两两确定的直线中,斜率不同的共有多少条. 分析 暴力枚举直线,算出来斜率放在k数组里面(斜率不存在记为INF),然后去个重统计个数就行了. 其实特水... 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int maxn=200+5; 5 const double eps=1e-8,INF=0x7f

计算几何及其应用——计算几何基础

写在前面:当时开计算几何这个专题神奇的从解析几何开始了,然后最近发现<计算几何及应用(金博)>这本书前面那章忽略掉了一些重要的东西比如说点定位.半平面相交之类的东西,恰好还有一些和计算几何扯上边但是不需要算法的简单题目没有整理,故在此开辟一块小空间. 我们再来看一道有关几何的问题.(Problem source:hdu2073)    数理分析:虽然这道题异常的简单,基本算不上计算几何这个专题当中的题目,但是把它拿到这里来,是源于这道简单几何题的思路其实体现了计算几何整个体系中比较重要的思维.

【习题整理】计算几何基础

bzoj1074[Scoi2007]折纸 思路:考虑倒着做,每次将在折叠的直线右边的扔掉,左边的点再对称一次加入: 算几知识:求向量关于法向量的对称向量 点$A$关于点$B$对称的点$C = 2B - A$ 如果要求$\vec{A}$关于法向量$\vec{l}$的对称向量$\vec{A'}$: 可以考虑都平移到原点 利用点积求出$\vec{A}$在$\vec{l}$上的投影点$D$, 再将点$A$关于$D$对称到$A'$: $A'$的坐标就是向量$\vec{A'}$ 1 #include<bit

计算几何基础

记录点滴. 1 /* 2 2015.6 HT 3 ACM Work_8 4 5 */ 6 7 #include <iostream> 8 #include <cstdio> 9 using namespace std; 10 11 /* 12 线段判交--ACM 13 14 1.快速排斥实验 15 设以线段P1P2和线段Q1Q2为对角线的矩形为M,N;若M,N 不相交,则两个线段显然不相交: 16 2.跨立实验 17 如果两线段相交,则两线段必然相互跨立对方.若P1P2跨立Q1Q2

[学习笔记]计算几何基础

三角函数 向量 线性空间中有大小和方向的量. 坐标表示:$P(x_1,y_1),Q(x_2,y_2)$. $\overrightarrow{PQ}=(x_2-x_1,y_2-y_1)$. $|PQ|=\sqrt{(x_2-x_1)^2+(y_2-y_1)^2}$. 向量运算 $a=(x_1,y_1),b=(x_2,y_2)$. 加法 $a+b=(x_1+x_2,y_1+y_2)$. 减法 $a-b=(x_1-x_2,y_1-y_2)$. 点乘 $a\;\cdot\;b=x_1x_2+y_1y_2