python 穿点最多的直线

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/lyc0424/article/details/102664118

题目描述

在二维平面上,有一些点,请找出经过点数最多的那条线。

给定一个点集vector<point>p和点集的大小n,没有两个点的横坐标相等的情况,请返回一个vector<double>,代表经过点数最多的那条直线的斜率和截距。</double></point>(这里的截距应该是x=0时,y的值,即纵截距,截距可正可负)

# -*- coding:utf-8 -*-

# class Point:
#     def __init__(self, a=0, b=0):
#         self.x = a
#         self.y = b
import collections
class DenseLine:
    def getLine(self, p, n):
        result=[]
        for i in range(n-1) :
            k=(p[i+1].y-p[i].y)/(p[i+1].x-p[i].x)
            b=p[i].y-k*p[i].x#y=kx+b
            result.append((k,b))
        count=collections.Counter(result)
        return count.most_common(1)[0][0]

关于Python collections.Counter()用法可以参考下面这篇博客:

https://blog.csdn.net/qwe1257/article/details/83272340

猜你喜欢

转载自blog.csdn.net/lyc0424/article/details/102664118