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/32768 K (Java/Others)
Total Submission(s): 6425 Accepted Submission(s): 3099
Give you N (1<=N<=100) segments(线段), please output the number of all intersections(交点). You should count repeatedly if M (M>2) segments intersect at the same point.
Note:
You can assume that two segments would not intersect at more than one point.
A test case starting with 0 terminates the input and this test case is not to be processed.
struct Point{
double x,y;
};
struct Line{
Point p1,p2;
};
double Max(double a,double b)
{
return a>b?a:b;
}
double Min(double a,double b)
{
return a<b?a:b;
}
double xmulti(Point p1,Point p2,Point p0)
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
}
bool inter(Line l1,Line l2)
{
if( Min(l2.p1.x,l2.p2.x)<=Max(l1.p1.x,l1.p2.x) &&
Min(l2.p1.y,l2.p2.y)<=Max(l1.p1.y,l1.p2.y) &&
Min(l1.p1.x,l1.p2.x)<=Max(l2.p1.x,l2.p2.x) &&
Min(l1.p1.y,l1.p2.y)<=Max(l2.p1.y,l2.p2.y) &&
xmulti(l1.p1,l2.p2,l2.p1)*xmulti(l1.p2,l2.p2,l2.p1)<= &&
xmulti(l2.p1,l1.p2,l1.p1)*xmulti(l2.p2,l1.p2,l1.p1)<= )
return true;
else
return false;
}
#include <iostream>
using namespace std;
struct Point{
double x,y;
};
struct Line{
Point p1,p2;
};
double Max(double a,double b)
{
return a>b?a:b;
}
double Min(double a,double b)
{
return a<b?a:b;
}
double xmulti(Point p1,Point p2,Point p0)
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
}
bool inter(Line l1,Line l2)
{
if( Min(l2.p1.x,l2.p2.x)<=Max(l1.p1.x,l1.p2.x) &&
Min(l2.p1.y,l2.p2.y)<=Max(l1.p1.y,l1.p2.y) &&
Min(l1.p1.x,l1.p2.x)<=Max(l2.p1.x,l2.p2.x) &&
Min(l1.p1.y,l1.p2.y)<=Max(l2.p1.y,l2.p2.y) &&
xmulti(l1.p1,l2.p2,l2.p1)*xmulti(l1.p2,l2.p2,l2.p1)<= &&
xmulti(l2.p1,l1.p2,l1.p1)*xmulti(l2.p2,l1.p2,l1.p1)<= )
return true;
else
return false;
}
int main()
{
int N;
Line l[];
while(cin>>N){
if(N==) break;
for(int i=;i<=N;i++)
cin>>l[i].p1.x>>l[i].p1.y>>l[i].p2.x>>l[i].p2.y;
int c = ;
for(int i=;i<=N-;i++)
for(int j=i+;j<=N;j++)
if(inter(l[i],l[j]))
c++;
cout<<c<<endl;
}
return ;
}
Freecode : www.cnblogs.com/yym2013
hdu 1086:You can Solve a Geometry Problem too(计算几何,判断两线段相交,水题)的更多相关文章
- 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( 判断线段是否相交 水题 )
链接:传送门 题意:给出 n 个线段找到交点个数 思路:数据量小,直接暴力判断所有线段是否相交 /*************************************************** ...
- 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 ...
- 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 ...
- (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 [线段相交]
题目:给出一些线段,判断有几个交点. 问题:如何判断两条线段是否相交? 向量叉乘(行列式计算):向量a(x1,y1),向量b(x2,y2): 首先我们要明白一个定理:向量a×向量b(×为向量叉乘),若 ...
- hdu 1147:Pick-up sticks(基本题,判断两线段相交)
Pick-up sticks Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- 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 ...
随机推荐
- Windows Store GIF player 诞生记
在Win8上面,Image source切换的时候有bug.当我们短时间定时切换的时候,Image不能正常地显示对应的图片.Image控件又不支持GIF播放,所以GIF图片的播放就是一个非常头痛的问题 ...
- Android酷炫加载进度动画
概述 本自定义动画进度酷炫View,是加载进度动画的自定义View,继承于ImageView来实现,主要实现蒙层加载进度的加载进度效果. 支持水平左右加载和垂直上下加载四个方向,同时也支持自定义蒙层进 ...
- javascript 异常基本语法
http://www.w3school.com.cn/js/js_onerror.asp try...catch 的作用是测试代码中的错误. JavaScript - 捕获错误 当我们在网上冲浪时 ...
- Linux-软件包管理-rpm命令管理-校验、文件提取
rpm -V httpd 查看已安装的apache包中文件信息是否已经被人修改 rpm -ql httpd 查看已安装的apache包中文件的位置 vim /etc/httpd/conf/httpd. ...
- HDU 4280Island Transport(网络流之最大流)
题目地址:pid=4280">http://acm.hdu.edu.cn/showproblem.php? pid=4280 这个题是一个纯最大流模板题..就是用来卡时间的.. 还好我 ...
- MFC总结之CListCtrl用法及技巧(二)
续第一篇:MFC总结之CListCtrl用法及技巧(一) http://blog.csdn.net/zwgdft/article/details/7560592 本篇重点介绍:禁止拖动表头.让第一列居 ...
- 改善用户体验,用图片的自身变化以及进度通知摆脱传统的进度条,okhttp,Canvas,Paint实现
转载请注明出处:王亟亟的大牛之路 从最開始的白页面等待,到后来的进度条告知用户.到如今的WebBO/微信这样的先下缩略图点击才又一次下大图的方式,我们开发人员对用户感知的注意度越来越高.昨天刷微博的时 ...
- mplayer 全屏问题
[root@ok home]# gedit ~/.mplayer/config # Write your default config options here! zoom=yes #加上这个参数!全 ...
- Missing separate debuginfos, use: debuginfo-install
环境:CentOS6.2 64位 操作:使用gdb调试C++查询MySQL数据库的程序 原因: 解决办法: 1. 修改文件/etc/yum.repos.d/CentOS-Debuginfo.repo ...
- lodop简单入门教程
lodop简单入门 1 安装(这个不介绍,下载安装即可) 声明只能装windows,linux不能装,所以linux 服务器要使用直接使用http://localhost:8000/CLodopfun ...