数学がわからない

日々の勉強をアウトプットする。

極座標で与えた4本の直線から四角形を作る

pythonで書く。

def Line2Vertex(r,t):
    # 入力はたとえば次のような4本の直線
    # r = [1,1,1,1]; # 原点からの距離
    # t = [10,100,190,280];    # 角度θ

    r = np.array(r)
    t = np.array(t)

    # COS
    c = (np.cos(np.deg2rad(t)))
    c[np.abs(c)<0.0001]=0

    # SIN
    s = (np.sin(np.deg2rad(t)))
    s[np.abs(s)<0.0001]=0

    # 4本の直線の交点を求め、頂点座標とする
    py = (r * np.append(c[1:4], c[0]) - c * np.append(r[1:4], r[0])) \
       / (s * np.append(c[1:4], c[0]) - c * np.append(s[1:4], s[0]))

    px=np.zeros(4)
    for n in range(4):
        if c[n]==0:
            if n == 3:
                px[n] = (r[0] - py[n]*s[0])/c[0]
            else:
                px[n] = (r[n+1] - py[n]*s[n+1])/c[n+1]
        else:
            px[n] = (r[n] - py[n]*s[n])/c[n]

    # 頂点座標表示
    print('px: ', px)
    print('py: ', py)

    # 描画
    for n in range(3):
        plt.plot([px[n], px[n+1]], [py[n],py[n+1]])
    plt.plot([px[3], px[0]], [py[3],py[0]])
    plt.plot(0,0,'o')

    plt.grid()                             
    plt.show()                              

実行するとこんな感じ。

Line2Vertex([1,1,1,1],[0,90,180,270])
Line2Vertex([1,1,1,1],[10,100,190,280])

f:id:rettouseinosugakubenkyo:20190519120228p:plain

Line2Vertex([1,2,3,4],[10,100,190,280])
Line2Vertex([1,2,3,4],[40,130,220,310])

f:id:rettouseinosugakubenkyo:20190519120248p:plain