最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

c# - Rotate a 3D Vector around Axis and degrees - Stack Overflow

matteradmin3PV0评论

I try to rotate a Vector (0,0,-1) around an axis (Y) (0,1,0) with specific degrees (90°) using the System.Numerics Libary from .NET

My Problem is that i get a wrong output and i dont get the Problem. The Output from the Code is: Original vector: <0 0 -1> rotated vector: <-0,99999994 0 -5,9604645E-08>

If i Rotate the Vector (0,0,-1) Around (0,1,0) with 990 degrees the output should be (-1,0,0)

using System.Numerics;

class Program
{
 static void Main()
    {

        Vector3 vector = new Vector3(0,0,-1);
        Vector3 rotateAxis = new Vector3(0,1,0);
        float angle = 90;

        //Create Radians
        float radians = (float)(angle *Math.PI/180);

        Quaternion rotation = Quaternion.CreateFromAxisAngle(rotateAxis, radians);

        // Rotate Vector
        Vector3 rotatedVector = Vector3.Transform(vector,rotation);

        Console.WriteLine($"Original vector: {vector}");
        Console.WriteLine($"rotated vector: {rotatedVector}");
    }
}

I try to rotate a Vector (0,0,-1) around an axis (Y) (0,1,0) with specific degrees (90°) using the System.Numerics Libary from .NET

My Problem is that i get a wrong output and i dont get the Problem. The Output from the Code is: Original vector: <0 0 -1> rotated vector: <-0,99999994 0 -5,9604645E-08>

If i Rotate the Vector (0,0,-1) Around (0,1,0) with 990 degrees the output should be (-1,0,0)

using System.Numerics;

class Program
{
 static void Main()
    {

        Vector3 vector = new Vector3(0,0,-1);
        Vector3 rotateAxis = new Vector3(0,1,0);
        float angle = 90;

        //Create Radians
        float radians = (float)(angle *Math.PI/180);

        Quaternion rotation = Quaternion.CreateFromAxisAngle(rotateAxis, radians);

        // Rotate Vector
        Vector3 rotatedVector = Vector3.Transform(vector,rotation);

        Console.WriteLine($"Original vector: {vector}");
        Console.WriteLine($"rotated vector: {rotatedVector}");
    }
}
Share Improve this question asked Nov 18, 2024 at 11:57 SciroccoXCodingSciroccoXCoding 31 silver badge1 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 1

The result is correct. You didn't get an precise result because the rotation angle itself is not precise. You can round the result to eliminate some bias. For example:

rotatedVector = rotatedVector.Round();

public static class Vector3Extension
{
    public static Vector3 Round(this Vector3 v, int decimals = 3)
    {
        return new Vector3(
            (float)Math.Round(v.X, decimals),
            (float)Math.Round(v.Y, decimals),
            (float)Math.Round(v.Z, decimals));
    }
}
Post a comment

comment list (0)

  1. No comments so far