poj 1269 Intersecting Lines
题目链接:http://poj.org/problem?id=1269
题目大意:给出四个点的坐标x1,y1,x2,y2,x3,y3,x4,y4,前两个形成一条直线,后两个坐标形成一条直线。然后问你是否平行,重叠或者相交,如果相交,求出交点坐标。
算法:二维几何直线相交+叉积
解法:先用叉积判断是否相交,如果相交的话,设交点坐标为p0(x0,y0)。向量(p0p1)和(p0p2)的叉积为0,有(x1-x0)*(y2-y0)-(y1-y0)*(x2-x0)=0;同理,求出p0和p3p4直线的式子。然后联立求解x0,y0。平行或重叠的情况就自己YY了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define inf 0x7fffffff
#define exp 1e-10
#define PI 3.141592654
using namespace std;
struct Point
{
double x,y;
Point(double x=,double y=):x(x),y() {}
};
typedef Point Vector;
double cross(Vector A,Vector B)
{
return A.x*B.y-A.y*B.x;
}
Point GetLineIntersection(Point P,Vector v,Point Q,Vector w)
{
Point uu;
Vector u=Point(P.x-Q.x , P.y-Q.y);
double t=cross(w,u)/cross(v,w);
uu.x=P.x+v.x*t;
uu.y=P.y+v.y*t;
return uu;
}//调用训练指南上这个函数怎么错了,我写错了吗
int main()
{
int n;
double x1,y1,x2,y2,x3,y3,x4,y4;
//cin>>n;
while (cin>>n)
{
printf("INTERSECTING LINES OUTPUT\n");
while (n--) {
cin>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4;
Point P,Q;
P.x=x1 ;P.y=y1 ;
Q.x=x3 ;Q.y=y3 ;
Vector v,w;
v.x=x2-x1 ;v.y=y2-y1;
w.x=x4-x3 ;w.y=y4-y3;
if (cross(v,w)!=)
{
//Vector vv=GetLineIntersection(P,v,Q,w);
double a1,b1,c1;
double a2,b2,c2;
a1=y1-y2 ;b1=x2-x1 ;c1=x1*y2-y1*x2;
a2=y3-y4 ;b2=x4-x3 ;c2=x3*y4-y3*x4;
double x0=(b1*c2-b2*c1)/(b2*a1-b1*a2);
double y0=(a2*c1-a1*c2)/(a1*b2-a2*b1);
printf("POINT %.2f %.2f\n",x0,y0);
}
else
{
if (fabs(v.x)<=exp && fabs(w.x)<=exp)
{
if (fabs(x1-x3)<=exp)
printf("LINE\n");
else printf("NONE\n");
}
else if (fabs(v.y)<=exp && fabs(w.y)<=exp)
{
if (fabs(y1-y3)<=exp)
printf("LINE\n");
else printf("NONE\n");
}
else
{
if (fabs((y3-w.y/w.x*x3)-(y1-w.y/w.x*x1))<=exp)
printf("LINE\n");
else printf("NONE\n");
}
}
}
printf("END OF OUTPUT\n");
}
return ;
}
poj 1269 Intersecting Lines的更多相关文章
- POJ 1269 Intersecting Lines(判断两直线位置关系)
题目传送门:POJ 1269 Intersecting Lines Description We all know that a pair of distinct points on a plane ...
- 判断两条直线的位置关系 POJ 1269 Intersecting Lines
两条直线可能有三种关系:1.共线 2.平行(不包括共线) 3.相交. 那给定两条直线怎么判断他们的位置关系呢.还是用到向量的叉积 例题:POJ 1269 题意:这道题是给定四个点p1, ...
- ●POJ 1269 Intersecting Lines
题链: http://poj.org/problem?id=1269 题解: 计算几何,直线交点 模板题,试了一下直线的向量参数方程求交点的方法. (方法详见<算法竞赛入门经典——训练指南> ...
- POJ 1269 - Intersecting Lines - [平面几何模板题]
题目链接:http://poj.org/problem?id=1269 Time Limit: 1000MS Memory Limit: 10000K Description We all know ...
- poj 1269 Intersecting Lines——叉积求直线交点坐标
题目:http://poj.org/problem?id=1269 相关知识: 叉积求面积:https://www.cnblogs.com/xiexinxinlove/p/3708147.html什么 ...
- POJ 1269 Intersecting Lines (判断直线位置关系)
题目链接:POJ 1269 Problem Description We all know that a pair of distinct points on a plane defines a li ...
- POJ 1269 Intersecting Lines(线段相交,水题)
id=1269" rel="nofollow">Intersecting Lines 大意:给你两条直线的坐标,推断两条直线是否共线.平行.相交.若相交.求出交点. ...
- POJ 1269 Intersecting Lines --计算几何
题意: 二维平面,给两条线段,判断形成的直线是否重合,或是相交于一点,或是不相交. 解法: 简单几何. 重合: 叉积为0,且一条线段的一个端点到另一条直线的距离为0 不相交: 不满足重合的情况下叉积为 ...
- POJ 1269 Intersecting Lines【判断直线相交】
题意:给两条直线,判断相交,重合或者平行 思路:判断重合可以用叉积,平行用斜率,其他情况即为相交. 求交点: 这里也用到叉积的原理.假设交点为p0(x0,y0).则有: (p1-p0)X(p2-p0) ...
随机推荐
- 封装document.ready方法
function $(fn){ if(document.addEventListener){ //W3C document.addEventListener('DOMContentLoaded',fu ...
- nginx+php与apache+php性能对比
测试工具http_load相同的动态页面测试,相同的硬件资源,相同并发,相同请求数量的前提下,nginx+php比apache+php的性能要 差,而且如果请求的压力大于硬件资源的承受能力,nginx ...
- 从PC跳转至wap
<script language="JavaScript">function mobile_device_detect(url){var thisOS=navigato ...
- 一月份实现Adb小程序
As Brian said: According to a post on xda-developers, you can enable ADB over WiFi from the device w ...
- 支付宝收款连接 非API
<a href="https://shenghuo.alipay.com/send/payment/fill.htm?_form_token=mMYOrAXfReOtBBCMmoaK7 ...
- MYSQL数据库表中字段追加字符串内容
$sql="update parts set p_notes=concat(p_notes,'{$p_notes}') where p_id={$p_id}"; parts为表名 ...
- 奇怪的函数 (codevs 3538/1696) 题解
[题目描述] 给定n,使得x^x达到或超过n位数字的最小正整数x是多少? [样例输入] 11 [样例输出] 10 [解题思路] 首先想到枚举,但是范围有点大,n<=2*10^9,果断用二分.其实 ...
- MVC MVVM Knockout 常遇问题总结
1.模板绑定(使用插件jquery.tmpl) var ViewModel={Product:ko.observable()} <div data-bind="template:{na ...
- ARM时钟初始化
2440: S3C2440可以使用外部晶振(XTIpll)(默认为12MHZ)和外部时钟(EXTCLK)两种方式输入时钟信号.它由跳线OM[3:2]决定.S3C2440 默认的工作主频为12MHz(晶 ...
- linux下的声卡驱动架构
1.linux下的声卡驱动架构主要分为OSS架构和ALSA架构. 2.OSS架构 OSS全称是Open Sound System,叫做开放式音频系统,这种早期的音频系统这种基于文件系统的访问方式,这意 ...