atan2(y,x)在180°处具有不连续性,其顺时针切换到-180°..0°.
如何将值范围映射到0°.360°?
这是我的代码:
CGSize deltaPoint = CGSizeMake(endPoint.x - startPoint.x, endPoint.y - startPoint.y);
float swipeBearing = atan2f(deltaPoint.height, deltaPoint.width);
Run Code Online (Sandbox Code Playgroud)
在给定startPoint和endPoint两个XY点结构的情况下,我正在计算滑动触摸事件的方向.该代码适用于iPhone,但任何支持atan2f()的语言都可以.
感谢您的帮助,包括一般解决方案和代码.
更新:我将erikkallen的答案变成了一个具有漂亮的长变量名的函数,所以我将在6个月后理解它.也许它会帮助其他一些iPhone noob.
float PointPairToBearingDegrees(CGPoint startingPoint, CGPoint endingPoint)
{
CGPoint originPoint = CGPointMake(endingPoint.x - startingPoint.x, endingPoint.y - startingPoint.y); // get origin point to origin by subtracting end from start
float bearingRadians = atan2f(originPoint.y, originPoint.x); // get bearing in radians
float bearingDegrees = bearingRadians * (180.0 / M_PI); // convert to degrees
bearingDegrees = (bearingDegrees > 0.0 ? bearingDegrees : (360.0 + …Run Code Online (Sandbox Code Playgroud) 我在的输出的最后一位看到Firefox和Safari之间的差异Math#atan2。
我的代码:
Math.atan2(-0.49999999999999994, 0.8660254037844387)
Run Code Online (Sandbox Code Playgroud)
Safari(12.1.1)提供,-0.5235987755982988但Firefox(Mac / 67.0)提供-0.5235987755982987。
这当然是微小的差异。但是,似乎所有实现都应该在所有输入上产生相同的输出。例如,这样的差异可能导致if语句根据浏览器遵循不同的路径。
我看到的内容是否违反任何版本的ECMAScript规范?
在GLSL(特别是我正在使用的3.00)中,有两个版本
atan():atan(y_over_x)只能返回-PI/2,PI/2之间的角度,同时atan(y/x)可以考虑所有4个象限,因此角度范围涵盖-PI, PI,就像atan2()在C++中一样.
我想用第二个atan将XY坐标转换为角度.但是,atan()在GLSL中,除了不能处理的时候x = 0,还不是很稳定.特别是在x接近零的地方,除法会溢出,导致相反的结果角度(你得到的东西接近-PI/2,你可以得到大约PI/2).
什么是一个好的,简单的实现,我们可以在GLSL之上构建,atan(y,x)使其更强大?
我需要计算线之间的角度.我需要计算atan.所以我正在使用这样的代码
static inline CGFloat angleBetweenLinesInRadians2(CGPoint line1Start, CGPoint line1End)
{
CGFloat dx = 0, dy = 0;
dx = line1End.x - line1Start.x;
dy = line1End.y - line1Start.y;
NSLog(@"\ndx = %f\ndy = %f", dx, dy);
CGFloat rads = fabs(atan2(dy, dx));
return rads;
}
Run Code Online (Sandbox Code Playgroud)
但是我不能超过180度((在179度之后178-160 ...... 150等等).
我需要旋转360度.我该怎么做?怎么了?
maby这有助于:
//Tells the receiver when one or more fingers associated with an event move within a view or window.
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSArray *Touches = [touches allObjects];
UITouch *first = [Touches objectAtIndex:0]; …Run Code Online (Sandbox Code Playgroud) 数学再一次打败了我.这是一项非常简单的任务,但我无法完成任务.
场景:我在SurfaceView上绘制一个圆形图像.用户触摸图像边框上的一个点并开始将其拖动到周围.我需要根据用户的移动旋转圆形图像.我有两个重要的信息,图像中心X,Y坐标和触摸点坐标.

正如您在图像中看到的那样,用户触摸了一个点,根据我的绘制,触摸点角度应该在40左右.我无法正确计算它.
我尝试使用这个公式:
angle = Math.atan2(touchedY - centerY, touchedX - centerX) * 180 / Math.PI
Run Code Online (Sandbox Code Playgroud)
我无法理解我应该如何计算角度,就像现在一样,它不能正常工作并且值不好.例如,在图像的情况下,角度计算是-50.
感谢您的时间,任何信息都很乐意接受.
LE:其实我认为我犯了一个错误,如下所述.圈子应该是这样的:

我遇到了一个计算的函数atan(x)(源在这里).将它减少到我的问题的核心并稍微重新格式化,他们有类似的东西:
static const double one = 1.0,
huge = 1.0e300;
double atan(double x)
{
/* A lot of uninteresting stuff here */
if (ix < 0x3fdc0000) { /* |x| < 0.4375 */
if (ix < 0x3e200000) { /* |x| < 2^-29 */
if ((huge + x) > one) return x; /* raise inexact */
}
id = -1;
}
/* A lot of more uninteresting stuff */
}
Run Code Online (Sandbox Code Playgroud)
我对这条线if ((huge + x) ...应该做什么以及它是如何工作非常感兴趣.
根据评论,如果绝对值 …
通常情况下,极坐标从0到π到2个π(之前2个π真的,因为它再次等于0).但是,在使用JavaScript atan2()函数时,我会得到一个不同的,奇怪的范围:
Cartesian X | Cartesian Y | Theta (?)
===========================================================
1 | 0 | 0 (0 × π)
1 | 1 | 0.7853981633974483 (0.25 × π)
0 | 1 | 1.5707963267948966 (0.5 × π)
-1 | 1 | 2.356194490192345 (0.75 × π)
-1 | 0 | 3.141592653589793 (1 × π)
-1 | -1 | -2.356194490192345 (-0.75 × π)
0 | -1 | -1.5707963267948966 (-0.5 × π … 我试图将R中的图像(表示为矩阵)转换为极坐标空间,原点为0,0(左上角).鉴于215x215矩阵x看起来像:

x0 = as.vector(col(x)) y0 = as.vector(row(x)) r = sqrt( (x0^2) + (y0^2) )#x a = atan(y0/x0)#y m = as.matrix(data.frame(y=a, x=r)) m = round(m) m[m>215] = NA m[m==0] = NA xn = x[m] xn = matrix(xn, 215, 215)
但是,xn看起来像:

当我期待这个:

知道我做错了什么吗?
好的,首先道歉,因为我知道这个问题不止一次被提出过.然而,即使在查看了其他问题和答案之后,我也无法将其用于我的情况.请参阅下面的示例:

我只是试图解决P1和P2之间的角度,假设0度如上所示,这样我就可以在正确的方向上指向2之间的箭头.所以我做这样的事......
Point p1 = new Point(200,300); Point p2 = new Point(300,200);
double difX = p2.x - p1.x; double difY = p2.y - p1.y;
double rotAng = Math.toDegrees(Math.atan2(difY,difX));
Run Code Online (Sandbox Code Playgroud)
它出现为:-45,它应该是45?然而,我不认为它会返回负面结果,例如,如果我将P1更改为300,300(低于P2),则角度应为0,但返回为-90.
所以我只是想知道是否有人可以指出我做错了什么来计算这个,或者甚至可以这样做呢?
我正在研究空间分析问题,该工作流程的一部分是计算连接线段之间的角度.
每个线段仅由两个点组成,每个点都有一对XY坐标(笛卡尔坐标).这是GeoGebra的图像.我总是对在0到180范围内获得正角度感兴趣.但是,根据输入线段中顶点的顺序,我得到所有类型的角度.

我使用的输入数据以坐标元组的形式提供.根据顶点创建顺序,每个线段的最后/结束点可以不同.以下是Python代码中的一些案例.我得到它们的线段的顺序是随机的,但在元组的元组中,第一个元素是起点,第二个元素是终点.DE线段,例如,本来((1,1.5),(2,2))和(1,1.5)为起点,因为它在坐标元组第一的位置.
但是,我需要确保我会得到的相同的角度DE,DF,并ED,DF依此类推.
vertexType = "same start point; order 1"
#X, Y X Y coords
lineA = ((1,1.5),(2,2)) #DE
lineB = ((1,1.5),(2.5,0.5)) #DF
calcAngle(lineA, lineB,vertexType)
#flip lines order
vertexType = "same start point; order 2"
lineB = ((1,1.5),(2,2)) #DE
lineA = ((1,1.5),(2.5,0.5)) #DF
calcAngle(lineA, lineB,vertexType)
vertexType = "same end point; order 1"
lineA = ((2,2),(1,1.5)) #ED
lineB = ((2.5,0.5),(1,1.5)) #FE
calcAngle(lineA, lineB,vertexType)
#flip lines order
vertexType = …Run Code Online (Sandbox Code Playgroud) atan2 ×10
math ×3
angle ×2
coordinates ×2
javascript ×2
android ×1
c ×1
c++ ×1
degrees ×1
geometry ×1
glsl ×1
ieee-754 ×1
java ×1
objective-c ×1
python ×1
quartz-2d ×1
r ×1
radians ×1
trigonometry ×1