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)文章
Python3.6+Django2.0以上 xadmin站點(diǎn)的配置和使用教程圖解
django自帶的admin站點(diǎn)雖然功能強(qiáng)大,但是界面不是很好看。這篇文章主要介紹了Python3.6+Django2.0以上 xadmin站點(diǎn)的配置和使用 ,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06
Python Selenium安裝及環(huán)境配置的實(shí)現(xiàn)
這篇文章主要介紹了Python Selenium安裝及環(huán)境配置的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
基于Python實(shí)現(xiàn)Markdown轉(zhuǎn)ePub
數(shù)字閱讀ePub格式因其良好的兼容性和閱讀體驗(yàn),已經(jīng)成為電子書的主流格式之一,而Markdown作為一種輕量級(jí)標(biāo)記語言,以其簡潔易用的特點(diǎn)深受技術(shù)寫作者喜愛,下面我們來看看如何實(shí)現(xiàn)二者之間的格式轉(zhuǎn)換吧2025-04-04
Python生成器深度解析如何構(gòu)建強(qiáng)大的數(shù)據(jù)處理管道
這篇文章主要為大家介紹了Python生成器深度解析如何構(gòu)建強(qiáng)大的數(shù)據(jù)處理管道,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
Python使用OpenCV實(shí)現(xiàn)物體跟蹤的實(shí)踐方法
文章介紹了使用OpenCV進(jìn)行物體跟蹤的小項(xiàng)目,涵蓋選擇ROI、初始化跟蹤器、逐幀更新等核心流程,代碼示例展示了不同OpenCV版本的兼容性處理、跟蹤失敗提示和交互控制,此外,還提供了常見問題解答和進(jìn)一步學(xué)習(xí)建議,需要的朋友可以參考下2026-04-04
使用Python編程分析火爆全網(wǎng)的魷魚游戲豆瓣影評(píng)
本文來為大家介紹如何使用Python爬取影評(píng)的操作,主要是爬取《魷魚游戲》在豆瓣上的一些影評(píng),對(duì)數(shù)據(jù)做一些簡單的分析,用數(shù)據(jù)的角度重新審視下這部劇,有需要的朋友可以借鑒參考下2021-10-10
Pycharm?debug程序,跳轉(zhuǎn)至指定循環(huán)條件/循環(huán)次數(shù)問題
這篇文章主要介紹了Pycharm?debug程序,跳轉(zhuǎn)至指定循環(huán)條件/循環(huán)次數(shù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08

