如何确定点位于罗盘上两点之间
给定罗盘上的任意两点(开始范围和结束范围)以形成一个范围。例如从 270(开始范围)度到 45(结束范围)度,并给出另一个点(例如 7),我如何计算出该点是否在开始范围和结束范围之间?
我正在尝试编写一些代码来计算风(在上述第3点中)是从海上吹还是从陆地吹,其中陆地由 Start range 和 End range 定义。
非常感谢 安迪
更新:11/10/2010 18:46BST 从 @sth 的解决方案来看,以下内容似乎按预期工作。
#!/usr/bin/perl -w
sub isoffshore {
my ( $beachstart,$beachend,$wind) = @_;
if( $beachend < $beachstart) {
$beachend += 360;
}
if ($wind < $beachstart){
$wind += 360;
}
if ($wind <= $beachend){
print ("Wind is Onshore\n");
return 0;
}else{
print ("Wind is Offshore\n");
return 1;
}
}
isoffshore ("0","190","3"); #Should be onshore
isoffshore ("350","10","11"); #Should be offshore
isoffshore ("270","90","180");#Should be offshore
isoffshore ("90","240","0"); #Should be offshore
isoffshore ("270","90","180");#Should be offshore
isoffshore ("0","180","90"); #Should be onshore
isoffshore ("190","0","160"); #Should be offshore
isoffshore ("110","240","9"); #Should be offshore
isoffshore ("0","180","9"); #Should be onshore
isoffshore ("0","180","179"); #Should be onshore
结果
@localhost ~]$ ./offshore2.pl
Wind is Onshore
Wind is Offshore
Wind is Offshore
Wind is Offshore
Wind is Offshore
Wind is Onshore
Wind is Offshore
Wind is Offshore
Wind is Onshore
Wind is Onshore
Given any two points on compass (Start range and End Range) to form a range. Example from 270(Start range) degrees to 45(End range)degrees and given another point say 7 , how can I work out if that point is between Start and End range ?
I'm trying to write some code to work out if the Wind (in the above point 3) is blowing from the sea or from the land , where the land is defind by Start range and End range .
Many Thanks
Andy
Update:11/10/2010 18:46BST
From @sth's solution the following seems to work for as expected.
#!/usr/bin/perl -w
sub isoffshore {
my ( $beachstart,$beachend,$wind) = @_;
if( $beachend < $beachstart) {
$beachend += 360;
}
if ($wind < $beachstart){
$wind += 360;
}
if ($wind <= $beachend){
print ("Wind is Onshore\n");
return 0;
}else{
print ("Wind is Offshore\n");
return 1;
}
}
isoffshore ("0","190","3"); #Should be onshore
isoffshore ("350","10","11"); #Should be offshore
isoffshore ("270","90","180");#Should be offshore
isoffshore ("90","240","0"); #Should be offshore
isoffshore ("270","90","180");#Should be offshore
isoffshore ("0","180","90"); #Should be onshore
isoffshore ("190","0","160"); #Should be offshore
isoffshore ("110","240","9"); #Should be offshore
isoffshore ("0","180","9"); #Should be onshore
isoffshore ("0","180","179"); #Should be onshore
Results
@localhost ~]$ ./offshore2.pl
Wind is Onshore
Wind is Offshore
Wind is Offshore
Wind is Offshore
Wind is Offshore
Wind is Onshore
Wind is Offshore
Wind is Offshore
Wind is Onshore
Wind is Onshore
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为罗盘上的点是指单位圆上的点。通过单位圆上的两点“之间”,您的意思是您已经在单位圆上描述了一条弧,并且想知道给定点是否在该弧中。
假设单位圆上的所有点都用角度来描述,对于描述单位圆上的点的角度
t
,我们要求0 <= t << 2 * 圆周率。
假设您的弧被描述为弧
(t_1, t_2)
(即,从单位圆上与角度t_1 对应的点逆时针遍历
到单位圆上与角度t_2
* 对应的点)。然后,给定单位圆上具有相应角度t
的点,t
确实位于从t_1
到的逆时针圆弧上>t_2
如果t_2 > t_1
和t_1 <= t <= t_2
或t_1 > t_2
而不是t_2 <= t <= t_1
。因此,
By points on a compass, I assume that you mean points on the unit circle. And by "between" two points on the unit circle, you mean that you have described an arc on the unit circle and want to know if a given point is in that arc.
Assume that all points on the unit circle are described by angles and for such an angle
t
describing a point on the unit circle we require that0 <= t < 2 * pi
.Let's say that your arc is described as the arc
(t_1, t_2)
(that is, traverse counterclockwise from the point on the unit circle corresponding to the anglet_1
to the point on the unit circle corresponding to the anglet_2
*). Then, given a point on the unit circle with corresponding anglet
, it is true thatt
is on the counterclockwise arc fromt_1
tot_2
ift_2 > t_1
andt_1 <= t <= t_2
ort_1 > t_2
and nott_2 <= t <= t_1
.Thus,
这是一个单行函数,它使用模 (
%
) 运算符来处理环绕情况。输入值假定在 0..359(度)范围内:请注意,在 C/C++ 中,测试每一侧都需要
+ 360
项,因为>%
处理负值。Here's a one line function which uses the modulo (
%
) operator to handle the wraparound case. Input values are assumed to be in the range 0..359 (degrees):Note that the
+ 360
term on each side of the test is required in C/C++ due to the unfortunate way that%
treats negative values.如果你的所有点都像
0 <= point << 360:
This should work if all your points are like
0 <= point < 360
: