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 ...
随机推荐
- 嵌入式开发值zynq驱动—— zynq SPI Flash 驱动过程
http://blog.csdn.net/pengwangguo/article/details/52292664 http://blog.csdn.net/pengwangguo/article/d ...
- asp.net 截屏
public class HomeController : Controller { // // GET: /Home/ static System.Windows.Forms.WebBrowser ...
- .net MVC 单页面 多个(行)数据修改
一 /// <summary> /// 参数信息分页请求,前台要设置Form,这样可以当前页多值修改 /// </summary> /// <returns>< ...
- Linux环境下Redis安装配置步骤[转]
在LInux下安装Redis的步骤如下: 1.首先下载一个Redis安装包,官网下载地址为:https://redis.io/ 2.在Linux下解压redis: tar -zxvf redis-2. ...
- JSP——MVC模式+Servlet生命周期
设计模式MVC模式 模型MODEL操作数据库的增删改查——javaBean 视图VIEW显示数据——JSP 控制器CONTROLLER响应用户的——servlet Model(模型),是程序的主体部分 ...
- php public,static,private,protected,final,const,abstract
public:权限是最大的,可以内部调用,实例调用等. protected: 受保护类型,用于本类和继承类调用. private: 私有类型,只有在本类中使用. final:PHP 5:不被改,不被继 ...
- Thinkphp5笔记四:设置模板路径
默认的模板路径在模块/view文件里面.如果你觉得这样不太方便管理,想要把他设置Template目录下,可以这样做. 模板参数 ,能够影响的它参数,是当前模块下config.php template- ...
- 复制js的“准”正确用法
function kobe(id) { var targetText = document.getElementById(id); try ...
- oracle自定义类型 示例
) ); ---自定义类型传参给存储过程,示例如下: create or replace procedure dropWf is cursor c_tenant is and t.id not in ...
- pom.xml 配置maven私服
1.pom.xml 配置maven私服 <repositories> <repository> <id>caf_repositories& ...