天天看點

python實作簡單的三邊測量定位

定位原理很簡單,故不贅述,直接上源碼,内附注釋。(如果對您的學習有所幫助,還請幫忙點個贊,謝謝了)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed May 16 10:50:29 2018

@author: dag
"""
import sympy
import numpy as np
import math
from matplotlib.pyplot import plot
from matplotlib.pyplot import show
import matplotlib.pyplot as plt
import matplotlib
#解決無法顯示中文問題,fname是加載字型路徑,根據自身pc實際确定,具體請百度
zhfont1 = matplotlib.font_manager.FontProperties(fname='/System/Library/Fonts/Hiragino Sans GB W3.ttc')

#随機産生3個參考節點坐标
maxy = 1000
maxx = 1000
cx = maxx*np.random.rand(3)
cy = maxy*np.random.rand(3)
dot1 = plot(cx,cy,'k^')

#生成盲節點,以及其與參考節點歐式距離
mtx = maxx*np.random.rand()
mty = maxy*np.random.rand()
plt.hold('on')
dot2 = plot(mtx,mty,'go')
da = math.sqrt(np.square(mtx-cx[0])+np.square(mty-cy[0]))
db = math.sqrt(np.square(mtx-cx[1])+np.square(mty-cy[1])) 
dc = math.sqrt(np.square(mtx-cx[2])+np.square(mty-cy[2]))

#計算定位坐标  
def triposition(xa,ya,da,xb,yb,db,xc,yc,dc): 
    x,y = sympy.symbols('x y')
    f1 = 2*x*(xa-xc)+np.square(xc)-np.square(xa)+2*y*(ya-yc)+np.square(yc)-np.square(ya)-(np.square(dc)-np.square(da))
    f2 = 2*x*(xb-xc)+np.square(xc)-np.square(xb)+2*y*(yb-yc)+np.square(yc)-np.square(yb)-(np.square(dc)-np.square(db))
    result = sympy.solve([f1,f2],[x,y])
    locx,locy = result[x],result[y]
    return [locx,locy]
    
#解算得到定位節點坐标
[locx,locy] = triposition(cx[0],cy[0],da,cx[1],cy[1],db,cx[2],cy[2],dc)
plt.hold('on')
dot3 = plot(locx,locy,'r*')

#顯示腳注
x = [[locx,cx[0]],[locx,cx[1]],[locx,cx[2]]]
y = [[locy,cy[0]],[locy,cy[1]],[locy,cy[2]]]
for i in range(len(x)):
    plt.plot(x[i],y[i],linestyle = '--',color ='g' )
plt.title('三邊測量法的定位',fontproperties=zhfont1)  
plt.legend(['參考節點','盲節點','定位節點'], loc='lower right',prop=zhfont1)
show() 
derror = math.sqrt(np.square(locx-mtx) + np.square(locy-mty)) 
print(derror) 
           

輸出效果圖:

python實作簡單的三邊測量定位