hdoj1086 You can Solve a Geometry Problem too
2010-05-07 11:32
344 查看
#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);
}
}
#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);
}
}
相关文章推荐
- 【HDOJ】1086 You can Solve a Geometry Problem too
- hdoj 1086 You can Solve a Geometry Problem too (判断线段相交)
- hdoj 1086 You can Solve a Geometry Problem too 【计算几何】
- hdoj1086 You can Solve a Geometry Problem too(数学几何题)
- you can Solve a Geometry Problem too(hdoj1086)
- You can Solve a Geometry Problem too—hdoj 1086(线段相交)
- HDOJ 1086 You can Solve a Geometry Problem too
- HDOJ 1086 You can Solve a Geometry Problem too 线段相交问题
- hdu/hdoj 1086 You can Solve a Geometry Problem too
- hdu 1086 You can Solve a Geometry Problem too(线段相交+枚举)
- hdu 1086 You can Solve a Geometry Problem too (几何)
- HDU 1086 You can Solve a Geometry Problem too(水题)
- hdu 1086 You can Solve a Geometry Problem too(线段相交点的个数)
- HDU-1086 You can Solve a Geometry Problem too 解题报告
- HDU_1086 You can Solve a Geometry Problem too(几何题)
- HDU-1086 You can Solve a Geometry Problem too
- HDU 1086 You can Solve a Geometry Problem too
- hdu 1086 You can Solve a Geometry Problem too(线段相交的交点个数)
- HDU 1086 You can Solve a Geometry Problem too
- HDU 1086 You can Solve a Geometry Problem too(简单的线段相交)