HDU 1086You can Solve a Geometry Problem too(判断两条选段是否有交点)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1086
判断两条线段是否有交点,我用的是跨立实验法:
两条线段分别是A1到B1,A2到B2,很显然,如果这两条线段有交点,那么可以肯定的是:
A1-B1,A2-B1这两个向量分别在B2-B1的两边,判断是不是在两边可以用向量的叉积来判断,这里就不说了,同理B1-A1,B2-A1在A2-A1的两边,当同时满足这两个条件时,说明这两条线段是有交点的。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn = ;
const double eps = 1e-;
struct point
{
double x,y;
point(double x = ,double y = ):x(x),y(y) {}
inline friend point operator + (point p1,point p2)
{
return point(p1.x+p2.x,p1.y+p2.y);
}
inline friend point operator - (point p1,point p2)
{
return point(p1.x-p2.x,p1.y-p2.y);
}
}A[maxn],B[maxn]; inline double dot(point p1,point p2)
{
return p1.x*p2.y - p2.x*p1.y;
}
inline double dis(point p1,point p2)
{
return sqrt((p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y));
}
int judge(point p1,point p2,point p3,point p4)
{
double temp = dot(p3-p1,p2-p1) * dot(p4-p1,p2-p1);
if(temp < || fabs(temp) < eps) return ;
return ;
} int main()
{
//freopen("in","r",stdin);
int n;
while(scanf("%d",&n),n)
{
for(int i = ;i < n;++i)
scanf("%lf%lf%lf%lf",&A[i].x,&A[i].y,&B[i].x,&B[i].y);
int ans = ;
for(int i = ;i < n;++i)
for(int j = i+;j < n;++j)
{
if(judge(A[i],B[i],A[j],B[j]) && judge(A[j],B[j],A[i],B[i])) ans++;
}
printf("%d\n",ans);
}
return ;
}
HDU 1086You can Solve a Geometry Problem too(判断两条选段是否有交点)的更多相关文章
- (hdu step 7.1.2)You can Solve a Geometry Problem too(乞讨n条线段,相交两者之间的段数)
称号: You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/ ...
- hdu 1086 You can Solve a Geometry Problem too 求n条直线交点的个数
You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3 ...
- HDU1086You can Solve a Geometry Problem too(判断线段相交)
You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3 ...
- hdu 1086 You can Solve a Geometry Problem too
You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3 ...
- HDU 1086:You can Solve a Geometry Problem too
pid=1086">You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Mem ...
- hdu 1086:You can Solve a Geometry Problem too(计算几何,判断两线段相交,水题)
You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3 ...
- hdu 1086 You can Solve a Geometry Problem too (几何)
You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3 ...
- You can Solve a Geometry Problem too(线段求交)
http://acm.hdu.edu.cn/showproblem.php?pid=1086 You can Solve a Geometry Problem too Time Limit: 2000 ...
- You can Solve a Geometry Problem too (hdu1086)几何,判断两线段相交
You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3276 ...
随机推荐
- javascript中对象字面量的理解
javascript中对象字面量与数组字面量 第一部分 我们知道JavaScript中的数据类型有基本数据类型和引用类型,其中Object类型就是非常常用的类型.那么如果创建一个Object类型的实例 ...
- mysql user administration
1. 为数据库databasename创建web用户 1.1 创建数据库 mysql> create database databasename; 1.2 为数据库创建用户 mysql> ...
- phpcms后台获取当前登录账号的数据
$amdinid=$_SESSION['userid'];$infoadmin=$this->admin->get_one(array('userid'=>$amdinid)); v ...
- 创建一个应用台程序,声明一个 int变量,并且初始化数组,通过while语句输出数组内的所有成员。
]{,,,,}; ; ) { Console.WriteLine("myNum[{0}]的值为{1}", s,myNum[s]); s++; } Console.ReadLine( ...
- 10月16日下午MySQL数据库CRUD操作(增加、删除、修改、查询)
1.MySQL注释语法--,# 2.2.后缀是.sql的文件是数据库查询文件. 3.保存查询. 关闭查询时会弹出提示是否保存,保存的是这段文字,不是表格(只要是执行成功了表格已经建立了).保存以后下次 ...
- JavaScript排序算法——冒泡排序
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- ecshop 调用收货地址
html {insert_scripts files='region.js,utils.js'} <script type="text/javascript"> reg ...
- PHP中的NULL类型
特殊的NULL值表示一个变量没有值,NULL类型唯一的值就是NULL.我们需要注意的是NULL不表示空格,也不表示零,也不是空字符串,而是一个变量的值为空.NULL不区分大小写,在下列情况下一个变量被 ...
- 浅谈T-SQL中的子查询
引言 这篇文章我们来简单的谈一下子查询的相关知识.子查询可以分为独立子查询和相关子查询.独立子查询不依赖于它所属的外部查询,而相关子查询则依赖于它所属的外部查询.子查询返回的值可以是标量(单值).多值 ...
- SQL having 子句
1.为什么存在? 在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与合计函数一起使用. 2.举例子: SELECT Customer,SUM(OrderPrice) FROM Or ...