POJ_1269_Intersecting Lines_求直线交点
POJ_1269_Intersecting Lines_求直线交点
Description
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
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
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
我们都知道一双独特的点在平面上定义了一条线,一条线在一个平面相交的三种方法:
1)没有交集,因为它们是平行的,
2)相交于一条线,因为他们是在另一个(即他们是相同的线),
3)相交于一点。在这个问题中,你将使用你的代数知识来创建一个程序来决定两条线的交点。
你的程序将会反复地读入四个点,在xy平面上定义两条直线,并确定直线的交点和位置。这个问题所要求的所有数字都是合理的,比如在-1000和1000之间。
先用两条直线的向量的叉积判断是否平行,然后用一个端点向另外一条直线的两个端点连线求叉积判断是否重合。
都不是就直接求两直线的交点,用平行四边形的面积求。 代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
typedef double f2;
#define eps 1e-6
f2 fabs(f2 x) {return x>0?x:-x;}
struct Point {
f2 x,y;
Point() {}
Point(f2 x_,f2 y_):
x(x_),y(y_) {}
Point operator + (const Point &p) const {return Point(x+p.x,y+p.y);}
Point operator - (const Point &p) const {return Point(x-p.x,y-p.y);}
Point operator * (f2 rate) const {return Point(x*rate,y*rate);}
void rd() {scanf("%lf%lf",&x,&y);}
};
typedef Point Vector;
f2 dot(const Point &p1,const Point &p2) {return p1.x*p2.x+p1.y*p2.y;}
f2 cross(const Point &p1,const Point &p2) {return p1.x*p2.y-p1.y*p2.x;}
struct Line {
Point p;
Vector v;
Line() {}
Line(const Point &p_,const Vector &v_):
p(p_),v(v_) {}
};
Point get_point(const Line &l1,const Line &l2) {
Vector u=l1.p-l2.p;
f2 t=cross(l2.v,u)/cross(l1.v,l2.v);
return l1.p+l1.v*t;
}
void solve() {
Point a1,a2,b1,b2;
a1.rd();a2.rd();b1.rd();b2.rd();
Vector A=a1-a2,B=b1-b2;
if(fabs(cross(A,B))<eps) {
if(fabs(cross(a1-b1,a2-b1))<eps&&fabs(cross(a1-b2,a2-b2))<eps) puts("LINE");
else puts("NONE");
}else {
Point ans=get_point(Line(a2,A),Line(b2,B));
printf("POINT %.2lf %.2lf\n",ans.x,ans.y);
}
}
int main() {
puts("INTERSECTING LINES OUTPUT");
int n;
scanf("%d",&n);
while(n--) {
solve();
}
puts("END OF OUTPUT");
}
POJ_1269_Intersecting Lines_求直线交点的更多相关文章
- UVa 11437:Triangle Fun(计算几何综合应用,求直线交点,向量运算,求三角形面积)
Problem ATriangle Fun Input: Standard Input Output: Standard Output In the picture below you can see ...
- Uva 11178 Morley's Theorem 向量旋转+求直线交点
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=9 题意: Morlery定理是这样的:作三角形ABC每个 ...
- poj 1269 Intersecting Lines——叉积求直线交点坐标
题目:http://poj.org/problem?id=1269 相关知识: 叉积求面积:https://www.cnblogs.com/xiexinxinlove/p/3708147.html什么 ...
- [java作业]Fan、求直线交点、Triangle2D、选课
public class Fan { public static void main(String[] args) { Fan fan1 = new Fan(), fan2 = new Fan(); ...
- poj1269 (叉积求直线的交点)
题目链接:https://vjudge.net/problem/POJ-1269 题意:给出4个顶点,表示两条直线,求这两条直线的相交情况,重合输出LINE,平行输出NONE,相交于一点输出该点的距离 ...
- 谈谈"求线段交点"的几种算法(js实现,完整版)
"求线段交点"是一种非常基础的几何计算, 在很多游戏中都会被使用到. 下面我就现学现卖的把最近才学会的一些"求线段交点"的算法总结一下, 希望对大家有所帮助. ...
- 计算几何——直线交点poj1269
求直线交点还是要推一个公式的.. 见博客https://blog.csdn.net/u013050857/article/details/40923789 还要学一下向量的定点比分法 另外poj精度好 ...
- MATLAB—求直线或者线段之间的交点坐标
function CrossPoint( ) %% 求两条直线的交点坐标 x1 = [7.8 8]; y1 = [0.96 0.94]; %line2 x2 = [8.25 8.25]; y2 = [ ...
- hdu 2528:Area(计算几何,求线段与直线交点 + 求多边形面积)
Area Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
随机推荐
- valid palindrome(回文)
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- oracle索引建立和删除
1.多列建立索引 SQL> create index dex_index2 on dex(sex,name); Index created. SQL> select object_name ...
- Day5_协程函数_面向过程
def func(count): while True: yield count count +=1 #这是一个生成器,需要利用next()来执行. func(10) #yield: #1.把函数的执 ...
- python argparse用法总结
转:python argparse用法总结 1. argparse介绍 argparse是python的一个命令行解析包,非常适合用来编写可读性非常好的程序. 2. 基本用法 prog.py是我在li ...
- 163邮箱 SMTP发送邮件注意点
在之前163邮箱注册的时候默认开通SMTP服务的,之后需要自己手动开始. 在配置的时候服务器的地址固定 用户名称就是你的邮箱 密码需要注意的是有的是你邮箱的密码,如果不对需要填写你的授权码!
- iview源码解析(1)
概述 公司技术栈开始用vue主导开发,但因为公司前端会vue的不多所以在项目中用到vue的技术不是很深,之前出去面试被接连打击,而且本来打算开始为公司vue的项目构建自己的组件库所以去下载了iview ...
- flume安装及入门实例
1. 如何安装? 1)将下载的flume包,解压到/home/hadoop目录中 2)修改 flume-env.sh 配置文件,主要是JAVA_HOME变量设置 root@m1:/home/hadoo ...
- 前端开发APP,从HBuilder开始~ 【转】
内容简介 介绍目前前端人员开发app的几种方法,具体介绍hbuilder开发app,一扇赞新的大门~ 无所不能的js 最开始js仅仅局限于网页上一些效果,操作网页内容等, 但是nodejs把js带入了 ...
- 并发编程(十):AQS
AQS全称为AbstractQueuedSynchronizer,是并发容器中的同步器,AQS是J.U.C的核心,它是抽象的队列式的同步器,AQS定义了一套多线程访问共享资源的同步器框架,许多同步类都 ...
- 适合Python 新手的5大练手项目,你练了么?
接下来就给大家介绍几种适合新手的练手项目. 0.算法系列-排序与查找 Python写swap很方便,就一句话(a, b = b, a),于是写基于比较的排序能短小精悍.刚上手一门新语言练算法最合适不过 ...