c# - C# 中的矢量投影/拒绝?

标签 c# vector 3d unity-game-engine

我正在尝试为 vector rejection 实现一个函数在 C# 中。即:

enter image description here

我一直在尝试用 C# 编写这个公式,但由于某种原因它总是返回零。这是我到目前为止所拥有的:

private Vector3 Projection(Vector3 vectorA, Vector3 vectorB) {
    Vector3 a2 = Vector3.Scale ( Divide (Vector3.Scale (vectorA, vectorB), Vector3.Scale (vectorB, vectorB)), vectorB);
    return a2;
}

private Vector3 Rejection(Vector3 vectorA, Vector3 vectorB) {
    Vector3 a2 = vectorA - Projection(vectorA, vectorB);
    return a2;
}

private Vector3 Divide(Vector3 a, Vector3 b) {
    Vector3 c = new Vector3 ();
    c.x = a.x / b.x;
    c.y = a.y / b.y;
    c.z = a.z / b.z;
    return c;
}

private void Example() {
    Vector3 a = new Vector3 (5, 5, 0);
    Vector3 b = new Vector3 (0, 10, 0);
    Vector3 c = Rejection(a, b); // Returns (NaN, 0, NaN)
}

// The Vector3 class represents a 3D vector and it's x, y and z components are of float type. It's [scale method][3] multiplies two vectors component wise.

示例:

假设向量 A 是施加到沿地面移动的物体的力,向量 B 是重力(垂直于地面)。当矢量 A 应用于物体时,它移动的轨迹应该是垂直于重力的第三个矢量,可以将其视为 A 垂直于 B 行进。这就是 A 与 B 的排斥。

enter image description here

最佳答案

符号a·b代表vector dot product结果是一个标量值。

所以你需要的是这样的代码:

public struct Vector3
{
    public readonly double x, y, z;
    public Vector3(double x, double y, double z)
    {
        this.x=x;
        this.y=y;
        this.z=z;
    }
    public double Dot(Vector3 other)
    {
        return x*other.x+y*other.y+z*other.z;
    }
    public Vector3 Scale(double factor)
    {
        return new Vector3(factor*x, factor*y, factor*z);
    }
    public Vector3 Add(Vector3 other)
    {
        return new Vector3(x+other.x, y+other.y, z+other.z);
    }
    public static Vector3 operator+(Vector3 a, Vector3 b) { return a.Add(b); }
    public static Vector3 operator-(Vector3 a) { return a.Scale(-1); }
    public static Vector3 operator-(Vector3 a, Vector3 b) { return a.Add(-b); }
    public static Vector3 operator*(double f, Vector3 a) { return a.Scale(f); }
    public static Vector3 operator/(Vector3 a, double d) { return a.Scale(1/d); }
    public static double operator*(Vector3 a, Vector3 b) { return a.Dot(b); }

    public Vector3 Projection(Vector3 other)
    {
        // (scalar/scalar)*(vector) = (vector)
        return (other*this)/(other*other)*other;
    }
    public Vector3 Rejection(Vector3 other)
    {
        // (vector)-(vector) = (vector)
        return this-Projection(other);
    }
}

class Program
{
    static void Main(string[] args)
    {
        var A=new Vector3(5, 5, 0);
        var B=new Vector3(0, 10, 0);
        var C=A.Rejection(B);
        // C = { 5,0,0}, expected answer from math {5,5,0}-0.5*{0,10,0}
    }
}

编辑

如果您无法控制代码,可以将代码移到 Vector3 类之外

// Vector3 defined elsewhere with .x, .y and .z fields

class VectorAlgebra
{
    public static Vector3 Subtract(Vector3 a, Vector3 b)
    {
        return new Vector3(a.x-b.x, a.y-b.y, a.z-b.z);
    }
    public static Vector3 Scale(float f, Vector3 a)
    {
        return new Vector3(f*a.x, f*a.y, f*a.z);
    }

    public static float Dot(Vector3 a, Vector3 b)
    {
        return (a.x*b.x)+(a.y*b.y)+(a.z*b.z);
    }

    public static Vector3 Projection(Vector3 a, Vector3 b)
    {
        return Scale(Dot(a, b)/Dot(b, b), b);
    }
    public static Vector3 Rejection(Vector3 a, Vector3 b)
    {
        return Subtract(a, Projection(a, b));
    }

    static void Main(string[] args)
    {
        var A=new Vector3(5, 5, 0);
        var B=new Vector3(0, 10, 0);
        var C=Rejection(A, B);
        // C = { 5,0,0}, expected answer from math {5,5,0}-0.5*{0,10,0}
    }
}

当你编写向量代数规则时,投影和拒绝的编码变得与数学书中的公式相同。您可以通过 Wolfram Alpha 查看答案.

关于c# - C# 中的矢量投影/拒绝?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26958198/

相关文章:

json - 使用 D3.js 和 Three.js 使用 json 数据创建交互式 3d 散点图

c# - 无法将 [] 索引应用于从 NHibernate 返回的对象数组的表达式类型 'object'

c++ - 在堆上创建多态对象的数据成员 vector ?

c++ - vector 字符串字符替换在字符串中间 C++

vector - 很好地介绍了如何使用向量和矩阵实现 3D 图形,而无需考虑编程语言或 directx/opengl

matlab - 使用点云的实体对象

c# - 为什么这个 CLS 不兼容?

c# - Entity Framework : Skip few columns or select as null from related entities

c# - PrivateObject 找不到属性

android - 我如何在android中添加填充矢量文件