How to find points of certain distance on a circle perimeter? - math

Suppose, (x1, y1) is a point on the perimeter of a circle (x-420)^2 + (y-540)^2 = 260^2 what are the two points on the circle perimeter of distance d(euclidean) from the point (x1, y1)

Using trig
Assuming you are using a programming language. The answer is using pseudo code.
Using radians the distance d along a circle can be expressed as an angle a computed as a = d / r (where r is the radius)
Given an arbitrary point on the circle. (x1-420)^2 + (y1-540)^2 = 260^2 (NOTE assumes x1, y1 are known) we can extract the center is x = 420, y = 540, and radius r = 260
The angular distance d is then a = d / 260.
Most languages have the function atan2 which will compute the angle of a vector, We can get the angle from the circle center to the arbitrary point as ang = atan2(y1 - 540, x1 - 420) (Note y first then x)
Thus the absolute angles from the arbitrary point {x1, y1} to the points d distance along the circle (ang1 , ang2) is computed as...
// ? represents known unknowns
x = 420
y = 540
r = 260
d = ?
x1 = ?
y1 = ?
ang = atan2(y1 - y, x1 - x)
ang1 = ang + d / r
ang2 = ang - d / r
And the coordinates of the points (px1, py1, px2, py2) computed as...
px1 = cos(ang1) * r + x
py1 = sin(ang1) * r + y
px2 = cos(ang2) * r + x
py2 = sin(ang2) * r + y
Vector algebra
The problem can also be solved using vector algebra and does not require the trig function atan2
Compute the unit vector representing the angle a = d / r and then with the circle at the origin, transform (rotate) the point on the circle using the unit vector in both directions. Translate the points back to the circles original position for the solution.

Related

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))

How to find points along arc if given initial point, ending point, random point + precision?

the precision is the number of points I want for my vector, from 0, the initial point of my arc, to the precision I want minus 1.
Code example in c++:
int precision = 20;
double pointInit[3] = {2,5,2};
double pointRandom[3] = {3,7,1};
double pointInit[3] = {0,-3,1};
std::vector<std::array<double,3>> pointArc;
std::array<double, 3> currentPoint;
// Fill the pointArc vector, from 0 (initial point) to precision -1 (ending point)
for (int i = 0 ; i < precision; i++)
{
// Find the value of the current point
// currentPoint[0] = ????;
// currentPoint[1] = ????;
// currentPoint[2] = ????;
pointArc.push_back(currentPoint);
}
EDIT : The arc I'm looking for is a circular arc
Use atan2() to find the angles of the endpoints with respect to the center, subtend the angle between them precision - 1 times, and convert the polar coordinates (use one of the endpoints to get the distance from the center) to rectangular form.
1) translate the three points so that P0 comes to the origin
2) consider the vectors P0P1 and P0P2 and form an orthonormal basis by the Gram-Schmidt process (this is easy)
3) in this new basis, the coordinates of the three points are (0, 0, 0), (X1, 0, 0), (X2, Y2, 0), and you have turned the 3D problem to 2D. (Actually X1=d(P0,P1) and X2, Y2 are obtained from the dot and cross products of P0P2 with P0P1 / X1)
The equation of a 2D circle through the origin is
x² + y² = 2Xc.x + 2Yc.y
Plugging the above coordinates, you easily solve the 2x2 system for Xc and Yc.
X1² = 2Xc.X1
X2² + Y2² = 2Xc.X2 + 2Yc.Y2
4) The parametric equation of the circle is
x = Xc + R cos(t)
y = Yc + R sin(t)
where R²=Xc²+Yc².
You can find the angles t0 and t2 corresponding to the endpoints with tan(t) = (y - Yc) / (x - Xc).
5) interpolate on the angle t0.(1-i/n) + t2.i/n, compute the reduced coordinates x, y from the parametric equation and apply the inverse transforms of 2) and 1).

Set vector2 coordinates by move distance and degree

I have a Vector2 in my 2D Game and what I would like to do now is set my vector2 x and y by calculating them using rotation in degrees
Do I need to use PI to calculate new X and Y coordinates then add move distance per second in order to get the correct coordinates?
Example : Lets say degree is 90, which means my gameobject would move forward,at 5 floating units per second, then Y would be 5,10,15 and if degree would be 180 then X would increase by 5 every second, this is simple, but how to do it for other degrees such as 38,268 etc?
The usual convention is that 0 degrees points in the positive X direction and as the angle increases you rotate the direction anti-clockwise. Your convention seems to be that 0 degrees points in the negative X direction and the angle increases clockwise, so first of all you must translate your angle, say alpha, into one with the usual convention, say beta
beta = 180.0 - alpha
Next, trigonometric functions assume radians which run from 0 to 2π rather than from 0 to 360, so you must translate beta into an angle in radians, say theta
theta = 2.0*PI*beta/360.0
Finally, cos(theta) gives the change in X for a move of 1 unit in the direction given by theta and sin(theta) gives the change in Y. So you need
X = X + D * cos(theta)
Y = Y + D * sintheta)
for a distance D. Using your convention this translates to
X = X + D * cos(2.0*PI*(180.0-alpha)/360.0)
Y = Y + D * sin(2.0*PI*(180.0-alpha)/360.0)

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