Get Angle from 2 Positions

Get angle from 2 positions

Does this other answer help?

How to map atan2() to degrees 0-360

I've written it like this:

- (CGFloat) pointPairToBearingDegrees:(CGPoint)startingPoint secondPoint:(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 + bearingDegrees)); // correct discontinuity
return bearingDegrees;
}

Running the code:

CGPoint p1 = CGPointMake(10, 10);
CGPoint p2 = CGPointMake(20,20);

CGFloat f = [self pointPairToBearingDegrees:p1 secondPoint:p2];

And this returns 45.

Hope this helps.

Calculating the angle between a line and the x-axis

Assumptions: x is the horizontal axis, and increases when moving from left to right.
y is the vertical axis, and increases from bottom to top. (touch_x, touch_y) is the
point selected by the user. (center_x, center_y) is the point at the center of the
screen. theta is measured counter-clockwise from the +x axis. Then:

delta_x = touch_x - center_x
delta_y = touch_y - center_y
theta_radians = atan2(delta_y, delta_x)

Edit: you mentioned in a comment that y increases from top to bottom. In that
case,

delta_y = center_y - touch_y

But it would be more correct to describe this as expressing (touch_x, touch_y)
in polar coordinates relative to (center_x, center_y). As ChrisF mentioned,
the idea of taking an "angle between two points" is not well defined.

Calculate Angle of 2 points

It's just float angle = atan2(p1.y - p2.y, p1.x - p2.x).

Of course the return type is in radians, if you need it in degrees just do angle * 180 / PI

How to know the angle between two vectors?

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 the axes in order to calculate the angle in radians. Once you have that angle, then you can use the angle for the remainder of your calculations.

Note that since the angle is in radians, you need to use the math.pi instead of 180 degrees within your code. Also your test for more than 360 degrees (2*math.pi) is not needed. The test for negative (< 0) is incorrect as you then force it to 0, which forces the target to be on the x axis in the positive direction.

Your code to calculate the angle between the gun and the target is thus

myradians = math.atan2(targetY-gunY, targetX-gunX)

If you want to convert radians to degrees

mydegrees = math.degrees(myradians)

To convert from degrees to radians

myradians = math.radians(mydegrees)

Python ATAN2

The Python ATAN2 function is one of the Python Math function which is
used to returns the angle (in radians) from the X -Axis to the
specified point (y, x).

math.atan2()

Definition Returns the tangent(y,x) in radius.

Syntax
math.atan2(y,x)

Parameters
y,x=numbers

Examples
The return is:

>>> import math  
>>> math.atan2(88,34)
1.202100424136847
>>>

C# finding angle between 2 given points

I created this WinForm example that calculates the angle and distance of the mouse from the center of the form every time you move the mouse on the form. The result I display in a label.

Sample Image

The red dot in the center of the form is just a reference panel and has no relevance in the code.

    private void f_main_MouseMove(object sender, MouseEventArgs e)
{
Point center = new Point(378, 171);
Point mouse = this.PointToClient(Cursor.Position);

lb_mouseposition.Text = $"Mouse Angle: {CalculeAngle(center, mouse)} / Distance: {CalculeDistance(center, mouse)}";
}

private double CalculeAngle(Point start, Point arrival)
{
var deltaX = Math.Pow((arrival.X - start.X), 2);
var deltaY = Math.Pow((arrival.Y - start.Y), 2);

var radian = Math.Atan2((arrival.Y - start.Y), (arrival.X - start.X));
var angle = (radian * (180 / Math.PI) + 360) % 360;

return angle;
}

private double CalculeDistance(Point start, Point arrival)
{
var deltaX = Math.Pow((arrival.X - start.X), 2);
var deltaY = Math.Pow((arrival.Y - start.Y), 2);

var distance = Math.Sqrt(deltaY + deltaX);

return distance;
}

The angle is here shown in degrees varying from 0 to 359.
I hope this helps in calculating the angle between your two points.

Calculate angle (clockwise) between two points

Use the inner product and the determinant of the two vectors. This is really what you should understand if you want to understand how this works. You'll need to know/read about vector math to understand.

See: https://en.wikipedia.org/wiki/Dot_product and https://en.wikipedia.org/wiki/Determinant

from math import acos
from math import sqrt
from math import pi

def length(v):
return sqrt(v[0]**2+v[1]**2)
def dot_product(v,w):
return v[0]*w[0]+v[1]*w[1]
def determinant(v,w):
return v[0]*w[1]-v[1]*w[0]
def inner_angle(v,w):
cosx=dot_product(v,w)/(length(v)*length(w))
rad=acos(cosx) # in radians
return rad*180/pi # returns degrees
def angle_clockwise(A, B):
inner=inner_angle(A,B)
det = determinant(A,B)
if det<0: #this is a property of the det. If the det < 0 then B is clockwise of A
return inner
else: # if the det > 0 then A is immediately clockwise of B
return 360-inner

In the determinant computation, you're concatenating the two vectors to form a 2 x 2 matrix, for which you're computing the determinant.

How to calculate an angle from points?

You want the arctangent:

dy = ey - cy
dx = ex - cx
theta = arctan(dy/dx)
theta *= 180/pi // rads to degs

Erm, note that the above is obviously not compiling Javascript code. You'll have to look through documentation for the arctangent function.

Edit: Using Math.atan2(y,x) will handle all of the special cases and extra logic for you:

function angle(cx, cy, ex, ey) {
var dy = ey - cy;
var dx = ex - cx;
var theta = Math.atan2(dy, dx); // range (-PI, PI]
theta *= 180 / Math.PI; // rads to degs, range (-180, 180]
//if (theta < 0) theta = 360 + theta; // range [0, 360)
return theta;
}

Java: Calculating the angle between two points in degrees

you could add the following:

public float getAngle(Point target) {
float angle = (float) Math.toDegrees(Math.atan2(target.y - y, target.x - x));

if(angle < 0){
angle += 360;
}

return angle;
}

by the way, why do you want to not use a double here?



Related Topics



Leave a reply



Submit