given number of points(cordinates) , find max points lie on the same line ?

• 08-23-2008, 07:30 PM
Hayzam
given number of points(cordinates) , find max points lie on the same line ?
error while given the test case , it finds 1 point while there are only 2 lines , each one has 2 points
Quote:

x [ 0 ] = 1 , y [ 0 ] = 5
x [ 1 ] = 5 , y [ 1 ] = 1
x [ 2 ] = 1 , y [ 2 ] = 1
x [ 3 ] = 5 , y [ 3 ] = 5
Code:

private static void solve_it(ArrayList<point> arr) {
System.out.println(" new test case ");
point p = new point();
point pp = new point();
point ppp = new point();
int slope, slope2, x, y, count = 0;

int[][] index = new int[arr.size()][arr.size()];

for (int i = 0; i < arr.size(); i++) {
count = 0;
p = arr.get(i);
System.out.println(" x [ " + i + " ] = " + p.x + " , y [ " + i + " ] = " + p.y);
for (int j = 0; j < arr.size(); j++) {

if (i == j) {
break;
}
pp = arr.get(j);

if ((pp.y - p.y) == 0) {
slope = 0;
} else {
slope = (pp.x - p.x) / (pp.y - p.y);
}

for (int l = 0; l < arr.size(); l++) {
if ((l == i) || (l == j)) {
break;
}
ppp = arr.get(l);

if ((ppp.y - p.y) == 0) {
slope2 = 0;
} else {
slope2 = (ppp.x - p.x) / (ppp.y - p.y);
}
if (slope == slope2) {
count++;
}
}
index[i][j] = count;

}

}

for (int q = 0; q < index.length; q++) {
System.out.println();
for (int w = 0; w < index.length; w++) {
System.out.print(" " + index[q][w]);
}

}

arr.clear();

}

• 08-23-2008, 09:07 PM
Norm
If you are getting errors, copy and paste full text here.