0

編集: サンプル ファイルの Git リポジトリ https://github.com/tpubben/lineIntersect

複数のセグメントで構成される 1 つの連続した線と交差する交差する線のセットに基づいて、x、y 座標で線の交点を計算しようとしています。

連続線は、次のようにタプルのリストで表されます。各セグメントは、前のセグメントの終点の x/y 座標で始まります。

lineA = [((x1, y1),(x2,y2)), ((x2,y2),(x3,y3))....]

交差する線は同じ方法で表されますが、それぞれが個別の線です (共有点はありません)。

lineB = [((x1, y1),(x2,y2))...]

連続線 (lineA) を反復処理し、どの交差する線が lineA のどのセグメントと交差するかを確認しようとしています。

線の交点がどのように見えるかの例の画像は次のとおりです。 交差する線

これまでのところ、私は次のことを試しました:

from __future__ import print_function

def newSurveys(nintyin, injectorin):
    # pull data out of pre-prepared CSV files
    fh = open(nintyin)
    fho = open(injectorin)
    rlines = fho.readlines()
    rlines90 = fh.readlines()

    segA = []
    segB = []
    segA90 = []
    segB90 = []

    for item in rlines:
        if not item.startswith('M'):
            item = item.split(',')
            segA.append((float(item[4]),float(item[5])))#easting northing
            segB.append((float(item[4]),float(item[5])))#easting northing

    segB.pop(0)
    z = len(segA)-1
    segA.pop(z)

    for item in rlines90:
        if not item.startswith('N'):
            item = item.split(',')
            segA90.append((float(item[1]),float(item[0])))#easting northing
            segB90.append((float(item[3]),float(item[2])))#easting northing

    activeWellSegs = []
    injector90Segs = []

    for a, b in zip(segA, segB):
        activeWellSegs.append((a,b))

    for c, d in zip(segA90, segB90):
        injector90Segs.append((c,d))


    if len(activeWellSegs) >= len(injector90Segs):
        lineA = activeWellSegs
        lineB = injector90Segs
    else:
        lineA = injector90Segs
        lineB = activeWellSegs


    for l1 in lineA:        
        for l2 in lineB:
            ##### Use differential equation to calculate line intersections,
            ##### taken from another user's post     
            def line_intersection(line1, line2):
                xdiff = (line1[0][0] - line1[1][0], line2[0][0] - line2[1][0])
                ydiff = (line1[0][1] - line1[1][1], line2[0][1] - line2[1][1])

                def det(a, b):
                    return a[0] * b[1] - a[1] * b[0]

                div = det(xdiff, ydiff)
                if div == 0:
                   raise Exception('lines do not intersect')

                d = (det(*line1), det(*line2))
                x = det(d, xdiff) / div
                y = det(d, ydiff) / div
                return x, y

            print (line_intersection(l1, l2), file=lprint)



    newSurveys('producer90.csv', 'injector.csv') 
4

1 に答える 1

-1

あなたのコードは特定のデータセットを扱っているように見えます(つまり、「startsWith( 'N')」などが何を指しているのかわかりません)ので、この質問には抽象的にしか答えられません。

すべてを実行しようとする 1 つの大きな関数ではなく、1 つの特定のタスクを実行する複数の関数にコードを分割してみてください。作業とトラブルシューティングがはるかに簡単になります。

def getScalar(lineSegment):
    return (lineSegment[1][0] - lineSegment[0][0],
            lineSegment[1][1] - lineSegment[0][1])

def doTheyIntersect(lineA, lineB):
    scalarA = getScalar(lineA)
    scalarB = getScalar(lineB)

    s = (-1.0 * scalarA[1] * (lineA[0][0] - lineB[0][0]) + scalarA[0] * (lineA[0][1] - lineB[0][1])) / (-1.0 * scalarB[0] * scalarA[1] + scalarA[0] * scalarB[1])
    t = (scalarB[0] * (lineA[0][1] - lineB[0][1]) - scalarB[1] * (lineA[0][0] - lineB[0][0])) / (-1.0 * scalarB[0] * scalarA[1] + scalarA[0] * scalarB[1])

    if 0.0 <= s <= 1.0 and 0.0 <= t <= 1.0:
        return True
    else:
        return False

lineA = [(x, y), (x1, y1), ...]
lineB = [(x, y), (x1, y1), ...]

for index, segment in enumerate(lineA):
    if index + 1 < len(lineA):
        for index2 in range(0, len(lineB), 2):
             if doTheyIntersect((lineA[index], lineA[index + 1]), (lineB[index2], lineB[index2+1])):
                print("lineB ({0}, {1}) intersects lineA at ({2}, {3})".format(str(lineB[index2]),str(lineB[index2+1]), str(lineA[index]), str(lineA[index + 1]))

これが一般的な考え方です。幾何学式は次から取得しました。

2 つの線分が交差する場所をどのように検出しますか?

于 2016-08-22T20:16:41.823 に答える