You can Solve a Geometry Problem too

                                        Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
                                        

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
0.00 0.00 1.00 1.00
0.00 1.00 1.00 0.00
3
0.00 0.00 1.00 1.00
0.00 1.00 1.00 0.000
0.00 0.00 1.00 0.00
0
 
Sample Output
1
3
 
Author
lcy
 
 
直接O(N^2)判断两线段是否相交即可。
 
判断线段是否相交的模板:
 
 inline double CrossProduct(node a, node b, node c){
return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
}
//Calculate the crossproduct inline bool SegX(node p1, node p2, node p3, node 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);
return (d1 * d2 <= && d3 * d4 <= );
}
//Judge whether the line segments intersact
 那么直接套用一下就好了。
 
 #include <bits/stdc++.h>

 using namespace std;

 struct node{
double x, y;
} pa[], pb[]; int n, num; inline double CrossProduct(node a, node b, node c){
return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
} inline bool SegX(node p1, node p2, node p3, node 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);
return (d1 * d2 <= && d3 * d4 <= );
} int main(){ while (~scanf("%d", &n), n){
num = ;
for (int i = ; i <= n; ++i) scanf("%lf%lf%lf%lf", &pa[i].x, &pa[i].y, &pb[i].x, &pb[i].y);
for (int i = ; i <= n - ; ++i)
for (int j = i + ; j <= n; ++j)
if (SegX(pa[i], pb[i], pa[j], pb[j])) ++num;
printf("%d\n", num);
} return ; }

HDU1086 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 MS (Java/Others)    Memory Limit: 65536/3 ...

  2. hdu_1086 You can Solve a Geometry Problem too(计算几何)

    http://acm.hdu.edu.cn/showproblem.php?pid=1086 分析:简单计算几何题,相交判断直接用模板即可. 思路:将第k条直线与前面k-1条直线进行相交判断,因为题目 ...

  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 ...

  4. 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 ...

  5. 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 ...

  6. (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/ ...

  7. 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 ...

  8. 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(线段求交)

    http://acm.hdu.edu.cn/showproblem.php?pid=1086 You can Solve a Geometry Problem too Time Limit: 2000 ...

随机推荐

  1. OpenCV学习笔记(六) 滤波器 形态学操作(腐蚀、膨胀等)

    转自:OpenCV 教程 另附:计算机视觉:算法与应用(2012),Learning OpenCV(2009) 平滑图像:滤波器 平滑 也称 模糊, 是一项简单且使用频率很高的图像处理方法.平滑处理的 ...

  2. 小米r3g旧版开发版固件,安装opkg

    1.开启ssh 1.1.刷入固件 在路由器更新界面,刷入 miwifi_r3g_firmware_c2175_2.25.122.bin 固件 下载地址: http://bigota.miwifi.co ...

  3. 安装Mysql community server遇到计算机中丢失msvcr120.dll

    一.下载community server版本 Mysql community server版本:https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7 ...

  4. 递归查询子类sql

    --通过父节点查询子节点 WITH TREE AS( SELECT * FROM Role WHERE RoleID = 4 -- 要查询的父 id UNION ALL SELECT Role.* F ...

  5. 使用bat命令实现拖动快速安装APK包

    平时安装APK包,每次都要打命令adb install *********** 很繁琐,网上找到一个用BAT命令快速安装的方法   在桌面创建一个bat文件,输入: @echo off title i ...

  6. 安装 Redis的Python客户端redis-py

    安装 redis-py 使用easy_install sudo easy_install redis 源码安装 git clone https://github.com/andymccurdy/red ...

  7. 使用jQuery ui创建模态表单

    jQuery UI 是一个建立在 jQuery JavaScript 库上的小部件和交互库,可以使用它创建高度交互的 Web 应用程序. 在web页面的开发过程中,在添加元素的时候需要用到弹出窗口添加 ...

  8. 带线表格据gt生成无线表格

    json解析练习,python图像处理练习,表格包围框毛刺问题待解决. # -*- coding: utf-8 -*- # coding: utf-8 from PIL import Image, I ...

  9. 深入学习之mysql(五)连接查询

    深入学习Mysql(五)连接查询 1.准备数据库: CREATE DATABASE IF NOT EXISTS `db_book2` DEFAULT CHARACTER SET UTF8; USE ` ...

  10. linux自动执行指令crontab和at

    目录 1 at和crontab指令 2 batch 一.at与crontab的区别 运行方式不同 at只运行一次,crontab循环运行 依赖的服务不同 at 对应的服务是 atd crontab 对 ...