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. PHP内置函数生成随机数的方法汇总

    PHP内部生成随机数的方法相比其他方法简单,不需要额外配置,是生成随机数的首选方案. 1 rand函数 rand() 函数可以不加任何参数,就可以生成随机整数.如果要设置随机数范围,可以在函数中设置 ...

  2. SOAP消息头的处理

    SOAP消息头的处理 WebService学习总结(四)——调用第三方提供的webService服务 SOAP中 RPC/ENC 为啥被抛弃

  3. 环境变量PATH/cp命令/mv命令/文档查看cat/more/less/head/tail

    2.10 环境变量PATH 2.11 cp命令 2.12 mv命令 2.13 文档查看cat/more/less/head/tail which  rmdir 可以查到命令的路径 例如: ls 命令是 ...

  4. datepicker clone 控件错误

    删除id,并删除hasDatepicker //+ -  function changeRows(sender,desc){ var tr = $(sender).closest("tr&q ...

  5. nvm安装node和npm,个人踩坑记录

    我采用nvm-setup安装windows版本的nvm nvm安装node出现的问题: 1.node成功了,npm没成功 解决:在nvm 安装了node之后,输入npm找不到该命令,当时安装报错如下: ...

  6. 如何在eclipse中使用mvn clean install

    1.在Maven项目或者pom.xml上右键——>Run As ——>“Maven Build...”或者Run Configuration——>“Maven Build” 2.在“ ...

  7. Java加密和C#解密=>DES方法

    Java加密代码: import javax.crypto.*; import javax.crypto.*; import java.io.UnsupportedEncodingException; ...

  8. 变分推断(Variational Inference)

    (学习这部分内容大约需要花费1.1小时) 摘要 在我们感兴趣的大多数概率模型中, 计算后验边际或准确计算归一化常数都是很困难的. 变分推断(variational inference)是一个近似计算这 ...

  9. C# 多线程 Parallel.ForEach 和 ForEach 效率问题研究及理解

    from:https://blog.csdn.net/li315171406/article/details/78450534 最近要做一个大数据dataTable循环操作,开始发现 运用foreac ...

  10. Java Jdk1.8 HashMap源代码阅读笔记二

    三.源代码阅读 3.元素包括containsKey(Object key) /** * Returns <tt>true</tt> if this map contains a ...