Heading from Point A to Point B in 2D space? - math

I'm working on a project which requires me to calculate the heading from a variable point A to a variable point B in 0 - 360 degrees to let the object at point A face point B.
Now, I'm unsure on how to achieve this, I googled but didn't find any good solution.
How would I calculate the heading from point A to point B in 2D space in any situation?

In a language such as C or C++ you might use the atan2 function, which calculates the arctangent of y/x over four quadrants, taking the signs of x and y into account.
If A is at (x1, y1) and B is at (x2, y2), then the heading in radians is given by:
theta_radians = atan2(y2 - y1, x2 - x1);
The range of theta_radians is -π to +π. You can convert this to degrees in the range 0 to 360 as follows:
theta_degrees = (theta_radians + M_PI) * 360.0 / (2.0 * M_PI);
$ man atan2

It's trig. You know the position of the two points and you can use them to make a right triangle. From that you can use SOH-CAH-TOA to find the angle you're interested in. Then from there you need to determine which quadrant the triangle is in and offset the computed angle appropriately.

Related

I have a dart board. How can I assign points for the different sections?

PLEASE do not just post a solution to my problem. For me, this is all about understanding how to do this and be able to explain to myself and others how this and that makes it all work!
I have a dart board I created with turtle. I can post it if someone really wants to see it.
Now, I need to create a function that will create a random spot on the board to hit, then incorporate the point value for that spot. The random point is simple. But is there a way that I can assign the correct value to an AREA without having to name EVERY coordinate one by one?
Say your dart board is centered at (x0, y0), and you have a dart at (x, y). You need to translate your dart into polar coordinates (phi, r):
r = sqrt((x - x0) ** 2, (y - y0) ** 2)
phi = math.atan2(y, x)
Then figure out whether r makes your dart is in center, inner, mid or outer ring, and in which section of the circle your phi lies.
What you need is a reverse transform, one that given x,y coordinates can identify which area it belongs to.
The best way to deal with this problem is to think in terms of coordinate systems. The area of a dart board is specified by its angle and radius. You must convert your x,y coordinates to an angle and radius, then determining the area it falls within will be simple.
Determining the angle is best done with an arctan2 function, which can directly convert an x,y offset into an angle. The radius is a simple sqrt(x**2 + y**2) once you have subtracted the center point from x,y.
good ol' Pythagoras
Let the point (x1, y1) be the center of the dart board that is used from the constructor. and let (x2, y2) be the random point on the board you find.
Use this formula to find d the distance from the center. Then you just need a few if statements
if 0 <= d || d <= 2:
# area 0
elif d < 2 || 4 <= d:
# area 1
elif d < 4 || 6 <= d:
# area 2

calculate angle from vector to coord

I am breaking my head trying to find an appropriate formula to calculate a what sounds to be an easy task but in practice is a big mathematical headache.
I want to find out the offset it needs to turn my vector's angle (X, Y, Angle) to face a coord ( X, Y )
My vector won't always be facing 360 degrees, so i need that as a variable as well..
Hoping an answer before i'm breaking my pc screen.
Thank you.
input
p1 = (x1,y1) point1 (vector origin)
p2 = (x2,y2) point2
a1 = 360 deg direction of vector
assuming your coodinate system is: X+ is right Y+ is up ang+ is CCW
your image suggest that you have X,Y mixed up (angle usually start from X axis not Y)
da=? change of a1 to match direction of p2-p1
solution 1:
da=a1-a2=a1-atanxy(x2-x1,y1-y1)
atanxy(dx,dy) is also called atan2 on some libs just make sure the order of operands is the right one
you can also use mine atanxy in C++
it is 4 quadrant arctangens
solution 2:
v1=(cos(a1),sin(a1))
v2=(x2-x1,y2-y1)
da=acos(dot(v1,v2)/(|v1|*|v2|))
or the same slightly different
v1=(cos(a1),sin(a1))
v2=(x2-x1,y2-y1)
v2/=|v2| // makes v2 unit vector, v1 is already unit
da=acos(dot(v1,v2))
so:
da=acos((cos(a1)*(x2-x1)+sin(a1)*(y2-y1)/sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)));
[notes]
just change it to match your coordinate system (which you did not specify)
use radians or degrees according to your sin,cos,atan dependencies ...
The difference between the vectors is also a vector.
Then calculate the tangens (y part / x part) and invert it to an angle.
Of course use the sign of y if x = 0.
if the coord to face is (x2 ,y2)
deltaY = y2 - y1
deltaX = x2 - x1
You have the angle in degrees between the two points using this formula...
angleInDegrees = arctan(deltaY / deltaX) * 180 / PI
subtract the original angle of your vector and you will get the correct offset!

