- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在 tkinter Canvas 上有两个点。我需要一个函数来确定它们之间绘制的线最接近哪个基本方向(N、NW、W SW、S 等)(方向很重要)?我该怎么做呢?请注意,在 Canvas 中,左上角是 (0,0)。
我已经尝试过:
def dot_product(self, v, w):
return v[0]*w[0]+v[1]*w[1]
def inner_angle(self, v, w):
cosx=self.dot_product(v,w)/(sqrt(v[0]**2+v[1]**2)*sqrt(w[0]**2+w[1]**2))
rad=acos(cosx)
return rad*180/pi
def getAngle(self, A, B):
inner=self.inner_angle(A,B)
det = A[0]*B[1]-A[1]*B[0]
if det<0:
return inner
else:
return 360-inner
和:
def getBearing(self, pointA, pointB):
if (type(pointA) != tuple) or (type(pointB) != tuple):
raise TypeError("Only tuples are supported as arguments")
lat1 = math.radians(pointA[0])
lat2 = math.radians(pointB[0])
diffLong = math.radians(pointB[1] - pointA[1])
x = math.sin(diffLong) * math.cos(lat2)
y = math.cos(lat1) * math.sin(lat2) - (math.sin(lat1) * math.cos(lat2) * math.cos(diffLong))
initial_bearing = math.atan2(x, y)
initial_bearing = math.degrees(initial_bearing)
compass_bearing = (initial_bearing + 360) % 360
return compass_bearing
(我用这个函数来获取方向(代码不完整,更多的是一个例子))
def findDirection(self, p1, p2):
bearing = self.getBearing(p1, p2) # OR getAngle()
print(bearing)
index = [180, 0]
closest = min(index, key=lambda x:abs(x-bearing))
if closest == 10:
print(str(bearing) + " : UP")
elif closest == 360:
print(str(bearing) + " : DOWN")
elif closest == 0:
print(str(bearing) + " : RIGHT")
elif closest == 180:
print(str(bearing) + " : LEFT")
这些都不起作用。结果似乎不够一致,无法使用。有更好的方法吗?
最佳答案
这是我提出的方法,用于确定与线段所指向的方向最接近的罗盘方向 [A, B]
由其端点 point_a
定义和point_b
:
point_a
<小时/>with points defined in screen coordinates (Y axis down), call
get_bearings(point_a, point_b)
If the points defined in standard cartesian coordinates (Y axis up), callassign_bearing_to_compass(point_a, point_b)
(The tests underneath the code show the results of using points in standard coordinates, and screen coordinates.)
import math
def _change_origin_of_point_b_to_point_a(point_a, point_b):
# uses standard Y axis orientation, not screen orientation
return (point_b[0] - point_a[0], point_b[1] - point_a[1])
def _calc_angle_segment_a_b_with_x_axis(point_a, point_b):
# uses standard Y axis orientation, not screen orientation
xa, ya = point_a
xb, yb = _change_origin_of_point_b_to_point_a(point_a, point_b)
return math.atan2(yb, xb)
def determine_bearing_in_degrees(point_a, point_b):
"""returns the angle in degrees that line segment [point_a, point_b)]
makes with the horizontal X axis
"""
# uses standard Y axis orientation, not screen orientation
return _calc_angle_segment_a_b_with_x_axis(point_a, point_b) * 180 / math.pi
def assign_bearing_to_compass(point_a, point_b):
"""returns the standard bearing of line segment [point_a, point_b)
"""
# uses standard Y axis orientation, not screen orientation
compass = {'W' : [157.5, -157.5],
'SW': [-157.5, -112.5],
'S' : [-112.5, -67.5],
'SE': [-67.5, -22.5],
'E' : [-22.5, 22.5],
"NE": [22.5, 67.5],
'N' : [67.5, 112.5],
'NW': [112.5, 157.5]}
bear = determine_bearing_in_degrees(point_a, point_b)
for direction, interval in compass.items():
low, high = interval
if bear >= low and bear < high:
return direction
return 'W'
def _convert_to_negative_Y_axis(compass_direction):
"""flips the compass_direction horizontally
"""
compass_conversion = {'E' : 'E',
'SE': 'NE',
'S' : 'N',
'SW': 'NW',
'W' : 'W',
"NW": 'SW',
'N' : 'S',
'NE': 'SE'}
return compass_conversion[compass_direction]
def get_bearings(point_a, point_b):
return _convert_to_negative_Y_axis(assign_bearing_to_compass(point_a, point_b))
(使用标准三角圆象限)
point_a = (0, 0)
points_b = [(1, 0), (1, 3), (1, 2), (1, 1), (2, 1), (3, 1), (0, 1)]
print("point_a, point_b Y_up Y_down (in screen coordinates)")
for point_b in points_b:
print(point_a, ' ', point_b, ' ', assign_bearing_to_compass(point_a, point_b), ' ', get_bearings(point_a, point_b))
point_a, point_b Y_up Y_down (in screen coordinates)
(0, 0) (1, 0) E E
(0, 0) (1, 3) N S
(0, 0) (1, 2) NE SE
(0, 0) (1, 1) NE SE
(0, 0) (2, 1) NE SE
(0, 0) (3, 1) E E
(0, 0) (0, 1) N S
point_a = (0, 0)
points_b = [(-1, 0), (-1, 3), (-1, 2), (-1, 1), (-2, 1), (-3, 1), (0, 1)]
print("point_a, point_b Y_up Y_down (in screen coordinates)")
for point_b in points_b:
print(point_a, ' ', point_b, ' ', assign_bearing_to_compass(point_a, point_b), ' ', get_bearings(point_a, point_b))
point_a, point_b Y_up Y_down (in screen coordinates)
(0, 0) (-1, 0) W W
(0, 0) (-1, 3) N S
(0, 0) (-1, 2) NW SW
(0, 0) (-1, 1) NW SW
(0, 0) (-2, 1) NW SW
(0, 0) (-3, 1) W W
(0, 0) (0, 1) N S
point_a = (0, 0)
points_b = [(-1, 0), (-1, -3), (-1, -2), (-1, -1), (-2, -1), (-3, -1), (0, -1)]
print("point_a, point_b Y_up Y_down (in screen coordinates)")
for point_b in points_b:
print(point_a, ' ', point_b, ' ', assign_bearing_to_compass(point_a, point_b), ' ', get_bearings(point_a, point_b))
point_a, point_b Y_up Y_down (in screen coordinates)
(0, 0) (-1, 0) W W
(0, 0) (-1, -3) S N
(0, 0) (-1, -2) SW NW
(0, 0) (-1, -1) SW NW
(0, 0) (-2, -1) SW NW
(0, 0) (-3, -1) W W
(0, 0) (0, -1) S N
point_a = (0, 0)
points_b = [(1, 0), (1, -3), (1, -2), (1, -1), (2, -1), (3, -1), (0, -1)]
print("point_a, point_b Y_up Y_down (in screen coordinates)")
for point_b in points_b:
print(point_a, ' ', point_b, ' ', assign_bearing_to_compass(point_a, point_b), ' ', get_bearings(point_a, point_b))
point_a, point_b Y_up Y_down (in screen coordinates)
(0, 0) (1, 0) E E
(0, 0) (1, -3) S N
(0, 0) (1, -2) SE NE
(0, 0) (1, -1) SE NE
(0, 0) (2, -1) SE NE
(0, 0) (3, -1) E E
(0, 0) (0, -1) S N
关于Python Tkinter Canvas 获取线的基本方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46258873/
我正在使用 MapBox 绘制兴趣点,这些兴趣点是通过基于 Rails 构建的用户生成表单提交的。当前,用户输入一个地址,然后该地址通过 gem(地理编码器)计算出 Lat 和 Lng。从那里我通过
我正在纵向平板电脑上开发应用程序。 但是,当平板电脑转到横向模式时,应用程序也会转动,并且所有对齐方式都将被取消。那么有什么方法可以将我的 WPF 应用程序锁定到一个方向? 谢谢! 最佳答案 我必须同
我在我的应用程序中的 mkmapview 上显示了两点之间的路线,但我想显示这两点的方向。点的纬度和经度存储在 NSArray 中。 最佳答案 这可能为时已晚,您可能已经解决了它,但这是我已经测试过并
我正在处理一个小型 Unity3D 项目,我需要从另一个工具导入一些数据。该工具通过两个向量为我提供了对象方向,我需要将其移植到 Unity。 例如,我有这两个向量; x = Vector( 0.70
有没有办法以编程方式设置 UIActionSheet 的方向?我的 iPhone 方向是纵向,但 UIActionSheet 需要是横向。这可以吗? 编辑: 所以我的问题是我不想将 rootviewc
如何在 Python 中根据 2 个 GPS 坐标计算速度、距离和方向(度)?每个点都有纬度、经度和时间。 我在这篇文章中找到了半正矢距离计算: Calculate distance between
需要一个代码来更改 div 的属性,具体取决于 iPhone 设备的位置。在这段代码工作之前现在停止这样做了吗? @media all and (orientation:portrait) { .
在“View Did Load”中,我试图确定 View 的大小,以便我可以适本地调整 subview 的大小。我希望它始终围绕屏幕的长度和宽度拉伸(stretch),而不管方向如何。 quest *
如何根据对象的方向移动对象?我的意思是,我有一个处于某个位置的立方体,我想绕 Y 轴旋转并根据它们的方向移动。然后再次移动和旋转以改变方向。像这样的事情: 最佳答案 在 JS 中你可以尝试这样的事情:
我目前有一个处于横向模式的 SurfaceView。 目前我正在尝试使用添加操作栏/菜单栏 /*Action Bar */ //this.setRequestedOrientation(Activit
我正在使用 cocos2d,我想播放电影。为此,我创建了 MPMoviePlayerViewController 并将其作为 [[CCDirector sharedDirector] openGLVi
我在 cocos2d 中创建了一个游戏,因为我想使用我找到的一些 UIKit 元素 kobold2d。 我移植了游戏,但问题是我的 iPhone 刺激器旋转了, 但不是显示的节点。 必须使用: bac
我可以在 iOS 中的 UITabBarController 中更改方向吗?我有这样的东西: UITableViewController-> Team Tab -> UINavigationContr
我有 UINavigationController 和几个 View Controller 。这是他们的名单:主->相册->图片 现在,在第一个和第二个(主要和专辑)中,我希望 UINavigatio
人们普遍认为,在过去几年中,标准显示器的最佳网站宽度已从 800 像素增加到 1024+ 像素(网站通常为 960 像素宽),但随着移动设备的兴起,哪些分辨率被认为是“关键”迎合? 例如,this
我正在做一个 GTK+ 项目,我需要一个像这样的垂直 GtkLevelBar: 但我不知道如何从默认的水平 GtkLevelBar 翻转它: 这是我的 GtkLevelBar 代码。 GtkWidge
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我的 collectionView 以横向模式显示 20 个项目。在纵向模式下,我只想展示 8 个可重复使用的项目。我怎样才能做到这一点? collectionView 何时在数据源上调用 colle
可以在 list 文件中设置 Activity 的方向。 但是否也可以通过代码来实现?如果是,怎么办? 谢谢! 最佳答案 setRequestedOrientation(ActivityInfo.SC
我希望在纬度、经度和用户当前位置之间集成方向。我希望通过点击按钮将用户定向到已安装的 Google map /其他应用程序并显示方向。 我搜索了 SO 和谷歌,但找不到好的来源,因此我发布了这个问题。
我是一名优秀的程序员,十分优秀!