简体   繁体   中英

Find the angle between any point inside a circle and the up vector direction

I have a circle with the up vector direction is Vector(0, -1) and a point, B which could be anywhere on that circle. I know its exact position inside the circle. The (0,0) point is located on the top left of the image.

How can I find the angle x between the point B and the up vector, relative to center of the circle?

在此处输入图片说明

I am developing a game in XNA and C#.

Update: I do not understand why this question is marked as not about programming. Anyway here's what I have done so far.

I can find the radian between two vectors

    private float radianBetweenVectors(Vector2 a, Vector2 b)
    {
        return (float)Math.Atan2(b.Y - a.Y, b.X - a.X);
    }

But I do not want to know about the location of the up vector on the sprite image if possible. The location here is the point at which it starts at 0 degree on the circle's circumference .

Update 2:

Once I have the angle, I want to obtain the rotation matrix:

Matrix rotMatrix = Matrix.CreateRotationZ(angle);

Now I have this matrix containing the rotation, I can ask XNA to transform the Up vector with this rotation:

moveDirection = Vector2.Transform(up, rotMatrix);

This I hope will take the original up direction, transform it with the rotation around the Z axis. I am still trying to figure out if this is the correct approach.

Find relative x and y coordinates of point B from center of the circle. (You can do this if you know the radius of the circle). If the circle diameter is the width of the sprite, just divide width by two to get radius. Then use arc tangent.

I'll try to help you out since I started XNA when I was relatively young and before I had any experience with vectors and you might be in the same position I was in 5 years ago.

If you don't understand the math behind it, you just treat point B as a point on a triangle that is formed from the 3 lines. I'll use (0,0) as the center of the circle, you can calculate the relative position of the center of the circle from the absolute position given radius. (If you can't do this part, you'll need to pay more attention in math class).

  • (0,0) to (0, By) will be the vertical leg.
  • (0, By) to (Bx, By) will be the horizontal leg
  • (Bx, By) to (0,0) will be the hypotenuse.

Then you use tangent with the rule tangent = opposite/adjacent. So you will know the distance of the opposite leg from the angle X is the distance from (0, By) to (Bx, By), the horizontal leg. The adjacent leg distance is (0,0) to (0, By). No need for distance formula, just subtract the coordinates. Now you have the value of tangent and want to find the value of X, so we'll use arctan. Math.Atan2(opposite/adjacent) . That will give you your answer in radians.

Edit: Oh and I remember you can draw the sprite so the center of the sprite will be rendered at the center of the sprite bitmap using the origin argument of draw. That way you don't have to worry about calcuating the center of the circle.

Speaking mathematically, if you have two vectors, lets supose A(0, -1) and B(222, 90) the angle X between the two vectors can be computed as cos(X)=(AB)/(||A||.||B||).

  1. A x B = (a1 x b1) + (a2 x b2) = 0x222 + (-1x90) = 0 - 90 = -90.

2.I. ||A|| = squareroot[0^2+(-1)^2] = sqrt[(-1)^2] = 1

2.II. ||B|| = squareroot[222^2+90^2] = sqrt(57384) ~= 239.55 (~= is approximately)

2.III. ||A||.||B|| = 1 x 239.55 ~= 239.55

Result: cos(X) ~= (-90)/(239.55) ~= X ~= cos^-1(X) ~= 1.955 radian.

For the vectors you gave above, the angle you looking for is approximately 1.955 radian.

Note: using a calculator, you will obtain the exact value, which is very close to the aproximate value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM