Add distance onto coordinate - math

I have a unit vector, a distance and a coordinate and I would like to calculate the new coordinate given by adding the distance onto the coordinate in the given direction. How do I do this?

Multiply the vector by the distance then add the resulting vector to the point.

Here's some pseudocode, assuming you're using Cartesian coordinates.
new_coord.x = distance * unit.x + coord.x
new_coord.y = distance * unit.y + coord.y

If with a unit vector, you mean a vector with distance 1. You can find the coordinate bij multiplying all coordinates with the distance.
V = V unit * distance
V unit = (1/2 sqrt(3), 1/2)
distance = 6
==>
V = (3 sqrt(3), 3)

Related

Find the next 3D point given a starting point, a orientation quaternion, and a distance travelled

What is the formula I need to use to find the second 3D point (P1) given:
The first point P0 = [x0, y0, z0]
An orientation quaternion Q0 = [q0, q1, q2, q3]
The distance traveled S
I'm guessing that the distance traveled S needs to be split up into it's constituent X, Y and Z components. Is there an easy way to do this using quaternions?
Components of direction vector (forward-vector) are:
x = 2 * (q1*q3 + q0*q2)
y = 2 * (q2*q3 - q0*q1)
z = 1 - 2 * (q1*q1 + q2*q2)
This formula is calculated from Quaternion-to-Matrix (below) with multiplication by (0,0,1) vector.
Normalize D=(x,y,z) if it is not unit, and calculate P_New.x= P0.x + S * D.x and other components.
To get up- and left- vector of orientation (perhaps your orientation refers to another base frame orientation - OX or OY as forward), use another columns of the matrix cited below:
Link:
Quaternion multiplication and orthogonal matrix multiplication can both be used to represent rotation. If a quaternion is represented by qw + i qx + j qy + k qz , then the equivalent matrix, to represent the same rotation, is:
1 - 2*qy2 - 2*qz2 2*qx*qy - 2*qz*qw 2*qx*qz + 2*qy*qw
2*qx*qy + 2*qz*qw 1 - 2*qx2 - 2*qz2 2*qy*qz - 2*qx*qw
2*qx*qz - 2*qy*qw 2*qy*qz + 2*qx*qw 1 - 2*qx2 - 2*qy2

Given 2 vector and 2 angle how to find the 3rd vector

It seems to be a very easy question but I just can't figure it out ...
as shown on the below graph:
Supposing we know :
Vector (X,Y)
Vector (X1,Y1)
Angle a
How can I get the vector (?,?) in Unity ?
Many Thanks in advance.
Subtract X1,Y1 from all coordinates.
XX = X - X1
YY = Y - Y1
Let (DX, DY) is vector between (XX, YY) and unknown point.
This vector is perpendicular to (XX, YY), so scalar product is zero.
And length of this vector is equal to length of (XX, YY) multiplied by tangent of angle.
So equation system is
DX * XX + DY * YY = 0
DX^2 + DY^2 = (XX^2 + YY^2) * Tan^2(Alpha)
Solve this system for unknowns (DX, DY) (there are two solutions in general case), then calculate unknown coordinates as (X + DX, Y + DY)
Not totally sure if there is a more efficient method to do this, but it will work.
First you need to find the magnitude of the distance vector between X,Y and X1,Y1. We will call this Dist1.
Dist1 = Vector2.Distance(new Vector2(X,Y), new Vector2(X1,Y1));
Using this distance, we can find the magnitude of the vector for the line going to X?,Y? which we will call DistQ.
DistQ = Dist1 / Mathf.Cos(a * Mathf.Deg2Rad);
You now need to find the angle of this line relative to the overall coordinate plane which will create a new triangle with X?Y? and the x-axis.
angle = Mathf.Atan2((Y - Y1), (X - X1)) * Mathf.Rad2Deg - a;
Now we can use more trig with the DistQ hypotenuse and this new angle to find the X?(XF) and Y?(YF) components relative to X1 and Y1, which we will add on to get the final vector components.
XF = DistQ * Mathf.Cos(angle * Mathf.Deg2Rad) + X1;
YF = DistQ * Mathf.Sin(angle * Mathf.Deg2Rad) + Y1;

Cone from direction vector

