Getting angle back from a sin/cos conversion
atan2(s_y, s_x) should give you the correct angle. Maybe you have reversed the order of s_x and s_y. Also, you can use the acos and asin functions directly on s_x and s_y respectively.
atan2(s_y, s_x) should give you the correct angle. Maybe you have reversed the order of s_x and s_y. Also, you can use the acos and asin functions directly on s_x and s_y respectively.
It turns out that Wikipedia does have the best answer: // h = 1..12, m = 0..59 static double angle(int h, int m) { double hAngle = 0.5D * (h * 60 + m); double mAngle = 6 * m; double angle = Math.abs(hAngle – mAngle); angle = Math.min(angle, 360 – angle); return angle; } … Read more
Use modulo arithmetic: this.orientation += degrees; this.orientation = this.orientation % 360; if (this.orientation < 0) { this.orientation += 360; }
The tangent of the angle between two points is defined as delta y / delta x That is (y2 – y1)/(x2-x1). This means that math.atan2(dy, dx) give the angle between the two points assuming that you know the base axis that defines the co-ordinates. Your gun is assumed to be the (0, 0) point of … Read more
What you want to use is often called the “perp dot product”, that is, find the vector perpendicular to one of the vectors, and then find the dot product with the other vector. if(a.x*b.y – a.y*b.x < 0) angle = -angle; You can also do this: angle = atan2( a.x*b.y – a.y*b.x, a.x*b.x + a.y*b.y … Read more
You forgot to add the center point: result.Y = (int)Math.Round( centerPoint.Y + distance * Math.Sin( angle ) ); result.X = (int)Math.Round( centerPoint.X + distance * Math.Cos( angle ) ); The rest should be ok… (what strange results were you getting? Can you give an exact input?)
2D case Just like the dot product is proportional to the cosine of the angle, the determinant is proportional to its sine. So you can compute the angle like this: dot = x1*x2 + y1*y2 # Dot product between [x1, y1] and [x2, y2] det = x1*y2 – y1*x2 # Determinant angle = atan2(det, dot) … Read more
Note: all of the other answers here will fail if the two vectors have either the same direction (ex, (1, 0, 0), (1, 0, 0)) or opposite directions (ex, (-1, 0, 0), (1, 0, 0)). Here is a function which will correctly handle these cases: import numpy as np def unit_vector(vector): “”” Returns the unit … Read more
I’m going to assume you’re using C++, but the answer should be the same if you’re using C or Python. The function minAreaRect seems to give angles ranging from -90 to 0 degrees, not including zero, so an interval of [-90, 0). The function gives -90 degrees if the rectangle it outputs isn’t rotated, i.e. … Read more
Given y and x, the angle with the x axis is given by: atan2(y, x) // note that Y is first With (0.5, 0.5) the angle is: radians: In [2]: math.atan2(0.5, 0.5) Out[2]: 0.7853981633974483 degrees: In [3]: math.atan2(0.5, 0.5)*180/math.pi Out[3]: 45.0