算法测试系列:Detect colinearity

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) 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值