直線の式の一般化

2022/09/04

Python 3.8.2

2点を通る直線の式(ax+by+c=0)を求める関数

def line_generalization(p0,p1): #p = (x,y)
    if p0[0] == p1[0]:
        a = 1.0
        b = 0.0
        c = float(-p0[0])
    elif p0[1] == p1[1]:
        a = 0.0
        b = 1.0
        c = float(-p0[1])
    else:
        slope = float(p1[1]-p0[1])/(p1[0]-p0[0])
        intercept = p0[1] - slope*p0[0]
        a = slope
        b = -1.0
        c = intercept
    return a,b,c