Given points on a two dimensional graph, find a line that passes the most number of points.
Any comments please?
import static org.junit.Assert.*;
import java.util.*;
import org.junit.*;
public class Solution {
public class Point {
double x_coord;
double y_coord;
public Point(double x, double y) {
x_coord = x;
y_coord = y;
}
}
public class Line {
// define line with two points
Point p1;
Point p2;
public Line(Point a, Point b) {
p1 = a;
p2 = b;
}
}
public Line maxLine(ArrayList<Point> points) {
int cnt = 0;
int maxL = 0;
Line bestL = null;
if (points.size() == 0) {
return new Line(new Point(0, 0), new Point(1, 1));
} else if (points.size() == 1) {
return new Line(new Point(0, 0), points.get(0));
} else if (points.size() == 2) {
return new Line(points.get(0), points.get(1));
} else {
for (Point p1 : points) {
for (Point p2 : points) {
if (p1.x_coord <= p2.x_coord) {
cnt = countPoints(p1, p2, points);
if (cnt > maxL) {
if (p1.x_coord == p2.x_coord && p1.y_coord <= p2.y_coord || p1.x_coord < p2.x_coord) {
bestL = new Line(p1, p2);
} else {
bestL = new Line(p2, p1);
}
maxL = cnt;
}
// define line with left most and right most points
// in case of equality, with bottom most left
// with upper most right
else if (cnt == maxL && p2.x_coord > bestL.p2.x_coord) {
bestL = new Line(bestL.p1, p2);
} else if (cnt == maxL && p1.x_coord < bestL.p1.x_coord) {
bestL = new Line(p1, bestL.p2);
} else if (cnt == maxL && p1.x_coord == bestL.p1.x_coord
&& p1.y_coord < bestL.p1.y_coord) {
bestL = new Line(p1, bestL.p2);
} else if (cnt == maxL && p2.x_coord == bestL.p2.x_coord
&& p2.y_coord > bestL.p2.y_coord) {
bestL = new Line(bestL.p1, p2);
}
}
}
}
}
return bestL;
}
public int countPoints(Point p1, Point p2, ArrayList<Point> points) {
int cnt = 0;
if (p1.x_coord != p2.x_coord) {
double slope = (p2.y_coord - p1.y_coord) / (p2.x_coord - p1.x_coord);
for (Point p : points) {
if (p.y_coord - p1.y_coord == slope * (p.x_coord - p1.x_coord)) {
cnt++;
}
}
} else {
for (Point p : points) {
if (p.x_coord == p1.x_coord) {
cnt++;
}
}
}
return cnt;
}
@Test
public void test_1() {
ArrayList<Point> p = new ArrayList<Point>();
p.add(new Point(0, 0));
// System.out.println(maxLine(p).p1.x_coord + " " + maxLine(p).p1.y_coord + " "
// + maxLine(p).p2.x_coord + " " + maxLine(p).p2.y_coord);
assertEquals(0, maxLine(p).p1.x_coord, 0);
assertEquals(0, maxLine(p).p1.y_coord, 0);
assertEquals(0, maxLine(p).p2.x_coord, 0);
assertEquals(0, maxLine(p).p2.y_coord, 0);
}
@Test
public void test_2() {
ArrayList<Point> p = new ArrayList<Point>();
p.add(new Point(0, 0));
p.add(new Point(0, 1));
// System.out.println(maxLine(p).p1.x_coord + " " + maxLine(p).p1.y_coord + " "
// + maxLine(p).p2.x_coord + " " + maxLine(p).p2.y_coord);
assertEquals(0, maxLine(p).p1.x_coord, 0);
assertEquals(0, maxLine(p).p1.y_coord, 0);
assertEquals(0, maxLine(p).p2.x_coord, 0);
assertEquals(1, maxLine(p).p2.y_coord, 0);
}
@Test
public void test_3() {
ArrayList<Point> p = new ArrayList<Point>();
p.add(new Point(0, 0));
p.add(new Point(1, 0));
assertEquals(0, maxLine(p).p1.x_coord, 0);
assertEquals(0, maxLine(p).p1.y_coord, 0);
assertEquals(1, maxLine(p).p2.x_coord, 0);
assertEquals(0, maxLine(p).p2.y_coord, 0);
}
@Test
public void test_4() {
ArrayList<Point> p = new ArrayList<Point>();
p.add(new Point(0, 0));
p.add(new Point(2, 0));
p.add(new Point(1, 0));
// System.out.println(maxLine(p).p1.x_coord + " " + maxLine(p).p1.y_coord + " "
// + maxLine(p).p2.x_coord + " " + maxLine(p).p2.y_coord);
assertEquals(0, maxLine(p).p1.x_coord, 0);
assertEquals(0, maxLine(p).p1.y_coord, 0);
assertEquals(2, maxLine(p).p2.x_coord, 0);
assertEquals(0, maxLine(p).p2.y_coord, 0);
}
public static void main(String[] args) {
Solution e = new Solution();
ArrayList<Point> p = new ArrayList<Point>();
e.test_1();
e.test_2();
e.test_3();
e.test_4();
}
}