MCPcopy Create free account
hub / github.com/careercup/ctci / findBestLine

Method findBestLine

java/Chapter 7/Question7_6/Question.java:49–71  ·  view source on GitHub ↗
(GraphPoint[] points)

Source from the content-addressed store, hash-verified

47
48
49 public static Line findBestLine(GraphPoint[] points) {
50 Line bestLine = null;
51 int bestCount = 0;
52 HashMap<Double, ArrayList<Line>> linesBySlope = new HashMap<Double, ArrayList<Line>>();
53
54 for (int i = 0; i < points.length; i++) {
55 for (int j = i + 1; j < points.length; j++) {
56 Line line = new Line(points[i], points[j]);
57 insertLine(linesBySlope, line);
58
59 /* count lines that are equivalent to current line */
60 int count = countEquivalentLines(linesBySlope, line);
61
62 /* if better than current line, replace it */
63 if (count > bestCount) {
64 bestLine = line;
65 bestCount = count;
66 }
67 }
68 }
69
70 return bestLine;
71 }
72
73 public static GraphPoint[] createPoints() {
74 int n_points = 100;

Callers 1

mainMethod · 0.95

Calls 2

insertLineMethod · 0.95
countEquivalentLinesMethod · 0.95

Tested by

no test coverage detected