所以我正在完成一项任务,而且我很困难。 这个问题是http://www.cs.princeton.edu/courses/archive/fall05/cos226/assignments/lines.html 基本上它涉及写3个班级。第一个,point.java用于保存关于XY平面中的点的信息。 然后分配要求您在同一条线上找到4个点的每个组合(具有相同的斜率) 首先使用Brute.java进行比较,然后比较每个可能的组合,然后使用Fast.jave进行比较,在那里选择起点并从那里进行比较,然后再不使用该点。这样你就可以避免检查相同的点。
我坚持的部分是最后一部分,Fast.java。 我得到几乎正确的输出,除了最后两行被交换的事实。 鉴于输入
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)
这是我的Point类:
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;
}
}
}
这是我的fast.java类:
private static void getLines(Point[] points) {
Point p = points[0];
Point[] lines = new Point[points.length];
lines[0] = p;
int pointsOnLine = 0;
double lastSlope = p.slopeTo(points[1]);
for(int i = 1; i < points.length; i++) {
Point newPoint = points[i];
double slope = p.slopeTo(newPoint);
if(slope == lastSlope) {
pointsOnLine++;
lines[pointsOnLine] = newPoint;
}
else {
if(pointsOnLine >= 3) {
printLine(lines, pointsOnLine + 1);
}
pointsOnLine = 1;
lines[1] = newPoint;
}
lastSlope = slope;
}
if(pointsOnLine >= 3) {
printLine(lines, pointsOnLine + 1);
}
}
private static void printLine(Point[] lines, int size) {
Arrays.sort(lines, 1, size);
if(lines[0].compareTo(lines[1]) < 0) {
StdOut.print(lines[0]);
for(int i = 1; i < size; i++) {
StdOut.print(" -> " + lines[i]);
}
StdOut.println();
}
}
我的主要功能
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)
任何可以看到我出错的地方的人? 感谢。
答案 0 :(得分:0)
输出将遵循pointsCopy
数组中的顺序。 Arrays.sort
正在以导致输出的方式排序。您可以重新排列pointsCopy
数组中的元素或更改数组排序逻辑以获得所需的输出。