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

1. Member Join Date
May 2008
Posts
9
Rep Power
0

## 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
x [ 0 ] = 1 , y [ 0 ] = 5
x [ 1 ] = 5 , y [ 1 ] = 1
x [ 2 ] = 1 , y [ 2 ] = 1
x [ 3 ] = 5 , y [ 3 ] = 5
Java 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();

}```  Reply With Quote

2. ## If you are getting errors, copy and paste full text here.
I see code but NO COMMENTs on how you are trying to solve the problem.  Reply With Quote

3. ## Two points define a line, you may accidentally have more than two points on a line, depending on the accuracy of your measurements.

But I don't see a question here.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•