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
才开始学向量,还在写模板中
题意:给你n条线段,判断相交点的个数(重复相交重复计算)
题解:线段相交,就快速排斥和跨立实验两个步骤吧,不懂戳着
附上AC代码
//#include"bits/stdc++.h"
//#include<unordered_map>
//#include<unordered_set>
#include<iostream>
#include<sstream>
#include<iterator>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<set>
#include<vector>
#include<bitset>
#include<climits>
#include<queue>
#include<iomanip>
#include<cmath>
#include<stack>
#include<map>
#include<ctime>
#include<new>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define MT(a,b) memset(a,b,sizeof(a))
#define lson l, mid, node<<1
#define rson mid + 1, r, node<<1|1
#define Root 1, m, 1
#define Node l, r, node
const int INF = 0x3f3f3f3f;
const int O = 1e6;
const int mod = 20071027;
const int maxn = 5e3 + 5;
const double PI = acos(-1.0);
const double E = 2.718281828459;
const double esp = 1e-8;
struct Point { double x, y; };
typedef Point Vector;
Vector operator + (Vector a, Vector b) { return { a.x + b.x, a.y + b.y }; }
Vector operator - (Vector a, Vector b) { return { a.x - b.x, a.y - b.y }; }
Vector operator * (Vector a, double p) { return { a.x * p, a.y * p}; }
Vector operator / (Vector a, double p) { return { a.x / p, a.y / p}; }
bool operator < (Vector a, Vector b) { return a.x * a.x + a.y * a.y < b.x * b.x + b.y * b.y; }
bool operator == (Vector a, Vector b) { return fabs(a.x - b.x) < esp && fabs(a.y - b.y) < esp; }
// 点积,模长,角度,叉积,逆时针旋转
double dotProduct (Vector a, Vector b) { return a.x * b.x + a.y * b.y; }
double getLen (Vector a) { return sqrt (a.x * a.x + a.y * a.y); }
double angle ( Vector a, Vector b) { return acos( dotProduct(a, b) / getLen(a) / getLen(b)); }
double CrossProduct (Vector a, Vector b) { return a.x * b.y - a.y * b.x; }
Vector Rotate (Vector a, double rad) { return { a.x*cos(rad) - a.y*sin(rad), a.x*sin(rad) + a.y*cos(rad) }; }
// 判断线段AB是否跨立MN
bool straddle (Point A, Point B, Point M, Point N) {
Vector p0 = { N.x - M.x , N.y - M.y }; // 向量 MN
Vector p1 = { A.x - M.x , A.y - M.y }; // 向量 MA
Vector p2 = { B.x - M.x , B.y - M.y }; // 向量 MB
return CrossProduct(p1, p0) * CrossProduct(p2, p0) <= 0;
}
// 判断 线段AB为对角线构成的矩形 和 MN线段为对角线构成的矩形 是否存在重叠
bool overlap (Point A, Point B, Point M, Point N){
Point P = { max(min(A.x, B.x), min(M.x, N.x)), max(min(A.y, B.y), min(M.y, N.y))};
Point Q = { min(max(A.x, B.x), max(M.x, N.x)), min(max(A.y, B.y), max(M.y, N.y))};
return P.x <= Q.x && P.y <= Q.y;
}
// 判断线段AB和MN是否相交
bool intersect (Point A, Point B, Point M, Point N) {
return straddle(A, B, M, N) && straddle(M, N, A, B) && overlap(A, B, M, N);
}
int main () {
int n;
while( scanf("%d", &n) && n){
int ans = 0;
Point p1[maxn], p2[maxn];
for(int i=0; i<n; i++) {
scanf("%lf%lf%lf%lf", &p1[i].x, &p1[i].y, &p2[i].x, &p2[i].y);
for(int j=0; j<i; j++) if(intersect(p1[i], p2[i], p1[j], p2[j])) ans ++;
}
printf("%d\n", ans);
}
return 0;
}