# Algorithm I assignment Collinear

1. 程序要求解耦
2. 循序渐进，这周的作业不允许使用hashcode()
3. 输入变量不允许被程序改变

 1 public class BruteCollinearPoints {
2     // finds all line segments containing 4 points
3     private int N;
4     private ArrayList<LineSegment> segment = new ArrayList<LineSegment>();
5
6     public BruteCollinearPoints(Point[] points) {
7         if (points == null)
8             throw new java.lang.NullPointerException();
9         N = 0;
10         Point[] copy = new Point[points.length];
11         for (int i = 0; i < points.length; i++) {
12             copy[i] = points[i];
13         }
14         Arrays.sort(copy);
15         for (int i = 0; i < copy.length - 1; i++) {
16             if (copy[i].compareTo(copy[i + 1]) == 0) {
17                 throw new java.lang.IllegalArgumentException();
18             }
19         }
20         for (int i = 0; i < copy.length - 3; i++) {
21             for (int j = i + 1; j < copy.length - 2; j++) {
22                 double slope1 = copy[i].slopeTo(copy[j]);
23                 for (int k = j + 1; k < copy.length - 1; k++) {
24                     double slope2 = copy[i].slopeTo(copy[k]);
25                     if (slope1 != slope2)
26                         continue;
27                     int temp = 0;
28                     for (int l = k + 1; l < copy.length; l++) {
29                         double slope3 = copy[i].slopeTo(copy[l]);
30                         if (slope1 == slope3)
31                             temp = l;
32                         if ((l == copy.length - 1) && (temp != 0)) {
33                             N++;
35                         }
36                     }
37                 }
38             }
39         }
40     }
41
42     // the number of line segments
43     public int numberOfSegments() {
44         return N;
45     }
46
47     // the line segments
48     public LineSegment[] segments() {
49         LineSegment[] results = new LineSegment[N];
50         for (int i = 0; i < N; i++) {
51             results[i] = segment.get(i);
52         }
53         return results;
54     }
55
56     public static void main(String[] args) {
57
58         // read the N points from a file
59         // In in = new In(args[0]);
60         In in = new In("./collinear/rs1423.txt");
62         System.out.println(N);
63         Point[] points = new Point[N];
64         for (int i = 0; i < N; i++) {
67             System.out.println("x:" + x + " y:" + y);
68             points[i] = new Point(x, y);
69         }
70
71         // draw the points
72         StdDraw.show(0);
73         StdDraw.setXscale(0, 32768);
74         StdDraw.setYscale(0, 32768);
75         for (Point p : points) {
76             p.draw();
77         }
78         StdDraw.show();
79
80         // print and draw the line segments
81         BruteCollinearPoints collinear = new BruteCollinearPoints(points);
82         for (LineSegment segment : collinear.segments()) {
83             StdOut.println(segment);
84             segment.draw();
85         }
86     }
87 }
BruteCollinearPoints

• Think of p as the origin.
• For each other point q, determine the slope it makes with p.
• Sort the points according to the slopes they makes with p.
• Check if any 3 (or more) adjacent points in the sorted order have equal slopes with respect to p. If so, these points, together with p, are collinear.

1. 对所有点排序，越靠近左下角的点越小
2. 遍历每一个点，遍历点P过程中，先将其他点根据与P的斜率进行排序
3. 对排序后的其他点进行遍历，若有斜率相同超过4个以上的点，即是要找的线段

package collinear;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;

import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdDraw;
import edu.princeton.cs.algs4.StdOut;

public class FastCollinearPoints {
private int N;
private ArrayList<LineSegment> segment = new ArrayList<LineSegment>();
private Point[] points;

public FastCollinearPoints(Point[] points) {
if (points == null)
throw new java.lang.NullPointerException();
this.points = new Point[points.length];
for (int i = 0; i < points.length; i++) {
this.points[i] = points[i];
}
N = 0;
Arrays.sort(this.points);
// remove repeat points
for (int i = 0; i < this.points.length - 1; i++) {
if (this.points[i].compareTo(this.points[i + 1]) == 0) {
throw new java.lang.IllegalArgumentException();
}
}
Point[] temp = new Point[this.points.length];
Point[] copy = new Point[this.points.length];
ArrayList<ArrayList<Point>> end_sets = new ArrayList<>();
// use a copy to sort
for (int i = 0; i < this.points.length; i++) {
temp[i] = copy[i] = this.points[i];
}

Arrays.sort(copy);

// to sort by slope
for (int i = 0; i < copy.length - 1; i++) {
Arrays.sort(temp, copy[i].slopeOrder());
// find same slope copy then save them as segment in segment
// ArrayList
int count = 1;
double slope0 = copy[i].slopeTo(temp[0]);
ArrayList<Point> set = new ArrayList<>();
for (int j = 0; j < temp.length; j++) {

// record max point
double slope1 = copy[i].slopeTo(temp[j]);
if (slope1 == slope0) {
count++;
if (count > 2 && j == temp.length - 1) {
Collections.sort(set);
// System.out.println(set);
if (set.get(0).compareTo(copy[i]) < 0) {

} else {
// no key, no set
N++;
}
count = 1;
}
} else {
if (count > 2) {
Collections.sort(set);
// System.out.println(set);

if (set.get(0).compareTo(copy[i]) < 0) {

} else {
// no key, no set
N++;
}
}
set = new ArrayList<>();
count = 1;
}
slope0 = slope1;
}
}
}

// the number of line segments
public int numberOfSegments() {
return N;
}

// the line segments
public LineSegment[] segments() {
LineSegment[] results = new LineSegment[N];
for (int i = 0; i < N; i++) {
results[i] = segment.get(i);
}
return results;
}

public static void main(String[] args) {

// read the N points from a file
// In in = new In(args[0]);
In in = new In("./collinear/rs1423.txt");
// System.out.println(N);
Point[] points = new Point[N];
for (int i = 0; i < N; i++) {
// System.out.println("x:" + x + " y:" + y);
points[i] = new Point(x, y);
}

// draw the points
StdDraw.show(0);
StdDraw.setXscale(0, 32768);
StdDraw.setYscale(0, 32768);
for (Point p : points) {
p.draw();
}
StdDraw.show();

// // print and draw the line segments
FastCollinearPoints collinear = new FastCollinearPoints(points);
for (LineSegment segment : collinear.segments()) {
StdOut.println(segment);
segment.draw();
}
}
}
FastCollinearPoints

posted @ 2015-10-05 23:28  一路绝尘  阅读(554)  评论(0编辑  收藏  举报