I have a normalized direction vector (from a 3d position to a light position) and I would like this vector to be rotated by some angle so I can create a "cone".
Id like to simulate cone tracing by using the direction vector as the center of the cone and create an X number of samples to create more rays to sample from.
What I would like to know is basically the math behind:
https://docs.unrealengine.com/latest/INT/BlueprintAPI/Math/Random/RandomUnitVectorinCone/index.html
Which seems to do exactly what Im looking for.
1) Make arbitrary vector P, perpendicular to your direction vector D.
You can choose component with max magnitude, exchange it with middle-magnitude component, negate it, and make min magnitude component zero.
For example, if z- component is maximal and y-component is minimal, you may make such P:
D = (dx, dy, dz)
p = (-dz, 0, dx)
P = Normalize(p) //unit vector
2) Make vector Q perpendicular both D and P through vector product:
Q = D x P //unit vector
3) Generate random point in the PQ plane disk
RMax = Tan(Phi) //where Phi is cone angle
Theta = Random(0..2*Pi)
r = RMax * Sqrt(Random(0..1))
V = r * (P * Cos(Theta) + Q * Sin(Theta))
4) Normalize vector V
Note that distribution of vectors is slightly non-uniform on the sphere segment.(it is uniform on the plane disk). There are methods to generate uniform distribution on the sphere but some work needed to apply them to segment (my first attempt before edit was wrong).
Edit: Modification to make sphere-uniform distribution (not checked thoroughly)
RMax = Tan(Phi) //where Phi is cone angle
Theta = Random(0..2*Pi)
u = Random(Cos(Phi)..1)
r = RMax * Sqrt(1 - u^2)
V = r * (P * Cos(Theta) + Q * Sin(Theta))

Calculate cartesian coordinates of a point on a perpendicular of a line

my math is a bit rusty seeing as how it's been years.
I added a small example graph to make this more clear.
I have 2 points in a cartesian coordinate system. On this line I take a random point, in this example the center. Now I draw the perpendicular line on that point. I want to know the coordinate of a point on that line, a certain known distance from that point. What's the formula to calculate this?
Graph: http://i44.tinypic.com/9vcjlf.png
In short
Known constants:
the coordinates of points A, B and C.
the lengths t1, t2, t3
Required:
the coordinate of the colored points
Thanks in advance
If these are the coordinates of A, B:
A = (Ax, Ay)
B = (Bx, By)
Then the vector from A to B is given by:
vector AB = (Bx-Ax, By-Ay) = (BAx, BAy)
And the unit vector (the vector of length 1) which points in the same direction is given by:
(BAx, BAy)
unit vector AB = ------------------, where length = sqrt(BAx^2 + BAy^2)
length
Now, the unit vector which is perpendicular to AB is given by:
(-BAy, BAx)
unit vector perpendicular to AB = -------------
length
There are two possible unit vectors perpendicular to AB. The one shown above is what you'd obtain by
rotating the unit vector AB counterclockwise by 90 degrees.
Given the above calculations, here are the desired coordinates:
coordinate at t1 = (Bx, By) + t1 * (unit vector perpendicular to AB)
coordinate at t2 = (Bx, By) + t2 * (unit vector perpendicular to AB)
coordinate at t3 = (Bx, By) - t3 * (unit vector perpendicular to AB)
To be explicit,
(Bx + t1*(-By+Ay), By + t1*(Bx-Ax))
coordinate at t1 = -------------------------------------
sqrt((Bx-Ax)^2 + (By-Ay)^2)
The others formulas are very similar.

Converting polar coordinates to rectangular coordinates

Convert angle in degrees to a point
How could I convert an angle (in degrees/radians) to a point (X,Y) a fixed distance away from a center-point.
Like a point rotating around a center-point.
Exactly the opposite of atan2 which computes the angle of the point y/x (in radians).
Note: I kept the original title because that's what people who do not understand will be searching by!
Let the fixed distance be D, then X = D * cos(A) and Y = D * sin(A), where A is the angle.
If center-point (Xcp, Ycp) isn't the origin you also need to add it's coordinates to (X,Y) i.e. X = Xcp + D * cos(A) and Y = Ycp + D * sin(A)
What PolyThinker said.
Also, if you need the distance from the origin, it's sqrt(x^2 + y^2).
t = angle
r = radius (fixed distance)
x = rcost
y = rsint

Resources