POJ2007 Scrambled Polygon

一.原题链接:

http://poj.org/problem?id=2007

二.题目大意:

给一个组成凸包的多边形,有一个顶点在 (0,0) ,从 (0,0) 开始,逆时针输出凸包的点。

三.解题思路:

其实就是输出凸包上的点,先找出 y 值最小,y值相等 x <script type="math/tex" id="MathJax-Element-5">x</script>值最小的点作为基点,然后进行一次极角排序,排完有了凸包的顺序,取个模输出就行了。
极角排序方法参考POJ1113的解题报告。

四.代码:
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;


class Main {
    static final int MAX_SIZE = 1050;
    static final double INF = 1e40;
    static final double ESP = 1e-4;

    public static void reDirectionInput(){
        BufferedInputStream in = null;
        try {
            in = new BufferedInputStream(new FileInputStream("in.txt"));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  
        System.setIn(in);  

    }

    static int vertexNum;
    static List<Point> points = new ArrayList<Point>();
    static Point base;

    static int cross(Vector v1, Vector v2){
        return v1.x*v2.y - v2.x*v1.y;
    }

    static double pointDist(Point p1, Point p2){
        return Math.sqrt((p1.x-p2.x)*(p1.x-p2.x) +
                (p1.y-p2.y)*(p1.y-p2.y));
    }

    static void sortPoints(){
        Collections.sort(points, new Comparator(){
            @Override
            public int compare(Object arg0, Object arg1) {
                Point p1 = (Point)arg0;
                Point p2 = (Point)arg1;

                Vector v1 = new Vector();
                Vector v2 = new Vector();
                v1.setXY(base, p1);
                v2.setXY(p1, p2);
                int res = cross(v1, v2); 
                if(res > 0){
                    return -1;
                }else if(res == 0){
                    return (pointDist(p1, base) > 
                    pointDist(p2, base)? 1:-1);
                }else{
                    return 1;
                }

            }

        });
    }



    public static void main (String args[]){

//reDirectionInput();

        Scanner in = new Scanner(System.in);
        int x, y, idx = 0;
        vertexNum = 0;
        while(in.hasNext()){
            x = in.nextInt();
            y = in.nextInt();

            Point p = new Point();
            p.setXY(x, y);
            points.add(p);
            if(points.get(idx).y > y || 
                    points.get(idx).y == points.get(idx).y && 
                    points.get(idx).x > x ){
                idx = vertexNum;
            }
            vertexNum++;
        }

        base = points.get(idx);
        sortPoints();

        for(idx = 0; idx < vertexNum; idx++){
            if(points.get(idx).x == 0 && points.get(idx).y == 0){
                break;
            }
        }

        System.out.printf("(%d,%d)\n", points.get(idx).x, points.get(idx).y);
        int saveIdx = idx;
        for(idx = (idx+1)%vertexNum; idx != saveIdx; idx = (idx+1)%vertexNum){
            System.out.printf("(%d,%d)\n", points.get(idx).x, points.get(idx).y);
        }
        in.close();
    }
}

class Point{
    public void setXY(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int x, y;
}

class Vector{
    public void setXY(Point begin, Point end) {
        this.x = end.x-begin.x;
        this.y = end.y-begin.y;
    }

    public Vector reVector() {
        Vector v = new Vector();
        v.x = -x;
        v.y = -y;
        return v;
    }
    public int x, y;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值