hdoj1086 You can Solve a Geometry Problem too

#include<iostream>
#include
<stdio.h>
using namespace std;
int n;
typedef 
struct {        //定义点
    double x, y;
} point;
struct segment {
    point fir, end;
};
segment pnt[
100];
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 <= 0.0000000001 && j * k <= 0.0000000001;
}

int main()
{
    
while (scanf("%d"&n) != EOF && n) {
    
int sum = 0;
    
for (int i = 0; i < n; ++i) {
        scanf(
"%lf%lf%lf%lf"&pnt[i].fir.x, &pnt[i].fir.y,
          
&pnt[i].end.x, &pnt[i].end.y);
    }
    
for (int i = 0; i < n; ++i) {
        
for (int j = i + 1; j < n; ++j) {
        
if (inter(pnt[i].fir, pnt[i].end, pnt[j].fir, pnt[j].end))
            sum
++;
        }
    }
    printf(
"%d\n", sum);
    }
}


posted @ 2010-05-07 11:32  open source  阅读(179)  评论(0编辑  收藏  举报