二维数学地理方向
我正在尝试使用南北东西方向的逻辑来检查二维图形上的点(px,py)相对于线段(lx1,ly1)(lx2,ly2)的位置。我实现的逻辑是从该点在线段上绘制垂直线。
如果垂线在一条线上,则表示其南方。
如果右侧的点表示其东方。
如果左侧的点表示西。
如果垂直线远离前进方向的线,则表示北。
如果垂线远离向后方向的线,则表示南。
我的问题是,这个逻辑在纸面上看起来不错,但很难决定它是西北、东北、西南还是东南案例。谁能建议我如何计算这个逻辑?我正在使用 C++,但是任何语言的算法都会有很大的帮助。
我正在使用线段的端点来计算南北关系。
干杯
I am trying to check the location of a point(px, py) on 2D graph in relation to a line segment (lx1, ly1) (lx2, ly2), using logic of North South East West directions. The logic I have implemented is to draw a perpendicular on the line segment from the point.
if perpendicular is on line that means its south.
If point on right side means its East.
If point on left side means West.
If perpendicular is away from line in forward direction will mean North.
If perpendicular is away from line in backward direction will mean South.
My problem is that this logic looks good on paper, but its getting really hard to decide whether its a NW, NE, SW or SE case. Can anyone suggest me how to compute this logic?? I am using C++, but algorithm in any language will be a great help.
I am using end point of line segment to calculate North South East West relation.
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
delta_x = x2 - x1
delta_y = y2 - y1
距离 = sqrt (delta_x^2 + delta_y^2)
tan (theta) = delta_y / delta_x
theta = arctan (delta_y / delta_x)
;; 但不要除以零!theta
乘以180/PI
即可得到度数。度数是从正侧开始逆时针旋转的x 轴。最终,您需要做少量代数来重新调整度数,使 0 向上(而不是向右)并顺时针运行。但在此之前:
有一个问题是
arctan (1 / -1)
与arctan (-1 / 1)
相同。 即,您将得到-PI/4
弧度或 -45 度,对于左上角(需要 180 度偏移)和右下角(正常)。您必须对delta_y
的符号进行测试 vs.delta_x
来查看arctan
的结果是否正确需要调整。在编写解决方案之前,请务必编写测试代码,以确保您调用的函数产生预期值。
delta_x = x2 - x1
delta_y = y2 - y1
distance = sqrt (delta_x^2 + delta_y^2)
tan (theta) = delta_y / delta_x
theta = arctan (delta_y / delta_x)
;; but don't divide by zero!theta
by180/PI
to get degreesThe degrees are counter-clockwise from the positive side of the x-axis. Eventually you'll need to do a small amount of algebra to reorient the degrees so that 0 is up (instead of to the right) and run clockwise. But before that:
A problem is that
arctan (1 / -1)
is the same asarctan (-1 / 1)
. I.e., you'll get-PI/4
radians or -45 degrees, for both upper left (needs 180 degree offset) and lower right (ok as is). You'll have to do tests on the sign ofdelta_y
vs.delta_x
to see if the result ofarctan
needs to be adjusted.Before you code your solution, be sure to code tests as well to make sure the functions you are calling produce expected values.
我同情
ndim
。计算从一点到另一点的方向很容易。我不明白哪种情况会要求您需要一条线的方向。这是一个地图应用程序吗?是否有一条路,并且在路段的大约一半处,您在侧面有一些点?I sympathise with
ndim
. Calculating direction from one point to another is easy. I don't understand which context would require you to want direction from a line. Is this a mapping application? Is there a road, and about halfway along a road segment you've got some point off to the side?线段的“北”或“NE”或“东”实际含义的语义尚不清楚。
“北”、“东”或“NE”等方向通常用于描述一个点相对于另一点(基点)的位置。您用作基点的线段上的点是什么?
编辑:现在你说你想使用终点
(x2,y2)
作为罗盘的中心点,一个点(x,y)
将通过检查 delta 向量(x-x2,y-y2)
相对于罗盘定位。易于推理的方法在 delta 向量上使用
atan2()
,并考虑atan2()
返回的角度。不过,将
atan2()
的参数相互比较并从中确定结果角度范围也可能是个好主意。这基本上避免了在运行时调用atan2()
,但代价是您需要在编译时(或之前)进行一些计算。The semantics of what "north" or "NE" or "east" of a line segment actually means is unclear.
Directions like "north" or "east" or "NE" are usually used to describe the location of one point relative to another (base) point. What is the point on the line segment you are using as that base point?
EDIT: Now that you say you want to use the end point
(x2,y2)
as the center point for the compass, a point(x,y)
will be located relative to the compass by examining the delta vector(x-x2,y-y2)
.The easy to reason about method uses
atan2()
on the delta vector, and considers the angle returned byatan2()
.However, it might also be a good idea to just compare the arguments to
atan2()
to each other and determine the resulting angle ranges from that. This basically avoids callingatan2()
at runtime at the cost of you needing to do some calculations at (or before) compile time.