Detect colinearity
Given n distinct lattice points (i.e., coordinates are integers) on the xy plane, determine if there are three points that lie on a straight line. There are at least 3 points in all test cases.
Input:
The first line is the number of points n. This number is guaranteed to be at least 3 in all test cases.
The second line is the dimension of the points, which is fixed to be 2 in this problem.
The following n lines consists of two integers, separated by space.
Output:
If there are three points lying on the same line, return 1. Else return 0
Example 1:
Input:
3
2
-1 -1
0 0
1 1
Output:
1
Example 2:
Input:
3
2
-1 -1
0 0
1 2
Output:
0
def collinear(x1, y1, x2, y2, x3, y3):
'''
Calculation the area of triangle. We have skipped multiplication with 0.5 to
avoid floating point computations
'''
a = x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)
if (a == 0):
print(1)
else:
print(0)
# Driver Code
x1, x2, x3, y1, y2, y3 = 1, 1, 1, 1, 4, 5
collinear(x1, y1, x2, y2, x3, y3)