扫掠线中点的实现

在扫掠线算法中,我使用了一个数组,该数组按其x坐标对点进行排序,并使用一个TreeSet按其y坐标对点进行排序。目前,我使用两个点类来指示要使用的比较器(即比较x坐标还是y坐标)。一个点类但两个compareTo函数是否可行?为了说明这一点,这就是我所拥有的。对于非专业术语表示歉意。

   public static class Point implements Comparable<Point>{
    int x; int y;
    public Point(int a,int b){
        this.x=a; this.y=b;
    }
    public int compareTo(Point other){
        if(this.x<other.x)return -1;
        if(this.x>other.x)return 1;
        if(this.y>other.y)return 1;
        return -1;
    }
}
public static class Point2 implements Comparable<Point2>{
    int x; int y;
    public Point2(int a,int b){
        this.x=a; this.y=b;
    }
    public int compareTo(Point2 other){
        if(this.y<other.y)return -1;
        if(this.y>other.y)return 1;
        if(this.x>other.x)return 1;
        return -1;
    }
}
yc1992217 回答:扫掠线中点的实现

数组和TreeSet都允许基于自定义Comparator进行排序:

new TreeSet<>(new Comparator<T>() {
        @Override
        public int compare(T t1,T t2) {
            // your implementation here
        }
    });

Arrays.sort(arr,new Comparator<T>() {
        @Override
        public int compare(T t1,T t2) {
            // your implementation here
        }
    });
本文链接:https://www.f2er.com/2720329.html

大家都在问