Today began to learn computational geometry, Baidu has two articles, and June Mutual encouragement!
Recommended for computational Geometry entry questions
Computational Geometry Basics
Test instructions: There is a box, divided by n pieces of wood into n+1 areas, each plank from left to right appears, and does not cross. There are M toys (which can be viewed as points) in this box, asking how many toys each area has.
Idea: First, use the cross product to determine whether the toy is on the left side of the board, and then use two points to find the right side of the board, the board answer plus one.
#include <stdio.h>#include<string.h>structpoint{intx, y; Point () {}, point (intX_inty_) {x=x_,y=Y_; } Pointoperator-(ConstPoint &b)Const{ returnPoint (x-b.x,y-b.y); } int operator*(ConstPoint &b)Const{//dot Product returnx*b.x+y*b.y; } int operator^(ConstPoint &b)Const{//Cross Product returnx*b.y-y*b.x; }};intCal (Point p0,point p1,point p2) {//less than 0 means left-folded at P1, greater than 0 right-fold, equals 0-line return(p1-p0) ^ (p2-p0);}Const intn=5555;p oint s[n],e[n];intAns[n];intMain () {intN,m,x1,x2,y1,y2; intx3,x4,i,f=0; while(~SCANF ("%d", &n) &&N) {scanf ("%d%d%d%d%d",&m,&x1,&y1,&x2,&y2); if(!f) f=1; ElsePuts""); memset (ans,0,sizeof(ans)); for(i=0; i<n;i++) {scanf ("%d%d",&x3,&x4); S[i]=Point (X3,Y1); E[i]=Point (X4,y2); } S[n]=Point (X2,Y1); E[n]=Point (X2,y2); while(m--){ intL=0, r=n,mid,x,y,tmp; scanf ("%d%d",&x,&y); Point P=Point (x, y); while(l<=R) {Mid= (l+r) >>1; if(Cal (P,s[mid],e[mid]) <0) {tmp=mid; R=mid-1; } ElseL=mid+1; } Ans[tmp]++; } for(i=0; i<=n;i++) {printf ("%d:%d\n", I,ans[i]); } } return 0;}
POJ 2318 TOYS "cross product + two points"