Questions tagged [atan2]

A function in many programming languages for computing the arctangent with two arguments

A function in many programming languages for computing the arctangent with two arguments

152 questions
132
votes
16 answers

How to map atan2() to degrees 0-360

atan2(y, x) has that discontinuity at 180° where it switches to -180°..0° going clockwise. How do I map the range of values to 0°..360°? here is my code: CGSize deltaPoint = CGSizeMake(endPoint.x - startPoint.x, endPoint.y - startPoint.y); float…
willc2
  • 38,991
  • 25
  • 88
  • 99
28
votes
3 answers

Are browser differences in the last digit of a JavaScript Math function (atan2) within spec?

I'm seeing differences between Firefox and Safari in the last digit of the output from Math#atan2. My code: Math.atan2(-0.49999999999999994, 0.8660254037844387) Safari (12.1.1) gives -0.5235987755982988 but Firefox (Mac/67.0) gives…
Rich Apodaca
  • 28,316
  • 16
  • 103
  • 129
18
votes
3 answers

Calculating angles between line segments (Python) with math.atan2

I am working on a spatial analysis problem and part of this workflow is to calculate the angle between connected line segments. Each line segment is composed of only two points, and each point has a pair of XY coordinates (Cartesian). Here is the…
Alex Tereshenkov
  • 3,340
  • 8
  • 36
  • 61
18
votes
5 answers

Robust atan(y,x) on GLSL for converting XY coordinate to angle

In GLSL (specifically 3.00 that I'm using), there are two versions of atan(): atan(y_over_x) can only return angles between -PI/2, PI/2, while atan(y/x) can take all 4 quadrants into account so the angle range covers everything from -PI, PI, much…
HuaTham
  • 7,486
  • 5
  • 31
  • 50
13
votes
3 answers

How to calculate wind direction from U and V wind components in R

I have U and V wind component data and I would like to calculate wind direction from these values in R. I would like to end up with wind direction data on a scale of 0-360 degrees, with 0° or 360° indicating a wind blowing to the north, 90°…
Emily
  • 859
  • 5
  • 14
  • 31
10
votes
6 answers

Calculate angle of touched point and rotate it in Android

Math has defeated me once again. This is such a simple task, but I can't manage to get it done. Scenario: I draw on a SurfaceView a round image. The user touches a point on image border and starts to drag it adround. I need to rotate the circle…
Alin
  • 14,809
  • 40
  • 129
  • 218
10
votes
4 answers

Convert atan2 value to standard 360-degree-system value

Lets say I'm using atan2 to get the angle between two vectors. atan2 gives a value in radians. I convert it to degrees using a built in function in Java. This gives me a value between 0 and 180 degrees or between 0 and -180 (the nature of atan2). Is…
user3150201
  • 1,901
  • 5
  • 26
  • 29
9
votes
4 answers

help to calculate atan2 properly

I need to calculate the angle between lines. I need to calculate atan. So I am using such code static inline CGFloat angleBetweenLinesInRadians2(CGPoint line1Start, CGPoint line1End) { CGFloat dx = 0, dy = 0; dx = line1End.x -…
yozhik
  • 4,644
  • 14
  • 65
  • 98
8
votes
1 answer

What exactly does (1.0e300 + pow(2.0, -30.0) > 1.0) do in STDC?

I have come across a function which computes atan(x) (the source is here). Reducing it to the core of my question and slightly reformatting it, they have something like that: static const double one = 1.0, huge =…
Binarus
  • 4,005
  • 3
  • 25
  • 41
7
votes
3 answers

Java: Determin angle between two points

OK firstly apologies as I know this kind of question has been asked before more than once. However even after looking at the other questions and answers I have been unable to get this to work for my situation. See below for an example: All I am…
Joss
  • 125
  • 1
  • 7
7
votes
1 answer

Polar-transform image in R

I am trying to transform an image (represented as a matrix) in R, into polar coordinate space with the origin being 0,0 (top left corner). Given the 215x215 matrix x which looks like: x0 = as.vector(col(x)) y0 = as.vector(row(x)) r = sqrt( (x0^2)…
Omar Wagih
  • 8,504
  • 7
  • 59
  • 75
7
votes
4 answers

Calculate atan2 without std functions or C99

I am calculating angles from a 3-axis accelerometer, but my compiler doesn't have a atan or atan2 function. It has a reserved memory slot, but it calls a function i can't find in any files. My compiler is Keil µVision 4 running the ARMCC…
Jeffa
  • 123
  • 1
  • 2
  • 7
7
votes
4 answers

JavaScript atan2() function not giving expected results

Normally, polar coordinates go from 0 to π to 2π (just before 2π really, as it equals 0 again). However, when using the JavaScript atan2() function, I'm getting a different, weird range: Cartesian X | Cartesian Y | Theta…
TerranRich
  • 1,263
  • 3
  • 20
  • 38
6
votes
5 answers

How to use atan2() in combination with other Radian angle systems

I am struggling with this in JavaScript, but this problem applies to many other languages/environments as well. I want to have one object rotating towards the position of another object, and I use: atan2(obj1.dy, obj1.dx) - obj2.getRotation() to…
Qqwy
  • 5,214
  • 5
  • 42
  • 83
6
votes
1 answer

Specify origin of Math.atan2

Math.atan2() is a very useful function for calculating angles. However, I cannot wrap my head around one thing: $(document).mousemove(function(event){ r = Math.atan2(event.pageY, event.pageX); deg = r * 180/Math.PI; console.log(deg); …
styke
  • 2,116
  • 2
  • 23
  • 51
1
2 3
10 11