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(计算几何)的更多相关文章

  1. HDU1086 You can Solve a Geometry Problem too(计算几何)

    You can Solve a Geometry Problem too                                         Time Limit: 2000/1000 M ...

  2. 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 ...

  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 ...

  4. (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/ ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. (叉积,线段判交)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 ...

  9. 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 ...

随机推荐

  1. Java并发笔记-未完待续待详解

    为什么需要并行? – 业务要求 – 性能 并行计算还出于业务模型的需要 – 并不是为了提高系统性能,而是确实在业务上需要多个执行单元. – 比如HTTP服务器,为每一个Socket连接新建一个处理线程 ...

  2. ubuntu 安装 SVN 后的错误:Subversion Native Library Not Available & Incompatible JavaHL library loaded

    问题一 安装了SVN的eclipse插件,使用的时候就会弹出一个错误的提示框:  Subversion Native Library Not Available,加载不到JavaHL.   解决方法 ...

  3. Office在线预览及PDF在线预览的实现方式

    原文链接:http://www.officeweb365.com/officetoview.html 一.服务器先转换为PDF,再转换为SWF,最后通过网页加载Flash预览 微软方:利用Office ...

  4. Linux--nginx域名绑定-url rewrite

    进入/usr/local/nginx/conf 编辑 nginx.conf 绑定域名: 添加一个 server元素,更改后的配置内容可能如下: server { listen       80; se ...

  5. VC设置cookies实现文件刷下载量

    VC设置cookies实现文件刷下载量 VC设置cookies实现文件刷下载量 分类: c/c++2013-10-11 17:22 250人阅读 评论(0) 收藏 举报   目录(?)[+]   同学 ...

  6. Thinkphp5笔记七:设置错误页面②

    更加完美的去设置错误页面. 一.准备一个错误页面 error.html,位置:thinkphp\template\index\default\error.html ,准备把前段所有的错误提示都指向这里 ...

  7. 【调研】在总体为n的情况下,多少样本有代表性?

    见这里: http://www.raosoft.com/samplesize.html

  8. MyEclipse使用笔记

    简单记录下个人常用的一些MyEclipse设置 VS颜色方案 Window-->Preference-->Java->Editor-->Syntax Coloring Clas ...

  9. 内省Introspector(反射操作javaBean)

    一:内省是一种特殊的反射,来更方便的操作javaBean对象,通过内省可以获取到类字节码的描述器, 然后解剖每一个字段,获取每个字段的读写方法,即get/set方法的反射,然后获取或者是封装bean的 ...

  10. [原]NGUI之按钮置灰

    传统按钮置灰,需要使用另外一张纹理. 本例通过修改shader和NGUI sprite的r值实现按钮置灰.优势:节省纹理,操作简单 将NGUI Unlit/Transparent Colored片段部 ...