【问题标题】:Implementation of points in sweep line扫描线中点的实现
【发布时间】:2020-05-10 11:22:07
【问题描述】:

在扫描线算法中,我使用了一个按 x 坐标对点进行排序的数组,以及一个按 y 坐标对点进行排序的 TreeSet。目前,我使用两个点类来指示要使用哪个比较器(即是比较 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;
    }
}

【问题讨论】:

  • 注意:我认为使用树形图不一定能解决问题。
  • 您可以创建一个自定义的Comparator,然后将其传递给TreeSet构造函数,以便它可以根据指定的比较器进行排序。见docs.oracle.com/javase/8/docs/api/java/util/…

标签: class data-structures computational-geometry implementation comparable


【解决方案1】:

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

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
        }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-22
    • 1970-01-01
    • 2017-06-04
    • 1970-01-01
    • 1970-01-01
    • 2011-01-15
    • 2013-05-01
    相关资源
    最近更新 更多