How to find placement of a point in degrees on a circle's circumference given coordinates

Who is a math wiz and wants to check my math?
I'm trying to find the placement of a point(x,y) on the circumference of a circle in degrees. If I use x=radius*cos(Q)+xOrigin, I end up with arccos((x/2)-xOrigin) = Q which gives me the angle in radians. Then I go D = Q(180/Pi) to get degrees, but I keep coming up about 3-5 degrees off of target.
What's up with that?
Your formula for Q should be
Q = acos((x-xOrigin)/radius)
Note that acos returns an angle between 0 and pi; if the angle is in the third or fourth quadrant you'll need to subtract the angle from 2 * pi.
Without knowing the actual values that are coming up wrong, it's hard to tell if that's the only problem. Your conversion from radians to degrees is correct.
Most modern programming languages have atan2() somewhere, which takes a rise and a run and spits out the angle in radians in all four quadrants.

Triangulating coordinates with an equation

Ok, I know this sounds really daft to be asking here, but it is programming related.
I'm working on a game, and I'm thinking of implementing a system that allows users to triangulate their 3D coordinates to locate something (eg for a task).
I also want to be able to let the user make the coordinates of the points they are using for triangulation have user-determined coordinates (so the location's coordinate is relative, probably by setting up a beacon or something).
I have a method in place for calculating the distance between the points, so essentially I can calculate the lengths of the sides of the triangle/pyramid as well as all but the coordinate I am after.
It has been a long time since I have done any trigonometry and I am rusty with the sin, cos and tan functions, I have a feeling they are required but have no clue how to implement them.
Can anyone give me a demonstration as to how I would go about doing this in a mathematical/programatical way?
extra info:
My function returns the exact distance between the two points, so say you set two points to 0,0,0 and 4,4,0 respectively, and those points are set to scale(the game world is divided into a very large 3d grid, with each 'block' area being represented by a 3d coordinate) then it would give back a value at around 5.6.
The key point about it varying is that the user can set the points, so say they set a point to read 0,0,0, the actual location could be something like 52, 85, 93. However, providing they then count the blocks and set their other points correctly (eg, set a point 4,4,0 at the real point 56, 89, 93) then the final result will return the relative position (eg the object they are trying to locate is at real point 152, 185, 93, it will return the relative value 100,100,0). I need to be able to calculate it knowing every point but the one it's trying to locate, as well as the distances between all points.
Also, please don't ask why I can't just calculate it by using the real coordinates, I'm hoping to show the equation up on screen as it calculates the result.7
Example:
Here is a diagram
Imagine these are points in my game on a flat plain.
I want to know the point f.
I know the values of points d and e, and the sides A,B and C.
Using only the data I know, I need to find out how to do this.
Answered Edit:
After many days of working on this, Sean Kenny has provided me with his time, patience and intellect, and thus I have now got a working implementation of a triangulation method.
I hope to place the different language equivalents of the code as I test them so that future coders may use this code and not have the same problem I have had.
I spent a bit of time working on a solution but I think the implementer, i.e you, should know what it's doing, so any errors encountered can be tackled later on. As such, I'll give my answer in the form of strong hints.
First off, we have a vector from d to e which we can work out: if we consider the coordinates as position vectors rather than absolute coordinates, how can we determine what the vector pointing from d to e is? Think about how you would determine the displacement you had moved if you only knew where you started and where you ended up? Displacement is a straight line, point A to B, no deviation, not: I had to walk around that house so I walked further. A straight line. If you started at the point (0,0) it would be easy.
Secondly, the cosine rule. Do you know what it is? If not, read up on it. How can we rearrange the form given in the link to find the angle d between vectors DE and DF? Remember you need the angle, not a function of the angle (cos is a function remember).
Next we can use a vector 'trick' called the scalar product. Notice there is a cos function in there. Now, you may be thinking, we've just found the angle, why are we doing it again?
Define DQ = [1,0]. DQ is a vector of length 1, a unit vector, along the x-axis. Which other vector do we know? Do we know of two position vectors?
Once we have two vectors (I hope you worked out the other one) we can use the scalar product to find the angle; again, just the angle, not a function of it.
Now, hopefully, we have 2 angles. Could we take one from the other to get yet another angle to our desired coordinate DF? The choice of using a unit vector earlier was not arbitrary.
The scalar product, after some cancelling, gives us this : cos(theta) = x / r
Where x is the x ordinate for F and r is the length of side A.
The end result being:
theta = arccos( xe / B ) - arccos( ( (A^2) + (B^2) - (C^2) ) / ( 2*A*B ) )
Where theta is the angle formed between a unit vector along the line y = 0 where the origin is at point d.
With this information we can find the x and y coordinates of point f relative to d. How?
Again, with the scalar product. The rest is fairly easy, so I'll give it to you.
x = r.cos(theta)
y = r.sin(theta)
From basic trigonometry.
I wouldn't advise trying to code this into one value.
Instead, try this:
//pseudo code
dx = 0
dy = 0 //initialise coordinates somehow
ex = ex
ey = ey
A = A
B = B
C = C
cosd = ex / B
cosfi = ((A^2) + (B^2) - (C^2)) / ( 2*A*B)
d = acos(cosd) //acos is a method in java.math
fi = acos(cosfi) //you will have to find an equivalent in your chosen language
//look for a method of inverse cos
theta = fi - d
x = A cos(theta)
y = A sin(theta)
Initialise all variables as those which can take decimals. e.g float or double in Java.
The green along the x-axis represents the x ordinate of f, and the purple the y ordinate.
The blue angle is the one we are trying to find because, hopefully you can see, we can then use simple trig to work out x and y, given that we know the length of the hypotenuse.
This yellow line up to 1 is the unit vector for which scalar products are taken, this runs along the x-axis.
We need to find the black and red angles so we can deduce the blue angle by simple subtraction.
Hope this helps. Extensions can be made to 3D, all the vector functions work basically the same for 3D.
If you have the displacements from an origin, regardless of whether this is another user defined coordinate or not, the coordinate for that 3D point are simply (x, y, z).
If you are defining these lengths from a point, which also has a coordinate to take into account, you can simply write (x, y, z) + (x1, y1, z1) = (x2, y2, z2) where x2, y2 and z2 are the displacements from the (0, 0, 0) origin.
If you wish to find the length of this vector, i.e if you defined the line from A to B to be the x axis, what would the x displacement be, you can use Pythagoras for 3D vectors, it works just the same as with 2D:
Length l = sqrt((x^2) + (y^2) + (z^2))
EDIT:
Say you have a user defined point A (x1, y1, z1) and you want to define this as the origin (0,0,0). You have another user chosen point B (x2, y2, z2) and you know the distance from A to B in the x, y and z plane. If you want to work out what this point is, in relation to the new origin, you can simply do
B relative to A = (x2, y2, z2) - (x1, y1, z1) = (x2-x1, y2-y1, z2-z1) = C
C is the vector A>B, a vector is a quantity which has a magnitude (the length of the lines) and a direction (the angle from A which points to B).
If you want to work out the position of B relative to the origin O, you can do the opposite:
B relative to O = (x2, y2, z2) + (x1, y1, z1) = (x1+x2, y1+y2, z1+z2) = D
D is the vector O>B.
Edit 2:
//pseudo code
userx = x;
usery = y;
userz = z;
//move origin
for (every block i){
xi = xi-x;
yi = yi - y;
zi = zi -z;
}

Angle of a vector pointing from A to B

I'm not the best in Maths, but for what I am doing now I need to calculate the angle of the vector which is shown as arrow in the picture below:
I have a point A and a point B in a 2D plane. I need to calculate the following:
The angle in which the arrow must be rotated in order to point to B
atan2(yB-yA, xB-xA), assuming your library has atan2. Otherwise you need to use atan, which will return the correct answer if B is to the right of A, and will be 180 degrees off otherwise. Also note that the return value is in radians, you can convert radians to degrees by multiplying by 180/pi if necessary.
Wikipedia has a detailed explanation of the geometry.
arctan((A.y - B.y) / (A.x - B.x)) and note the special case where A.x = B.x

Resources