Method to return the equation of a straight line given two points

后端 未结 5 972
清歌不尽
清歌不尽 2020-12-29 06:17

I have a class Point, consisting of a point with x and y coordinates, and I have to write a method that computes and returns the equation of a straight line joi

5条回答
  •  囚心锁ツ
    2020-12-29 06:43

    from numpy import ones,vstack
    from numpy.linalg import lstsq
    points = [(1,5),(3,4)]
    x_coords, y_coords = zip(*points)
    A = vstack([x_coords,ones(len(x_coords))]).T
    m, c = lstsq(A, y_coords)[0]
    print("Line Solution is y = {m}x + {c}".format(m=m,c=c))
    

    but really your method should be fine ...

提交回复
热议问题