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): 199 Accepted Submission(s): 132 |
Problem Description
Many geometry(几何)problems were designed in the ACM/ICPC. And now, I also prepare a geometry problem for this final exam. According to the experience of many ACMers, geometry problems are always much trouble, but this problem is very easy, after all we are now attending an exam, not a contest :) 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. |
Input
Input contains multiple test cases. Each test case contains a integer N (1=N<=100) in a line first, and then N lines follow. Each line describes one segment with four float values x1, y1, x2, y2 which are coordinates of the segment’s ending. A test case starting with 0 terminates the input and this test case is not to be processed.
|
Output
For each case, print the number of intersections, and one line one case.
|
Sample Input
2 |
Sample Output
1 |
Author
lcy
|
/*
计算几何求所给线段的交点数量 */
#include<bits/stdc++.h>
using namespace std;
struct Point{//点
double x,y;
Point(){}
Point(int a,int b){
x=a;
y=b;
}
void input(){
scanf("%lf%lf",&x,&y);
}
};
struct Line{//线段
Point a,b;
Line(){}
Line(Point x,Point y){
a=x;
b=y;
}
void input(){
a.input();
b.input();
}
};
bool judge(Point &a,Point &b,Point &c,Point &d)
{
if(!(min(a.x,b.x)<=max(c.x,d.x) && min(c.y,d.y)<=max(a.y,b.y)&&
min(c.x,d.x)<=max(a.x,b.x) && min(a.y,b.y)<=max(c.y,d.y)))//这里的确如此,这一步是判定两矩形是否相交
//特别要注意一个矩形含于另一个矩形之内的情况
return false;
double u,v,w,z;//分别记录两个向量
u=(c.x-a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y);
v=(d.x-a.x)*(b.y-a.y)-(b.x-a.x)*(d.y-a.y);
w=(a.x-c.x)*(d.y-c.y)-(d.x-c.x)*(a.y-c.y);
z=(b.x-c.x)*(d.y-c.y)-(d.x-c.x)*(b.y-c.y);
return (u*v<=0.00000001 && w*z<=0.00000001);
}
vector<Line>v;//用来存放线段
int n;
Line a;
void init(){
v.clear();
}
int main(){
//freopen("in.txt","r",stdin);
while(scanf("%d",&n)!=EOF&&n){
init();
for(int i=;i<n;i++){
a.input();
v.push_back(a);
}//将线段存入
int cur=;
for(int i=;i<v.size();i++){
for(int j=i+;j<v.size();j++)
if(judge(v[i].a,v[i].b,v[j].a,v[j].b))
cur++;
}
printf("%d\n",cur);
}
return ;
}
You can Solve a Geometry Problem too(判断两线段是否相交)的更多相关文章
- HDU 1086You can Solve a Geometry Problem too(判断两条选段是否有交点)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1086 判断两条线段是否有交点,我用的是跨立实验法: 两条线段分别是A1到B1,A2到B2,很显然,如果 ...
- 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 ...
- NYOJ 1016 判断两线段是否相交
#include<cstdio> #include<cmath> #include<iostream> #include<algorithm> #inc ...
- Pick-up sticks--poj2653(判断两线段是否相交)
http://poj.org/problem?id=2653 题目大意:有n根各种长度的棍 一同洒在地上 求在最上面的棍子有那几个 分析: 我刚开始想倒着遍历 因为n是100000 想着会 ...
- 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 ...
- 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 ...
- HDU1086 You can Solve a Geometry Problem too(计算几何)
You can Solve a Geometry Problem too Time Limit: 2000/1000 M ...
- hdu 1086 You can Solve a Geometry Problem too [线段相交]
题目:给出一些线段,判断有几个交点. 问题:如何判断两条线段是否相交? 向量叉乘(行列式计算):向量a(x1,y1),向量b(x2,y2): 首先我们要明白一个定理:向量a×向量b(×为向量叉乘),若 ...
- HDU 1086 You can Solve a Geometry Problem too( 判断线段是否相交 水题 )
链接:传送门 题意:给出 n 个线段找到交点个数 思路:数据量小,直接暴力判断所有线段是否相交 /*************************************************** ...
随机推荐
- touch.js——常见应用操作
touch.js--常见应用操作 基本事件: touchstart //手指刚接触屏幕时触发 touchmove //手指在屏幕上移动时触发 touchend //手指从屏幕上移开时 ...
- 阿里云服务器解决mysql远程连接失败问题
嗯,自己买了个阿里云的学生机服务器,奈何装了mysql以后一直不能连接,也是够笨的. 记录一下自己遇到的问题. 当然了,首先需要在阿里云安全组开放3306端口,第一次玩儿云服务器差点把我搞坏了.... ...
- .NET Excel导出方法及其常见问题详解
摘要:.NET Excel导出方法及其常见问题详解. 一.Excel导出的实现方法 在.net 程序开发中,对于Excel文件的导出我们一共有三种导出方式: 利用文件输出流进行读写操作 这种方式的导出 ...
- AngularJS -- Bootstrap(启动器)
点击查看AngularJS系列目录 转载请注明出处:http://www.cnblogs.com/leosx/ Bootstrap(初始化) 这章介绍了Angular的初始化过程,以及如何在必要的时候 ...
- MySQL主从同步和读写分离的配置
主服务器:192.168.1.126 从服务器:192.168.1.163 amoeba代理服务器:192.168.1.237 系统全部是CentOS 6.7 1.配置主从同步 1.1.修改主服务器( ...
- java集合系列——List集合之LinkedList介绍(三)
1. LinkedList的简介 JDK 1.7 LinkedList是基于链表实现的,从源码可以看出是一个双向链表.除了当做链表使用外,它也可以被当作堆栈.队列或双端队列进行操作.不是线程安全的,继 ...
- 再起航,我的学习笔记之JavaScript设计模式28(委托模式)
## 委托模式 ### 概念介绍 **委托模式(Entrust): **多个对象接收并处理同一请求,他们将请求委托给另一个对象统一处理请求. ### 利用委托优化循环 如果我们有一个需求需要让用户点击 ...
- Power Sum 竟然用原根来求
Power Sum Time Limit: 20000/10000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitS ...
- CentOS7.3下部署Rsyslog+LogAnalyzer+MySQL中央日志服务器
一.简介 1.LogAnalyzer 是一款syslog日志和其他网络事件数据的Web前端.它提供了对日志的简单浏览.搜索.基本分析和一些图表报告的功能.数据可以从数据库或一般的syslog文本文件中 ...
- Django 1.10中文文档-聚合
Django 数据库抽象API 描述了使用Django 查询来增删查改单个对象的方法. 然而,有时候你要获取的值需要根据一组对象聚合后才能得到. 这个主题指南描述了如何使用Django的查询来生成和返 ...