明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1808|回复: 5

计算坐标方位角函数

[复制链接]
发表于 2018-1-7 05:22 | 显示全部楼层 |阅读模式
;;计算坐标方位角函数
;;(azimuth (getpoint) (getpoint))
;;返回从pt1到pt2的方位角弧度值
;;by 半边书生
  1. (defun azimuth (pt1 pt2 / dxy fwj)
  2.     (setq dxy (mapcar '- pt2 pt1))
  3.     (setq fwj (atan (car dxy) (cadr dxy)))
  4.     (if (< fwj 0) (setq fwj (+ fwj (* 2 pi))))
  5.      fwj
  6. )


评分

参与人数 1明经币 +1 收起 理由
USER2128 + 1 赞一个!

查看全部评分

发表于 2018-1-14 03:25 来自手机 | 显示全部楼层
跟angle有区别吗?
发表于 2020-11-11 21:49 | 显示全部楼层
这是解密angle
发表于 2022-10-15 22:47 | 显示全部楼层
本帖最后由 cchessbd 于 2022-10-15 23:20 编辑

经过仔细核对,楼主的函数有误。(atan y x) 参数用反了。幸亏我发现了这个错误!下面贴出autocad 2006自带的atan函数英文说明。

atan

Returns the arctangent of a number in radians
(atan num1 [num2])
Arguments
num1
A number.
num2
A number.
Return Values
The arctangent of num1, in radians, if only num1 is supplied. If you supply both num1 and num2 arguments, atan returns the arctangent of num1/num2, in radians. If num2 is zero, it returns an angle of plus or minus 1.570796 radians (+90 degrees or –90 degrees), depending on the sign of num1. The range of angles returned is &#8722;pi/2 to +pi/2 radians.
Examples
Command: (atan 1)
0.785398
Command: (atan 1.0)
0.785398
Command: (atan 0.5)
0.463648
Command: (atan 1.0)
0.785398
Command: (atan -1.0)
-0.785398
Command: (atan 2.0 3.0)
0.588003
Command: (atan 2.0 -3.0)
2.55359
Command: (atan 1.0 0.0)
1.5708


将y x参数改过来后,实际上得到的是极坐标下与x轴夹角的角度。极坐标下的方位角有更简单的函数。

而且与地勘的方位角是2个概念,因为地勘是以北向为轴。

好了,不多说了,我还是再去搬砖吧,因为刚刚做了几十个错的地勘方位角。。。


发表于 2022-10-16 01:32 | 显示全部楼层
本帖最后由 cchessbd 于 2022-10-17 12:51 编辑


注意以下几句话是错误的:

进一步排错发现,楼主太马虎了。

第三象限的角度肯定是错的,因为第三象限atan 返回正值,在闭区间(0,90°);此时x轴方位角正确区间为(180,270)。

第二象限应该也是错的。此时atan 返回负值,在闭区间(-90°,0),加上360后在(270,360);而此时正确区间(90,180)。

综上,大家不要用这个函数。用系统自带的angle就行。


发表于 2022-10-17 08:25 | 显示全部楼层
本帖最后由 cchessbd 于 2022-10-17 12:49 编辑
cchessbd 发表于 2022-10-16 01:32
进一步排错发现,楼主太马虎了。

经过实际运行,楼主函数是对的,没有问题。但是很奇怪,我之前读的几十个方位角不对是什么问题?


经过对 atan 函数在cad里面的运行测试。搞明白了(atan x y)和(atan num)函数的不同。

获取角度函数有三个,(angel pt1 pt2),(atan num),(atan x y)

对点P(x,y),从第一象限顺时针转到第四象限时,(atan x y) 返回以Y轴和OP夹角弧度值,[0,pi]。此时北向方位角和返回值一致

对点P(x,y),从第二象限逆时针转到第三象限时,(atan x y) 返回以Y轴和OP夹角弧度值,(0,-pi)。此时北向方位角需加上2*pi。

所以(atan x y)返回值区间为(-pi,pi]。这和数学中atan函数以及帮助里面的说明不一样。

附带英文帮助说明:The range of angles returned is -pi/2 to +pi/2 radians.即(-pi/2,pi/2),这个是(atan num)的返回值。

而(angel pt1 pt2)返回极坐标角,即与X轴夹角。区间按第一二三四象限逆时针的话,为[0,2*pi)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-4-25 13:55 , Processed in 4.553173 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表