在扫掠线算法中,我使用了一个数组,该数组按其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;
}
}