面向python的平面角度数学库。
anglr的Python项目详细描述
面向python的平面角度数学库。
这个库包含许多不同的功能,用于单位之间的转换、角度比较和角度运算。
链接:
快速启动:pip3 install anglr。
基本原理
请考虑以下简单的角度比较代码:
importmathheading=get_compass_value()# angle in radians normalized to $[0, 2*pi)$iftarget-math.pi/4<=heading<=target+math.pi/4:print("Facing the target")else:print("Not facing the target")
角码无处不在。以上完全是错误的(考虑当target为0时会发生什么),但是在编写和代码评审期间,这很容易被忽略。
有了Angr,有一个更好的方法:
importmathfromanglrimportAngleheading=Angle(get_compass_value())ifheading.angle_between(target)<=math.pi/4:print("Facing the target")else:print("Not facing the target")
更好的是-这将正确地考虑模块化算法时,比较角度。
示例
角度创建:
frommathimportpifromanglrimportAngleprint(Angle())print(Angle(87*pi/2))print(Angle(pi/2,"radians"))print(Angle(Angle(pi/2,"radians")))# same as aboveprint(Angle(64.2,"degrees"))print(Angle(384.9,"gradians"))print(Angle(4.5,"hours"))print(Angle(203.8,"arcminutes"))print(Angle(42352.7,"arcseconds"))print(Angle((56,32),"vector"))# angle in standard position - counterclockwise from positive X-axis
角度转换:
fromanglrimportAnglex=Angle(58.3)print([x],str(x),x.radians,x.degrees,x.gradians,x.hours,x.arcminutes,x.arcseconds,x.vector,x.x,x.y)print(complex(x))print(float(x))print(int(x))x.radians=pi/2print(x.dump())x.degrees=64.2print(x.dump())x.gradians=384.9print(x.dump())x.hours=4.5print(x.dump())x.arcminutes=203.8print(x.dump())x.arcseconds=42352.7print(x.dump())x.vector=(56,32)print(x.dump())
角度运算:
frommathimportpifromanglrimportAngleprint(Angle(pi/6)+Angle(2*pi/3))print(x*2+Angle(3*pi/4)/4+5*Angle(pi/3))print(-abs(+Angle(pi)))print(round(Angle(-75.87)))print(Angle(-4.3)<=Angle(pi/4)>Angle(0.118)==Angle(0.118))print(Angle(-870.3,"gradians").normalized())print(Angle(-870.3,"gradians").normalized(0))# same as aboveprint(Angle(-870.3,"gradians").normalized(0,2*pi))# same as aboveprint(Angle(-870.3,"gradians").normalized(-pi,pi))print(Angle(-870.3,"gradians").normalized(-pi,0))print(Angle(1,"degrees").angle_between_clockwise(Angle(0,"degrees")))print(Angle(1,"degrees").angle_between(Angle(0,"degrees")))print(Angle(0,"degrees").angle_within(Angle(-45,"degrees"),Angle(45,"degrees")))print(Angle(-1,"degrees").angle_within(Angle(-1,"degrees"),Angle(1,"degrees"),strictly_within=True))print(Angle(-1,"degrees").angle_to(Angle(180,"degrees")))print(Angle(0,"degrees").angle_to(Angle(180,"degrees")))
要将以上所有内容作为测试运行,只需在项目目录中运行python3 tests.py。
要求
此库需要运行Python3.2或更高版本。