欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python中實現(xiàn)根據(jù)坐標(biāo)點位置求方位角

 更新時間:2023年08月16日 09:16:04   作者:邊ing  
這篇文章主要介紹了python中實現(xiàn)根據(jù)坐標(biāo)點位置求方位角方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

python根據(jù)坐標(biāo)點位置求方位角

話不多說,直接上代碼:

from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QLabel, QWidget, QVBoxLayout
import sys
import math
class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
    def initUI(self):
        self.resize(500, 250)
        self.setWindowTitle("坐標(biāo)系")
        self.lb = QLabel("點1到原點距離:", self)
        self.lb.move(20, 40)
        self.lb2 = QLabel("點1與原點的角度:", self)
        self.lb2.move(20, 80)
        self.lb3 = QLabel("點2到原點距離:", self)
        self.lb3.move(20, 120)
        self.lb4 = QLabel("點2與原點的角度:", self)
        self.lb4.move(20, 160)
        self.bt1 = QPushButton('查詢', self)
        self.bt1.move(20, 200)
        self.edit = QLineEdit('', self)
        self.edit.move(150, 40)
        self.edit2 = QLineEdit('', self)
        self.edit2.move(150, 80)
        self.edit3 = QLineEdit('', self)
        self.edit3.move(150, 120)
        self.edit4 = QLineEdit('', self)
        self.edit4.move(150, 160)
        self.bt1.clicked.connect(self.calc_angle)
        self.show()
    def calc_angle(self):
        x1 = float(self.edit.text()) * math.cos(math.radians(int(self.edit2.text())))
        y1 = float(self.edit.text()) * math.sin(math.radians(int(self.edit2.text())))
        x2 = float(self.edit3.text()) * math.cos(math.radians(int(self.edit4.text())))
        y2 = float(self.edit3.text()) * math.sin(math.radians(int(self.edit4.text())))
        angle = 0
        dy = y2 - y1
        dx = x2 - x1
        if dx == 0 and dy > 0:
            angle = 90
            print('順時針:', angle, '°')
        if dx == 0 and dy < 0:
            angle = 270
            print('順時針:', angle, '°')
        if dy == 0 and dx > 0:
            angle = 0
            print('順時針:', angle, '°')
        if dy == 0 and dx < 0:
            angle = 180
            print('順時針:', angle, '°')
        if dx > 0 and dy > 0:
            angle = math.atan(dy / dx)* 180 / math.pi
            print('東偏北:',angle,'°')
        elif dx < 0 and dy > 0:
            angle = 90 - math.atan(dy / abs(dx))* 180 / math.pi
            print('北偏西:', angle, '°')
        elif dx < 0 and dy < 0:
            angle = math.atan(dy / dx)* 180 / math.pi
            print('西偏南:', angle, '°')
        elif dx > 0 and dy < 0:
            angle = math.atan(abs(dy) / dx)* 180 / math.pi
            print('東偏南:', angle, '°')
        length = math.sqrt(dy * dy + dx * dx)
        print(length)
if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

> 這里是引用

這里是引用

最后的結(jié)果之所以有那么多小數(shù)點,是因為math.pi,其實就是π,3.1415926…,有很多小數(shù)位!

其實上文就是,在一個坐標(biāo)系中,已知兩個點到坐標(biāo)原點的距離,以及它們和橫軸的角度(這都是需要自己手動輸入的),那么就以第一個點為此時的坐標(biāo)原點,求第二個點到第一個點的距離,以及第二點在第一點的相關(guān)方位。

ps:我這里是已知兩點到原點的距離和角度,就像在一個極坐標(biāo)里一樣,如果直接知道兩點的橫縱坐標(biāo),那么會更好求。

python根據(jù)坐標(biāo)點計算方位角函數(shù)

# 計算方位角函數(shù)
def azimuthAngle( x1, y1, x2, y2):
  angle = 0.0;
  dx = x2 - x1
  dy = y2 - y1
  if x2 == x1:
    angle = math.pi / 2.0
    if y2 == y1 :
      angle = 0.0
    elif y2 < y1 :
      angle = 3.0 * math.pi / 2.0
  elif x2 > x1 and y2 > y1:
    angle = math.atan(dx / dy)
  elif x2 > x1 and y2 < y1 :
    angle = math.pi / 2 + math.atan(-dy / dx)
  elif x2 < x1 and y2 < y1 :
    angle = math.pi + math.atan(dx / dy)
  elif x2 < x1 and y2 > y1 :
    angle = 3.0 * math.pi / 2.0 + math.atan(dy / -dx)
  return (angle * 180 / math.pi)
#計算角度
print(white_point)
if white_point[0][0]>white_point[1][0]:
    x1=white_point[1][0];
    y1=white_point[1][1];
    x2=white_point[0][0];
    y2=white_point[0][1];
else:
    x1=white_point[0][0];
    y1=white_point[0][1];
    x2=white_point[1][0];
    y2=white_point[1][1];
angle = 90-azimuthAngle(x1,y1,x2,y2)
print("angle:"+str(angle))

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論