险段相交(附模板)_

来源:互联网 发布:同花顺股票模拟软件 编辑:程序博客网 时间:2024/05/23 16:53
线段相交(附模板)
  - You can Solve a Geometry Problem too Time Limit:1000MS     Memory Limit:32768KB    64bit IO Format:%I64d & %I64u
SubmitStatus

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

20.00 0.00 1.00 1.000.00 1.00 1.00 0.0030.00 0.00 1.00 1.000.00 1.00 1.00 0.0000.00 0.00 1.00 0.000
 

Sample Output

13
题解
1 2 3 4 5 6 7 8 9101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
#include <iostream>
#include <cstdio>
using namespace std;
struct Line{
double x1, y1;
double x2, y2;
}l[205];
const double eps=1e-10;
double min(doublea, double b) {
 return a < b? a : b;
}
double max(doublea, double b) {
 return a > b? a : b;
}
bool inter(Linel1, Line l2){
 double ax = l1.x1,ay = l1.y1;
  double bx = l1.x2,by = l1.y2;
 double cx = l2.x1,cy = l2.y1;
double dx = l2.x2,dy = l2.y2;
if( min(ax,bx) > max(cx,dx) || min(ay,by) > max(cy,dy)
|| min(cx, dx) > max(ax,bx) || min(cy,dy) > max(ay,by) )
return 0;
double h, i,j, k;
h = (bx - ax) * (cy -ay) - (by - ay) * (cx- ax);
i = (bx - ax) * (dy -ay) - (by - ay) * (dx- ax);
 j = (dx - cx) * (ay- cy) - (dy- cy) * (ax- cx);
k = (dx - cx) * (by -cy) - (dy - cy) * (bx- cx);
return h * i <=eps && j * k <= eps;
}int main(){
int n; int cnt;
while (~scanf("%d",&n)){
if (n == 0)
 break;
cnt = 0;
for (int i=0;i<n; i++)
scanf("%lf%lf%lf%lf", &l[i].x1,&l[i].y1,&l[i].x2,&l[i].y2);
for (int i=0;i<n-1;i++) {
 for (int j=i+1;j<n; j++)
{
if (inter(l[i],l[j])) cnt ++;
}
 }
printf("%d\n",cnt);
}
return 0;
}
 
#include <cstdio>

using namespace std;

struct Line
{
    double x1, y1;
    double x2, y2;
}l[205];

const double eps=1e-10;
double min(double a, double b) { return a < b ? a : b; }
double max(double a, double b) { return a > b ? a : b; }
bool inter(Line l1, Line l2)
{
    double ax = l1.x1, ay = l1.y1;
    double bx = l1.x2, by = l1.y2;
    double cx = l2.x1, cy = l2.y1;
    double dx = l2.x2, dy = l2.y2;
    if( min(ax, bx) > max(cx, dx) ||
        min(ay, by) > max(cy, dy) ||
        min(cx, dx) > max(ax, bx) ||
        min(cy, dy) > max(ay, by) ) return 0;
    double h, i, j, k;
    h = (bx - ax) * (cy - ay) - (by - ay) * (cx - ax);
    i = (bx - ax) * (dy - ay) - (by - ay) * (dx - ax);
    j = (dx - cx) * (ay - cy) - (dy - cy) * (ax - cx);
    k = (dx - cx) * (by - cy) - (dy - cy) * (bx - cx);
    return h * i <= eps && j * k <= eps;
}

int main()
{
    int n;
    int cnt;
    while (~scanf("%d", &n))
    {
        if (n == 0)
           break;
        cnt = 0;
        for (int i=0; i<n; i++)
            scanf("%lf%lf%lf%lf", &l[i].x1, &l[i].y1, &l[i].x2, &l[i].y2);
        for (int i=0; i<n-1; i++)
        {
            for (int j=i+1; j<n; j++)
            {
                if (inter(l[i], l[j]))
                    cnt ++;
            }
        }
        printf("%d\n", cnt);
    }

    return 0;
}
模板
//线段相交
const double eps=1e-10;
struct point { double x, y; };
double min(double a, double b) { return a < b ? a : b; }
double max(double a, double b) { return a > b ? a : b; }
bool inter(point a, point b, point c, point d){
if( min(a.x, b.x) > max(c.x, d.x) ||
  min(a.y, b.y) > max(c.y, d.y) ||
  min(c.x, d.x) > max(a.x, b.x) ||
  min(c.y, d.y) > max(a.y, b.y) ) return 0;
double h, i, j, k;
 h = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
 i = (b.x - a.x) * (d.y - a.y) - (b.y - a.y) * (d.x - a.x);
 j = (d.x - c.x) * (a.y - c.y) - (d.y - c.y) * (a.x - c.x);
 k = (d.x - c.x) * (b.y - c.y) - (d.y - c.y) * (b.x - c.x);
return h * i <= eps && j * k <= eps;




0 0