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

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

//////////////////////////////////////////////////////////////////////////////直接上的模板解决的,不知是喜是忧啊

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <algorithm>

using namespace std;
const int MAX=110;
const double eps = 1e-6;

struct point
{
    double x,y;
};

struct beline
{
    point a,b;
};

point p[MAX];
int n=0;
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!=0)
    {
         beline L[MAX];
         n=0;
         for(i=0;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=0;i<cas;i++)
         {
             for(j=i+1;j<cas;j++)
                 if(segIntersect(L[i].a,L[i].b,L[j].a,L[j].b))
                 {
                     n++;
                 }
         }
         printf("%d\n",n);
    }
    return 0;
}

hdu 1086(计算几何入门题——计算线段交点个数),布布扣,bubuko.com

时间: 2024-10-24 11:29:23

hdu 1086(计算几何入门题——计算线段交点个数)的相关文章

poj1039——计算几何 求直线与线段交点,判断两条直线是否相交

poj1039——计算几何  求直线与线段交点,判断两条直线是否相交 Pipe Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9439   Accepted: 2854 Description The GX Light Pipeline Company started to prepare bent pipes for the new transgalactic light pipeline. During the de

EDU 50 E. Covered Points 利用克莱姆法则计算线段交点

E. Covered Points 利用克莱姆法则计算线段交点.n^2枚举,最后把个数开方,从ans中减去. ans加上每个线段的定点数, 定点数用gcs(△x , △y)+1计算. #include <algorithm> #include <iterator> #include <iostream> #include <cstring> #include <cstdlib> #include <iomanip> #include

*HDU 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): 10259    Accepted Submission(s): 5074 Problem Description Many geometry(几何)problems were designed in the ACM/

hdu 2571 dp入门题

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2571 常规dp,刚好让我这菜鸟来找找 找状态转移方程的感觉.. 定义dp[i][j]  表示的是第i行j列所拥有的最大的幸运值. dp[i][j] 由三种情况转移而来:1.从上面一个坐标转移而来,即dp[i-1][j] .2.由左边转移过来,即dp[i][j-1].3.由他同一行中的 j 的因数列转移过来的(但是要除了j自己),即dp[i][ j 的因数列] 找出这三种情况中最大的就可以了,dp[i

[HDU]1016 DFS入门题

题目的意思就是在1到n的所有序列之间,找出所有相邻的数相加是素数的序列.Ps:题目是环,所以头和尾也要算哦~ 典型的dfs,然后剪枝. 这题目有意思的就是用java跑回在tle的边缘,第一次提交就tle了(服务器负载的问题吧),一模一样的第二次提交就ac了,侧面也反应了递归对stack的开销影响效率也是严重的.好了,上代码!! 题目传送门: HDU_1016 import java.util.Scanner; public class Main { public static final int

hdu 1754:I Hate It(线段树,入门题,RMQ问题)

I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 33726    Accepted Submission(s): 13266 Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少.这让很多学生很反感.不管你喜不喜欢,现在需要你做的是,就是按照老师的要求

HDU 1086 You can Solve a Geometry Problem too(判断线段相交)

题目地址:HDU 1086 就这么一道仅仅判断线段相交的题目写了2k多B的代码..是不是有点浪费...但是我觉得似乎哪里也优化不了了.... 判断线段相交就是利用的叉积.假如现在两条线段分别是L1和L2,先求L1和L2两个端点与L1的某个端点的向量的叉积,如果这两个的叉积的乘积小于0的话,说明L1在是在L2两个端点之间的,但此时并不保证一定相交.此时需要用同样的方法去判断L2是否在L1的两个端点之间,如果L2也在L1的两个端点之间的话,那就足以说明L1与L2相交.但是这题还需要判断是否端点也相交

sdut 2603:Rescue The Princess(第四届山东省省赛原题,计算几何,向量旋转 + 向量交点)

Rescue The Princess Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Several days ago, a beast caught a beautiful princess and the princess was put in prison. To rescue the princess, a prince who wanted to marry the princess set out immedia

HDU 1166 敌兵布阵【树状数组入门题】

敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 74051    Accepted Submission(s): 31080 Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务