计算几何【3】 判断相交+poj2563+极角排序+poj 2007

来源:互联网 发布:java解压war包命令 编辑:程序博客网 时间:2024/06/16 08:44

判断相交的模板:

Line l[maxn];bool inter(Line l1,Line l2){    return        max(l1.s.x,l1.e.x) >= min(l2.s.x,l2.e.x) &&        max(l2.s.x,l2.e.x) >= min(l1.s.x,l1.e.x) &&        max(l1.s.y,l1.e.y) >= min(l2.s.y,l2.e.y) &&        max(l2.s.y,l2.e.y) >= min(l1.s.y,l1.e.y) &&        sgn((l2.s-l1.e)^(l1.s-l1.e))*sgn((l2.e-l1.e)^(l1.s-l1.e)) <= 0 &&        sgn((l1.s-l2.e)^(l2.s-l2.e))*sgn((l1.e-l2.e)^(l2.s-l2.e)) <= 0;}

poj 2653
Pick-up sticks
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 14082 Accepted: 5342
Description

Stan has n sticks of various length. He throws them one at a time on the floor in a random way. After finishing throwing, Stan tries to find the top sticks, that is these sticks such that there is no stick on top of them. Stan has noticed that the last thrown stick is always on top but he wants to know all the sticks that are on top. Stan sticks are very, very thin such that their thickness can be neglected.
Input

Input consists of a number of cases. The data for each case start with 1 <= n <= 100000, the number of sticks for this case. The following n lines contain four numbers each, these numbers are the planar coordinates of the endpoints of one stick. The sticks are listed in the order in which Stan has thrown them. You may assume that there are no more than 1000 top sticks. The input is ended by the case with n=0. This case should not be processed.
Output

For each input case, print one line of output listing the top sticks in the format given in the sample. The top sticks should be listed in order in which they were thrown.

The picture to the right below illustrates the first case from input.
Sample Input

5
1 1 4 2
2 3 3 1
1 -2.0 8 4
1 4 8 2
3 3 6 -2.0
3
0 0 1 1
1 0 2 1
2 0 3 1
0
Sample Output

Top sticks: 2, 4, 5.
Top sticks: 1, 2, 3.
Hint

Huge input,scanf is recommended.
Source

Waterloo local 2005.09.17

代码;

#include<cstdio>#include<cmath>#include<algorithm>using namespace std;const int maxn=100000+10;const double eps=1e-8;double sgn(double x){ if(fabs(x) < eps)return 0;//控制精度    if(x < 0)return -1;    else return 1;}struct point{    double x,y;    point() {}    point(double _x,double _y)    {        x = _x;        y = _y;    }    point operator -(const point &b)const    {        return point(x - b.x,y - b.y);    }//叉积    double operator ^(const point &b)const    {        return x*b.y - y*b.x;    }//点积    double operator *(const point &b)const    {        return x*b.x + y*b.y;    }//绕原点旋转角度B(弧度值),后x,y的变化    void transXY(double B)    {        double tx = x,ty = y;        x = tx*cos(B) - ty*sin(B);        y = tx*sin(B) + ty*cos(B);    }};point p[maxn];double dist(point p0,point p1){    return (double)sqrt((p1.x-p0.x)*(p1.x-p0.x)+(p1.y-p0.y)*(p1.y-p0.y));}bool _cmp(point p1,point p2){    double tmp = (p1-p[0])^(p2-p[0]);    if(sgn(tmp) > 0)return true;  else if(sgn(tmp) == 0 && sgn(dist(p1,p[0]) - dist(p2,p[0])) <= 0)//控制精度       return true;    else return false;}int main(){    int num=0,x,y;    while(~scanf("%lf%lf",&p[num].x,&p[num].y))num++;    sort(p+1,p+num,_cmp);    for(int i=0;i<num;i++)    {        printf("(%.0f,%.0f)\n",p[i].x,p[i].y);    }    return 0;}

【2】极角排序
极角排序:

首先我们先选择点集
最下方的点中最靠左的点。

以该点为原点建立坐标系。
保证所有的点都在Ⅰ、Ⅱ象限或X轴上。

然后以原点为起点
其他各点为终点做出多条向量。
根据之前学习的关于向量叉积的性质可以知道
一个向量在另一个向量的顺时针方向
那么他们的叉积大于零

bool Cmp(Point a,Point b){
return Cross(a-O,b-O)>=0;
}

这里写图片描述

例题:poj 2007

Scrambled Polygon
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 9487 Accepted: 4497
Description

A closed polygon is a figure bounded by a finite number of line segments. The intersections of the bounding line segments are called the vertices of the polygon. When one starts at any vertex of a closed polygon and traverses each bounding line segment exactly once, one comes back to the starting vertex.
这里写图片描述

A closed polygon is called convex if the line segment joining any two points of the polygon lies in the polygon. Figure 1 shows a closed polygon which is convex and one which is not convex. (Informally, a closed polygon is convex if its border doesn’t have any “dents”.)

