Line is defined by both slope and the constant : y = Mx + C, but the solutions seem to only consider slope


#1

For example consider the following points : (0,0) (1,1) (0,1) (1,2) (1,0) (2,1)

There are three parallel lines in the above set, all with a slope = 1, but (0,0) and (0,1) lie on different lines.

Most of the solutions I saw have this code
slope = (double)(yj - yi) / (double)(xj - xi);

                if(map.containsKey(slope))
                    map.put(slope, map.get(slope) + 1);
                else 
                    map.put(slope, 1);

How is this being checked?


#2

In the solution, they from each point draw lines to all other points. And by doing so they nullify C.

int xdiff = X[i] - X[j];
int ydiff = Y[i] - Y[j];

In other words, if you only consider lines which are drawn from the same point, you need to take into account the only scope. That is why you need to do such operation for each point. It is O(N*N) solution.


#3

" if you only consider lines which are drawn from the same point"
But why should I consider that? What if I have the next six points: (4, 5) (5, 6) (4, 6) (5, 7) (4, 7) (5, 8)?
I will encounter the same slope 3 times, don’t I?


#4

If two lines pass through same point and have same slope, it means the two lines are identical.


#5

Makes sense… I tried hashing the slope and intercept for every line(considered straight and horizontal lines as well) and incremented the value for the (slope,intercept) pair if the point fell on that line(using y=mx+c)
But some test cases don’t pass…why wont this work? :confused:
eg. [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]