finding angles 0-360
Math.atan limits you to the two rightmost quadrants on the unit circle. To get the full 0-360 degrees:
if x < 0 add 180 to the angle
else if y < 0 add 360 to the angle.
Your coordinate system is rotated and inverted compared to mine (and compared to convention). Positive x is to the right, positive y is up. 0 degrees is to the right (x>0, y=0, 90 degrees is up (x=0,y>0) 135 degrees is up and to the left (y>0, x=-y), etc. Where are your x- and y-axes pointing?
Get angle in terms of 360 degrees
You need to use a little bit more information than just the arcsin to figure this out. The reason for this is that arcsin will only return values between -π/2 and π/2 (-90 degrees and 90 degrees), as you already know.
So, to figure out the other part, you need to be aware of what quadrant you are in.
In quadrant 2, arcsin is between 90 and 0, the real angle is between 90 and 180. So if you are in quadrant 2, 180-calculated angle
=real angle
. (180-90=90, 180-0=180)
.
In quadrant 3, arcsin is between 0 and -90. The real angle is between 180 and 270. so again, 180-calculated angle
= real angle
. (180-0=180, 180-(-90)=270)
.
In quadrant 4, arcsin is between -90 and 0. The real angle is between 270 and 360. So 360+calculated angle
=real angle
. (360+(-90)=270, 360+0)=360)
.
The same is also true for quadrant 1, you just don't need to make the transformation. (0+360=360 (equivalent to 0), 90+360=450 (equivalent to 90) )
.
So, determine the quadrant first, and apply a rule based on that.
For an (x,y) coordinate, if x is positive and y is positive, you are quadrant 1. If x is negative and y is positive, you are quadrant 2. If x is negative and y is negative, you are quadrant 3. if x is positive and y is negative, you are quadrant 4.
So in your case, the distance from the origin is you (dX,dY)
coordinate pair, so something like this should do it:
//your original code...
var degrees = radians*(180/Math.PI); //convert from radians to degrees
//then
if (dX>=0 and dY>=0)
{//quadrant 1
//no transformation, do nothing
}
if (dX>=0 and dy<0)
{ //quadrant 4
degrees=360+degrees;
}
if (dX<0 and dy<0)
{ //quadrant 3
degrees=180-degrees;
}
if (dX<0 and dy>0)
{ //quadrant 2
degrees=180-degrees;
}
this.calculatedAngle = degrees;
How to map atan2() to degrees 0-360
(x > 0 ? x : (2*PI + x)) * 360 / (2*PI)
Python: Find the degree of the Angle [0:360] from cos(a) and sin(a) values
I gues you mean something like:
import math
angle = math.degrees(math.acos(df['cos']))
To really stay in [0, 360] you will have to check for negative cos and adapt the code like:
import math
a_acos = math.acos(df['cos'])
if df['sin'] < 0:
angle = math.degrees(-a_acos) % 360
else:
angle = math.degrees(a_acos)
more pythonic way to calculate angle from three x,y points return 0-360 degrees with "up" as 0?
This is not about Python, but about how the math works. The arccos
(also arcsin
, etc.) functions return a non-unique value, since the [-1, 1]
range of possible sine/cosine values maps to an interval which is 180 degrees wide. The arctan2
, which returns the angle in the range of -180 to 180 degrees, was introduced (into C, as atan2
) to fight the very same issue. Fortunately, it's an easy way to convert [-180, 180]
to [0, 360]
in Python:
angle = np.arctan2(...)
return np.degrees(angle) % 360.0 # the modulo operator does this job well
SKSpriteNode get the value of the angle from 0 to 360 degrees
My recommendation is that you should not do what you're suggesting. If the rotation is coming out negative, it's because it is negative. However, you can do this if you really want to: If the result is negative, add 360 to it. We can readily write a CGFloat extension to take care of that:
extension CGFloat {
var toDegreesNormalized : CGFloat {
var deg = self * 180 / .pi
return deg >= 0 ? deg : deg + 360
}
}
Now your code becomes
childNode(withName: "wheel")?.zRotation.toDegreesNormalized
calculate angle between 2 vectors return value in degrees between 0 and 360
You need to "rotate" your mouse.x and mouse.y coordinates about 90° (=swap y, x with -x, y) and add an offset of 180° (= Math.PI
):
let base = {x: 100, y: 100};
document.addEventListener('mousemove', event => { let angle = Math.PI + Math.atan2(-event.x + base.x, event.y - base.y); console.log(angle * 180 / Math.PI);});
<div style="position: absolute; top: 100px; left: 100px">x</div>
Related Topics
Instead of Downloading, Files Are Opening in Browser
How to Save .Xlsx Data to File as a Blob
Amount of All Goals of One Team (From Json)
How to Delete Rows of Data Using Sequelize
How to Disable Clicking Inside Div
Javascript Blob Filename Without Link
Accessing Ejs Variable in JavaScript Logic
How to Run HTML File on Localhost
Javascript: Search String in Array Then Count Occurrences
Bootstrap Slides Lagging While Using Multi Item Carousel Using Angular 6
How to Add Node Module to Angular Project If Is Not Schematics Enabled
Angular: Toggle Text of Button Based on Boolean Value in Model
How to Set Image to Fit Width of the Page Using Jspdf
Correct Way to Push into State Array
Draw a Connecting Line Between Two Elements