python中實(shí)現(xiàn)根據(jù)坐標(biāo)點(diǎn)位置求方位角
python根據(jù)坐標(biāo)點(diǎn)位置求方位角
話不多說,直接上代碼:
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("點(diǎn)1到原點(diǎn)距離:", self) self.lb.move(20, 40) self.lb2 = QLabel("點(diǎn)1與原點(diǎn)的角度:", self) self.lb2.move(20, 80) self.lb3 = QLabel("點(diǎn)2到原點(diǎn)距離:", self) self.lb3.move(20, 120) self.lb4 = QLabel("點(diǎn)2與原點(diǎn)的角度:", 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('順時(shí)針:', angle, '°') if dx == 0 and dy < 0: angle = 270 print('順時(shí)針:', angle, '°') if dy == 0 and dx > 0: angle = 0 print('順時(shí)針:', angle, '°') if dy == 0 and dx < 0: angle = 180 print('順時(shí)針:', 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ù)點(diǎn),是因?yàn)閙ath.pi,其實(shí)就是π,3.1415926…,有很多小數(shù)位!
其實(shí)上文就是,在一個(gè)坐標(biāo)系中,已知兩個(gè)點(diǎn)到坐標(biāo)原點(diǎn)的距離,以及它們和橫軸的角度(這都是需要自己手動(dòng)輸入的),那么就以第一個(gè)點(diǎn)為此時(shí)的坐標(biāo)原點(diǎn),求第二個(gè)點(diǎn)到第一個(gè)點(diǎn)的距離,以及第二點(diǎn)在第一點(diǎn)的相關(guān)方位。
ps:我這里是已知兩點(diǎn)到原點(diǎn)的距離和角度,就像在一個(gè)極坐標(biāo)里一樣,如果直接知道兩點(diǎn)的橫縱坐標(biāo),那么會(huì)更好求。
python根據(jù)坐標(biāo)點(diǎn)計(jì)算方位角函數(shù)
# 計(jì)算方位角函數(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)
#計(jì)算角度 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é)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python實(shí)現(xiàn)為圖片添加水印的示例詳解
這篇文章主要介紹了如何通過Python3實(shí)現(xiàn)添加水印,這樣發(fā)朋友圈,圖片再也不怕被盜了?。?!文中的示例代碼簡潔易懂,需要的可以參考一下2022-02-02詳解Python中的時(shí)間格式的讀取與轉(zhuǎn)換(time模塊)
這篇文章主要介紹了Python中的時(shí)間格式的讀取與轉(zhuǎn)換(time模塊),文末給大家介紹了python的時(shí)間獲取與轉(zhuǎn)化:time模塊和datetime模塊的相關(guān)知識(shí),需要的朋友可以參考下2023-05-05Python實(shí)現(xiàn)按逗號(hào)分隔列表的方法
今天小編就為大家分享一篇Python實(shí)現(xiàn)按逗號(hào)分隔列表的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-10-10解決Django 在ForeignKey中出現(xiàn) non-nullable field錯(cuò)誤的問題
今天小編就為大家分享一篇解決Django 在ForeignKey中出現(xiàn) non-nullable field錯(cuò)誤的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08PyInstaller將Python腳本打包為.exe可執(zhí)行文件的步驟詳解
Python是一門強(qiáng)大而靈活的編程語言,為了方便共享和部署,我們可以將 Python 腳本打包為可執(zhí)行文件(.exe),這樣其他用戶就無需安裝 Python環(huán)境,直接運(yùn)行可執(zhí)行文件即可,本文將介紹如何使用PyInstaller 工具實(shí)現(xiàn)這一目標(biāo),需要的朋友可以參考下2023-12-12三個(gè)python爬蟲項(xiàng)目實(shí)例代碼
這篇文章主要介紹了三個(gè)python爬蟲項(xiàng)目實(shí)例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12python3.8+django2+celery5.2.7環(huán)境準(zhǔn)備(python測(cè)試開發(fā)django)
這篇文章主要介紹了python測(cè)試開發(fā)django之python3.8+django2+celery5.2.7環(huán)境準(zhǔn)備工作,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07