hdu 1086(计算几何入门题——计算线段交点个数)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1086
You can Solve a Geometry Problem too
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7167 Accepted Submission(s): 3480
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.
//////////////////////////////////////////////////////////////////////////////直接上的模板解决的,不知是喜是忧啊
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <algorithm> using namespace std;
const int MAX=;
const double eps = 1e-; struct point
{
double x,y;
}; struct beline
{
point a,b;
}; point p[MAX];
int n=;
bool dy(double x,double y)
{
return x>y+eps;
}
bool xy(double x,double y)
{
return x<y-eps;
}
bool dyd(double x,double y)
{
return x > y - eps;
}
bool xyd(double x,double y)
{
return x<y+eps;
}
bool dd(double x,double y)
{
return fabs(x-y) < eps;
}
double crossProduct(point a,point b,point c)
{
return (c.x - a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y);
}
bool onSegment(point a,point b,point c)
{
double maxx=max(a.x,b.x);
double maxy=max(a.y,b.y);
double minx=min(a.x,b.x);
double miny=min(a.y,b.y);
if(dd(crossProduct(a,b,c),0.0)&&dyd(c.x,minx)&&xyd(c.x,maxx)&&dyd(c.y,miny)&&xyd(c.y,maxy))
return true;
return false;
} bool segIntersect(point p1,point p2,point p3,point p4)
{
double d1 = crossProduct(p3,p4,p1);
double d2 = crossProduct(p3,p4,p2);
double d3 = crossProduct(p1,p2,p3);
double d4 = crossProduct(p1,p2,p4);
if(xy(d1 * d2,0.0)&&xy(d3*d4,0.0))
return true;
if(dd(d1,0.0)&&onSegment(p3,p4,p1))
return true;
if(dd(d2,0.0)&&onSegment(p3,p4,p2))
return true;
if(dd(d3,0.0)&&onSegment(p1,p2,p3))
return true;
if(dd(d4,0.0)&&onSegment(p1,p2,p4))
return true;
return false;
} int main()
{
int cas,i,j;
while(scanf("%d",&cas)!=EOF&&cas!=)
{
beline L[MAX];
n=;
for(i=;i<cas;i++)
{
scanf("%lf%lf%lf%lf",&L[i].a.x,&L[i].a.y,&L[i].b.x,&L[i].b.y);
}
for(i=;i<cas;i++)
{
for(j=i+;j<cas;j++)
if(segIntersect(L[i].a,L[i].b,L[j].a,L[j].b))
{
n++;
}
}
printf("%d\n",n);
}
return ;
}
hdu 1086(计算几何入门题——计算线段交点个数)的更多相关文章
- EDU 50 E. Covered Points 利用克莱姆法则计算线段交点
E. Covered Points 利用克莱姆法则计算线段交点.n^2枚举,最后把个数开方,从ans中减去. ans加上每个线段的定点数, 定点数用gcs(△x , △y)+1计算. #include ...
- POJ P2318 TOYS与POJ P1269 Intersecting Lines——计算几何入门题两道
rt,计算几何入门: TOYS Calculate the number of toys that land in each bin of a partitioned toy box. Mom and ...
- *HDU 1086 计算几何
You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3 ...
- hdu 4720 计算几何简单题
昨天用vim练了一道大水题,今天特地找了道稍难一点的题.不过也不是很难,简单的计算几何而已.练习用vim编码,用gdb调试,结果居然1A了,没调试...囧... 做法很简单,无非就是两种情况:①三个巫 ...
- hdu 3062 2-sat入门题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3062 #include <cstdio> #include <cmath> # ...
- [HDU]1016 DFS入门题
题目的意思就是在1到n的所有序列之间,找出所有相邻的数相加是素数的序列.Ps:题目是环,所以头和尾也要算哦~ 典型的dfs,然后剪枝. 这题目有意思的就是用java跑回在tle的边缘,第一次提交就tl ...
- Rikka with Mista 线段树求交点个数
由于上下线段是不可能有交点的 可以先看左右线段树,按照y递增的顺序,对点进行排序. 升序构造,那么对于从某一点往下的射线,对于L,R进行区间覆盖,线段交点个数就是单点的被覆盖的次数. 降序构造,那么对 ...
- 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 2857:Mirror and Light(计算几何,点关于直线的对称点,求两线段交点坐标)
Mirror and Light Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
随机推荐
- javascript 金额格式化
金额格式化 example: <!DOCTYPE html> <html> <head> <script src="http://code.jque ...
- 使用MeanJS Yeoman Generator
1.首先全局安装该生成器 sudo npm install -g generator-meanjs 2.为项目创建一个路径 mkdir xmen && cd xmen 3.创建app ...
- php curl应该怎么使用呢
原php默认并不进行此项功能的扩展,但还是有的,只是没有让它生效罢了.打开PHP安装目录,搜索以下三个文件 ssleay32.dll.libeay32.dll和 php_ curl .dll,一一拷贝 ...
- Entrust是一种为Laravel5添加基于角色的权限的简洁而灵活的方法。
安装 首先要在composer.json中添加: "zizaco/entrust": "5.2.x-dev" 然后运行composer install 或者 c ...
- JavaEE基础(十一)/Eclipse介绍
1.Java开发工具(常见开发工具介绍) A:操作系统自带的记事本软件 B:高级记事本软件 C:集成开发环境 IDE (Integrated Development Environment) D:Ec ...
- C#中一个关于不同窗体间的颜色参数的传递
1目标是 在弹出菜单中选择颜色,在主菜单中对控件进行操作(弹出菜单选择的颜色就是主菜单控件的颜色) 2颜色属性需要来回转换(也许不用转换,暂时还不会,有会的提醒下,TKS) 3用到一个颜色控件(col ...
- ecshop订单打印页显示商品缩略图和序号
ecshop订单打印页显示商品缩略图和序号 订单打印页显示商品缩略图,在论坛没找到适合2.7.2相关的文章,特意贴上来给大家研究一下.1.找到 $sql = "SELECT o.*, IF( ...
- [thml]HTML select标签 获取选中的option的value及Text内容
很简单的select标签: <select id="hello" onchange="getContent(this.value,this.options[this ...
- 基于SURF特征的图像与视频拼接技术的研究和实现(一)
基于SURF特征的图像与视频拼接技术的研究和实现(一) 一直有计划研究实时图像拼接,但是直到最近拜读西电2013年张亚娟的<基于SURF特征的图像与视频拼接技术的研究和实现>,条 ...
- C#获得枚举类型的长度
enum MyEnum { Value1, Value2, } class Program { static void Main(string[] args) { var e = new MyEnum ...