+ 2

Colinear pointa

have array of points and i have to check if there is any colinear three points. I tried with 3 for loops but it throws an null pointer exception. Here is the code For(i=0;i<length-2;i++) For(j=i+1;j<length-1;j++) For(z=j+1;z<length;z++) { //declaring y1, y2... If((y1-y2) *(x1-x3) ==(y1-y3) *(x1-x2)) { //do something } } Any help will be appreciated, ty

14th Mar 2018, 9:13 PM
Milan Srdić
Milan Srdić - avatar
1 Answer
+ 13
to find whether 3 points are collinear or not , then just find slope of line forming by them ... [U need only 2 pts to find slope] ... if slope equates then are collinear else not //no need of loops ... when we can make simple 1-liner 👉(x2-x1)/(y2-y1)==(x3-x2)/(y3-y2)
14th Mar 2018, 9:41 PM
Gaurav Agrawal
Gaurav Agrawal - avatar