使用排序Java查找斜坡上的点
所以我正在做一项任务,我被卡住了。 这个是助理 基本上它包括写3个类。第一个是point.java,用于保存XY平面中某个点的信息。 作业要求你在同一条线上找到每一个由4个点组成的组合(坡度相同) 首先使用Brute.java进行比较,比较所有可能的组合,然后使用Fast.jave进行比较,从中选择一个起点并进行比较,然后不再使用该点。这样可以避免检查相同的点 我一直坚持的部分是最后一个,Fast.java。 我得到了几乎正确的输出,除了最后两行是交换的。 给定输入使用排序Java查找斜坡上的点,java,Java,所以我正在做一项任务,我被卡住了。 这个是助理 基本上它包括写3个类。第一个是point.java,用于保存XY平面中某个点的信息。 作业要求你在同一条线上找到每一个由4个点组成的组合(坡度相同) 首先使用Brute.java进行比较,比较所有可能的组合,然后使用Fast.jave进行比较,从中选择一个起点并进行比较,然后不再使用该点。这样可以避免检查相同的点 我一直坚持的部分是最后一个,Fast.java。 我得到了几乎正确的输出,除了最后两行是交换的。 给定输入 15 10000 0 800
15
10000 0
8000 2000
2000 8000
0 10000
20000 0
18000 2000
2000 18000
10000 20000
30000 0
0 30000
20000 10000
13000 0
11000 3000
5000 12000
9000 6000
在找到斜坡上的点之前,我正在对数组进行排序。
调用getLines之前的排序数组如下所示:
(10000, 0)
(8000, 2000)
(2000, 8000)
(0, 10000)
(20000, 0)
(18000, 2000)
(2000, 18000)
(10000, 20000)
(30000, 0)
(0, 30000)
(20000, 10000)
(13000, 0)
(11000, 3000)
(5000, 12000)
(9000, 6000)
以下是我的重点课程:
public class Point implements Comparable<Point> {
public final int x, y;
// compare points by slope
public final Comparator<Point> SLOPE_ORDER = new Comparator<Point>()
{
@Override
public int compare(Point arg0, Point arg1) {
// TODO Auto-generated method stub
if(slopeTo(arg0) < slopeTo(arg1)){
return -1;
}
if(slopeTo(arg0) > slopeTo(arg1)){
return 1;
}
else{
return 0;
}
}
};
// create the point (x, y)
public Point(int x, int y) {
this.x = x;
this.y = y;
}
// plot this point to standard drawing
public void draw() {
/* DO NOT MODIFY */
StdDraw.point(x, y);
}
// draw line between this point and that point to standard drawing
public void drawTo(Point that) {
/* DO NOT MODIFY */
StdDraw.line(this.x, this.y, that.x, that.y);
}
// slope between this point and that point
public double slopeTo(Point that) {
// TODO: Implement this
if(this.x == that.x){
return Double.NEGATIVE_INFINITY;
}
else if(that.y - this.y == 0){
return 0;
}
else if(this.x == that.x){
return Double.POSITIVE_INFINITY;
}
else{
return (((double)that.y - (double)this.y)/((double)that.x - (double)this.x));
}
}
/**
* Is this point lexicographically smaller than that one? comparing
* y-coordinates and breaking ties by x-coordinates
*/
public int compareTo(Point that) {
// TODO: Implement this
if(this.y < that.y){
return -1;
}
else if(this.y > that.y){
return 1;
}
else{
if(this.x < that.x){
return -1;
}
else if(this.x > that.x){
return 1;
}
else{
return 0;
}
}
}
而正确的输出是
(10000, 0) -> (8000, 2000) -> (2000, 8000) -> (0, 10000)
(10000, 0) -> (13000, 0) -> (20000, 0) -> (30000, 0)
(13000, 0) -> (11000, 3000) -> (9000, 6000) -> (5000, 12000)
(30000, 0) -> (20000, 10000) -> (10000, 20000) -> (0, 30000)
有人知道我哪里出了问题吗?
谢谢。输出将遵循
PointScope
数组中的顺序。Arrays.sort
的排序方式会导致输出。您可以重新排列PointScope
数组中的元素,或更改数组排序逻辑以获得所需的输出。如果您让它在运行getLines
之前打印出排序后的点数组,并向我们展示其外观,可能会有所帮助。同时发布你的点
对象的代码,这样我们就可以看到你的自定义比较器了。最后,试着用你自己的话来总结作业,这样如果有人在链接断开时出现,他们仍然可以理解问题。谢谢,我根据你的建议更改了问题:)酷。还有一件事,您确定点
类中的代码是正确的吗?它不应该有一个比较器来方法吗?哎呀,我的错。复制了错误的代码。应该是现在,您可以尝试将compare
方法更改为简单地返回slopeto(arg0)-slopeto(arg1)
?请让我知道发生了什么。我真的不知道该怎么做。既然Point是一个对象,我可以在Arrays.sort函数中使用SLOPE_ORDER以外的东西作为比较器吗?
public static void main(String[] args) {
In in = new In();
int N = in.readInt();
Point[] points = new Point[N];
for(int i=0; i< N; i++) {
int x = in.readInt();
int y = in.readInt();
points[i] = new Point(x,y);
}
in.close();
Point [] pointsCopy = Arrays.copyOf(points, points.length);
for (Point originPoint : points) {
Arrays.sort(pointsCopy, originPoint.SLOPE_ORDER);
getLines(pointsCopy);
}
}
(10000, 0) -> (8000, 2000) -> (2000, 8000) -> (0, 10000)
(10000, 0) -> (13000, 0) -> (20000, 0) -> (30000, 0)
(30000, 0) -> (20000, 10000) -> (10000, 20000) -> (0, 30000)
(13000, 0) -> (11000, 3000) -> (9000, 6000) -> (5000, 12000)
(10000, 0) -> (8000, 2000) -> (2000, 8000) -> (0, 10000)
(10000, 0) -> (13000, 0) -> (20000, 0) -> (30000, 0)
(13000, 0) -> (11000, 3000) -> (9000, 6000) -> (5000, 12000)
(30000, 0) -> (20000, 10000) -> (10000, 20000) -> (0, 30000)