HDU 1086 You can Solve a Geometry Problem too (determine the intersection of line segments)
Address: HDU 1086
In this case, I wrote more than 2 k B code to determine the intersection of line segments .. Is it a waste... But I don't think I can optimize it any more ....
The cross product is used to determine the intersection of line segments. If the two line segments are L1 and L2, calculate the cross product of the vectors of one of L1 and L2 endpoints and one of L1 endpoints first. If the product of the two cross products is less than 0, it indicates that L1 is between two L2 endpoints, but at this time it is not guaranteed to be intersecting. In this case, you need to use the same method to determine whether L2 is between two L1 endpoints. If L2 is also between two L1 endpoints, it is enough to indicate that L1 and L2 are at the same intersection. However, you still need to determine whether the endpoint is also the same. At that time, you did not expect this, resulting in a period of time .. As for the determination of the endpoint, I did not expect any good methods .. It directly judges whether the four endpoints are the same ..
The Code is as follows:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include using namespace std;#define eqs 1e-10struct node{ double x, y;} point[1000];node xiang(node a, node b){ node f1; f1.x=a.x-b.x; f1.y=a.y-b.y; return f1;}int cross(node a, node b){ double c; c= a.x*b.y-a.y*b.x; if(c>0) return 1; else if(c==0) return 0; else return -1;}int dcmp(double x, double y){ if(fabs(x-y)<=eqs) return 1; return 0;}int main(){ int n, i, j; int c1, c2, c3, c4, ans; while(scanf("%d",&n)!=EOF&&n) { ans=0; for(i=0; i