hdu_1086 You can Solve a Geometry Problem too(计算几何)
http://acm.hdu.edu.cn/showproblem.php?pid=1086
分析:简单计算几何题,相交判断直接用模板即可。
思路:将第k条直线与前面k-1条直线进行相交判断,因为题目中不排除多条直线相交于同一个点的重复情况。
代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>
using namespace std;
const double eps = 1e-8;
const double PI = acos(-1.0); //比直接写3.1415926精确 int sgn(double x) //三态函数,精确度提高
{
if(fabs(x) < eps)return 0;
else return x<0? -1:1;
} struct Point
{
double x,y;
Point(){}
Point(double _x,double _y) //带参构造函数
{
x = _x;y = _y;
}
Point operator -(const Point &b)const //点相减
{
return Point(x - b.x,y - b.y);
}
double operator ^(const Point &b)const //叉积(外积)
{
return x*b.y - y*b.x;
}
double operator *(const Point &b)const //点积
{
return x*b.x + y*b.y;
}
void transXY(double B) //绕原点旋转角度B(弧度值),后x,y的变化
{
double tx = x,ty = y; //
x = tx*cos(B) - ty*sin(B);
y = tx*sin(B) + ty*cos(B);
}
};
struct Line
{
Point s,e;
Line(){}
Line(Point _s,Point _e)
{
s = _s;e = _e;
}
//两直线相交求交点
//第一个值为0表示直线重合,为1表示平行,为0表示相交,为2是相交
//只有第一个值为2时,交点才有意义
pair<int,Point> operator &(const Line &b)const
{
Point res = s;
if(sgn((s-e)^(b.s-b.e)) == 0)
{
if(sgn((s-b.e)^(b.s-b.e)) == 0)
return make_pair(0,res);//重合
else return make_pair(1,res);//平行
}
double t = ((s-b.s)^(b.s-b.e))/((s-e)^(b.s-b.e));
res.x += (e.x-s.x)*t;
res.y += (e.y-s.y)*t;
return make_pair(2,res);
}
};
Point ms,me;
Line ml[120]; //*两点间距离
double dist(Point a,Point b)
{
return sqrt((a-b)*(a-b));
}
//*判断线段相交
bool inter(Line l1,Line l2)
{
return
max(l1.s.x,l1.e.x) >= min(l2.s.x,l2.e.x) &&
max(l2.s.x,l2.e.x) >= min(l1.s.x,l1.e.x) &&
max(l1.s.y,l1.e.y) >= min(l2.s.y,l2.e.y) &&
max(l2.s.y,l2.e.y) >= min(l1.s.y,l1.e.y) &&
sgn((l2.s-l1.e)^(l1.s-l1.e))*sgn((l2.e-l1.e)^(l1.s-l1.e)) <= 0 &&
sgn((l1.s-l2.e)^(l2.s-l2.e))*sgn((l1.e-l2.e)^(l2.s-l2.e)) <= 0;
} int main()
{
int n;
int ans;
while(scanf("%d",&n),n){
ans=0;
for(int i=0;i<n;i++){
scanf("%lf%lf%lf%lf",&ms.x,&ms.y,&me.x,&me.y);
ml[i].s=ms; ml[i].e=me;
}
for(int i=1;i<n;i++)
for(int j=0;j<i;j++){
if(inter(ml[i],ml[j])) ans++;
}
printf("%d\n",ans);
}
return 0;
}
hdu_1086 You can Solve a Geometry Problem too(计算几何)的更多相关文章
- HDU1086 You can Solve a Geometry Problem too(计算几何)
You can Solve a Geometry Problem too Time Limit: 2000/1000 M ...
- 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 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
pid=1086">You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Mem ...
- 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 ...
- (叉积,线段判交)HDU1086 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 (hdu1086)几何,判断两线段相交
You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3276 ...
随机推荐
- e770. 确定按钮租中已选的单选按钮
When you ask a button group for the currently selected radio button, it returns the selected radio b ...
- 我的openwrt开发相关文章
openwrt学习笔记: 在openwrt的学习过程中,走了非常多的弯路.一直以来有个期盼.希望能够出个简易教程,希望openwrt的同仁们能够更加高速的入手. . openwrt学习笔记(三十二): ...
- LigerUI编辑表格组件单元格校验问题
这几天在使用LigerUI(版本为1.2.2)编辑表格组件的时候,遇到几个小问题,从官方demo和api中没有找到解决的办法 问题1.从数据库查询出来的主键单元格不可编辑问题 主键单元格已经保存之前编 ...
- phpmyadmin配置文件权限错误,不应任何用户都能修改
访问phpmyadmin提示“配置文件权限错误,不应任何用户都能修改” 原因:phpmyadmin目录权限过高,设置了777访问权限 解决: $ phpmyadin 转自: http://www.it ...
- Solaris10 修改hostname
修改/etc/nodename 及 /etc/hosts & /etc/hostname.<NIC Name> 这三个文件就可以了 重启
- linux nginx配置多个网站
1.建立wwwroot(/home/wwwrooot) 另建立一个wwwroot/test/index.html(网站目录) 2.建立vhost文件(/usr/local/nginx/conf/vho ...
- 使用 UICollectionView 实现网格化视图效果
讲解 UICollectionView 的相关链接:http://blog.csdn.net/eqera/article/details/8134986 关键操作: 效果如下: KMCollectio ...
- android手机内的通讯录数据库
今天看了一下 android手机内的通讯录数据库,简单的汇总了一下. 数据库见附件中的contacts2.db , 里面一共有40个表,34个视图,很庞大,挑几个重点的看一下. 1.表Raw_cont ...
- 数据库事务MTDC出错解决办法
Communication with the underlying transaction manager has failed 与基础事务管理器的通信失败 排除步驟: 1.檢查MS DTC 設置.[ ...
- 实现一个div,左边固定div宽度200px,右边div自适应
实现一个div,左边固定div宽度200px,右边div自适应<div class= "container"> <div class="left&quo ...