您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python代码计算三点之间的角度(lat长坐标)

5b51 2022/1/14 8:21:49 python 字数 3912 阅读 569 来源 www.jb51.cc/python

任何人都可以建议如何计算三点之间的角度(纬度长坐标)A : (12.92473, 77.6183) B : (12.92512, 77.61923) C : (12.92541, 77.61985) 最佳答案假设您需要角度ABC(B是角度的顶点),我会看到两种解决问题的主要方法.由于您的三个点彼此接近(纬度小于0.0007°,经度相差0.002°),我们可以

概述

任何人都可以建议如何计算三点之间的角度(纬度长坐标)

A : (12.92473,77.6183)
B : (12.92512,77.61923)
C : (12.92541,77.61985)

这是我的问题代码.为方便起见,我在这里使用numpy模块,但如果没有它,这可以很容易地完成.这段代码非常冗长,因此您可以更好地了解正在完成的工作.

import numpy as np
import math

def latlong_to_3d(latr,lonr):
    """Convert a point given latitude and longitude in radians to
    3-dimensional space,assuming a sphere radius of one."""
    return np.array((
        math.cos(latr) * math.cos(lonr),math.cos(latr) * math.sin(lonr),math.sin(latr)
    ))

def angle_between_vectors_degrees(u,v):
    """Return the angle between two vectors in any dimension space,in degrees."""
    return np.degrees(
        math.acos(np.dot(u,v) / (np.linalg.norm(u) * np.linalg.norm(v))))

# The points in tuple latitude/longitude degrees space
A = (12.92473,77.6183)
B = (12.92512,77.61923)
C = (12.92541,77.61985)

# Convert the points to numpy latitude/longitude radians space
a = np.radians(np.array(A))
b = np.radians(np.array(B))
c = np.radians(np.array(C))

# Vectors in latitude/longitude space
avec = a - b
cvec = c - b

# Adjust vectors for changed longitude scale at given latitude into 2D space
lat = b[0]
avec[1] *= math.cos(lat)
cvec[1] *= math.cos(lat)

# Find the angle between the vectors in 2D space
angle2deg = angle_between_vectors_degrees(avec,cvec)


# The points in 3D space
a3 = latlong_to_3d(*a)
b3 = latlong_to_3d(*b)
c3 = latlong_to_3d(*c)

# Vectors in 3D space
a3vec = a3 - b3
c3vec = c3 - b3

# Find the angle between the vectors in 2D space
angle3deg = angle_between_vectors_degrees(a3vec,c3vec)


# Print the results
print('\nThe angle ABC in 2D space in degrees:',angle2deg)
print('\nThe angle ABC in 3D space in degrees:',angle3deg)

这给出了结果

The angle ABC in 2D space in degrees: 177.64369006

The angle ABC in 3D space in degrees: 177.643487338

请注意,结果非常接近(偏离大约五分之一度),正如预期的那样,三个点非常接近.

总结

以上是编程之家为你收集整理的Python代码计算三点之间的角度(lat长坐标)全部内容,希望文章能够帮你解决Python代码计算三点之间的角度(lat长坐标)所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