【发布时间】: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