The subject of this problem is a closed convex polygon in the coordinate plane, one of whose vertices is the origin (x = 0, y = 0). Figure 2 shows an example. Such a polygon will have two properties significant for this problem.

The first property is that the vertices of the polygon will be confined to three or fewer of the four quadrants of the coordinate plane. In the example shown in Figure 2, none of the vertices are in the second quadrant (where x < 0, y > 0).

To describe the second property, suppose you “take a trip” around the polygon: start at (0, 0), visit all other vertices exactly once, and arrive at (0, 0). As you visit each vertex (other than (0, 0)), draw the diagonal that connects the current vertex with (0, 0), and calculate the slope of this diagonal. Then, within each quadrant, the slopes of these diagonals will form a decreasing or increasing sequence of numbers, i.e., they will be sorted. Figure 3 illustrates this point.
这里写图片描述
这里写图片描述
Input

The input lists the vertices of a closed convex polygon in the plane. The number of lines in the input will be at least three but no more than 50. Each line contains the x and y coordinates of one vertex. Each x and y coordinate is an integer in the range -999..999. The vertex on the first line of the input file will be the origin, i.e., x = 0 and y = 0. Otherwise, the vertices may be in a scrambled order. Except for the origin, no vertex will be on the x-axis or the y-axis. No three vertices are colinear.
Output

The output lists the vertices of the given polygon, one vertex per line. Each vertex from the input appears exactly once in the output. The origin (0,0) is the vertex on the first line of the output. The order of vertices in the output will determine a trip taken along the polygon’s border, in the counterclockwise direction. The output format for each vertex is (x,y) as shown below.
Sample Input

0 0
70 -50
60 30
-30 -50
80 20
50 -60
90 -20
-30 -40
-10 -60
90 10
Sample Output

(0,0)
(-30,-40)
(-30,-50)
(-10,-60)
(50,-60)
(70,-50)
(90,-20)
(90,10)
(80,20)
(60,30)
Source

Rocky Mountain 2004

代码:

#include<cstdio>#include<cmath>#include<algorithm>using namespace std;const int maxn=100000+10;const double eps=1e-8;double sgn(double x){    if(fabs(x) < eps)return 0;    if(x < 0)return -1;    else return 1;}struct point{    double x,y;    point() {}    point(double _x,double _y)    {        x = _x;        y = _y;    }    point operator -(const point &b)const    {        return point(x - b.x,y - b.y);    }//叉积    double operator ^(const point &b)const    {        return x*b.y - y*b.x;    }//点积    double operator *(const point &b)const    {        return x*b.x + y*b.y;    }//绕原点旋转角度B(弧度值),后x,y的变化    void transXY(double B)    {        double tx = x,ty = y;        x = tx*cos(B) - ty*sin(B);        y = tx*sin(B) + ty*cos(B);    }};struct Line{    point s,e;    Line() {}    Line(point _s,point _e)    {        s = _s;        e = _e;    }//两直线相交求交点//第一个值为0表示直线重合,为1表示平行,为0表示相交,为2是相交//只有第一个值为2时,交点才有意义//    pair<int,point> operator &(const Line &b)const//    {//        point res = s;//        if(sgn((s-e)^(b.s-b.e)) == 0)//        {//            if(sgn((s-b.e)^(b.s-b.e)) == 0)//                return make_pair(0,res);//重合//            else return make_pair(1,res);//平行//        }//        double t = ((s-b.s)^(b.s-b.e))/((s-e)^(b.s-b.e));//        res.x += (e.x-s.x)*t;//        res.y += (e.y-s.y)*t;//        return make_pair(2,res);//    }};Line l[maxn];bool inter(Line l1,Line l2){    return        max(l1.s.x,l1.e.x) >= min(l2.s.x,l2.e.x) &&        max(l2.s.x,l2.e.x) >= min(l1.s.x,l1.e.x) &&        max(l1.s.y,l1.e.y) >= min(l2.s.y,l2.e.y) &&        max(l2.s.y,l2.e.y) >= min(l1.s.y,l1.e.y) &&        sgn((l2.s-l1.e)^(l1.s-l1.e))*sgn((l2.e-l1.e)^(l1.s-l1.e)) <= 0 &&        sgn((l1.s-l2.e)^(l2.s-l2.e))*sgn((l1.e-l2.e)^(l2.s-l2.e)) <= 0;}int main(){    int n;    while(~scanf("%d",&n)&&n)    {        point a,b;        for(int i=1;i<=n;i++)        {            scanf("%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y);            l[i]=Line(a,b);        }        bool first=true;        printf("Top sticks: ");        for(int i=1;i<=n;i++)        {            bool ok=true;            for(int j=i+1;j<=n&&ok;j++)            {                if(inter(l[i],l[j])) ok=false;            }            if(ok)            {                if(!first) printf(", ");                first=false;                printf("%d",i);            }        }        printf(".\n");    }    return 0;}

这里主要用到模板,模板总结会传到博客上吧。。。

原创粉丝点击