python 已知四点,两条线,求交点

import numpy as np
import pandas as pd
def get_crosss_point(A,B,C,D):
    k1,k2,b1,b2 = np.nan,np.nan,0,0
    xa,xb,xc,xd = A[0],B[0],C[0],D[0]
    ya, yb, yc, yd = A[1], B[1], C[1], D[1]

    if xb!=xa:
        k1 = (yb-ya)/(xb-xa)
    if xc!=xd:
        k2 = (yd-yc)/(xd-xc)
    if k1==k2:
        print('平行无交点')
        return None
    if k1!=np.nan:
        b1 = (xb*ya-xa*yb)/(xb-xa)
    if k2!=np.nan:
        b2 = (xd*yc-xc*yd)/(xd-xc)

    if k1==np.nan:
        cpx = xa
        cpy = np.float(k2*cpx+b2)
    elif k2== np.nan:
        cpx = xc
        cpy = np.float(k1*cpx+b1)
    elif k1==0:
        cpx = np.float((ya-b2)/k2)
        cpy = ya
    elif k2==0:
        cpx = np.float((yc-b1)/k1)
        cpy = yc
    else:
        cpx = np.float((b2-b1)/(k1-k2))
        cpy = np.float((k2*b1-k1*b2)/(k2-k1))
    return [cpx,cpy]




if __name__ == '__main__':
    #求四点确定的两条线段的交点,p2,p3   ---line1       p4,p5----line2
    points = [(7, 7), (33, 375), (314, 375), (454, 249), (437, 12)]
    p1, p2, p3, p4, p5 = points[0], points[1], points[2], points[3], points[4]
    crossp=  get_crosss_point(p2, p3, p4, p5)
    print(crossp) #[463.0379746835443, 375]

你可能感兴趣的:(python,开发语言)