按照在球面上的位置对Vector3进行排序

4

我有一个Vector3点的列表,这些点是球面上的顶点。我想将它们排序,使其从南极开始向北极螺旋排列。

我认为可以按照y值进行排序,然后再按照z和x进行次级排序,但我不知道如何在c#中实现这一点。


请查看此链接,这里有完整的解决方案。 - Maifee Ul Asad
1个回答

2
以下函数是一个比较方法,用于比较两个向量,您可以在List.Sort方法中引用它。
首先,它会比较这两个向量是否相同。
然后,它会比较它们的y分量,并最终比较向量在(XZ)平面上的角度。
public static int Compare( Vector3 v1, Vector3 v2 )
{
    // Comparing two vectors this way is fine
    // Unity has overloaded the == operator
    // So as to avoid floating point imprecision
    if ( v1 == v2 ) return 0;

    if ( Mathf.Approximately( v1.y, v2.y ) )
    {
        float magnitude1 = v1.magnitude;
        float magnitude2 = v2.magnitude;

        if ( Mathf.Approximately( magnitude1, 0 ) ) return -1;
        if ( Mathf.Approximately( magnitude2, 0 ) ) return 1;

        // I took opposite coordinates because I had
        // "better" results, meaning the "smallest" angle would be
        // associated to the vector closer to the (1,0,0) vector
        // with an anti clock-wise rotation
        float angle1 = Mathf.Atan2( -v1.z, -v1.x );
        float angle2 = Mathf.Atan2( -v2.z, -v2.x );
        if ( Mathf.Approximately( angle1, angle2 ) ) return 0;
        return angle1 > angle2 ? 1 : -1;
    }
    return v1.y > v2.y ? 1 : -1;
}


这里有一小段代码片段,可以在场景视图中添加一些小工具,以便查看向量。
#if UNITY_EDITOR
    private void OnDrawGizmosSelected()
    {
        Vector3 origin = Vector3.zero;
        Gizmos.color = new Color( 1, 1, 1, 0.2f );
        for( int i = 0 ; i < Vectors.Count ; ++i )
        {
            Gizmos.DrawLine( origin, Vectors[i] );

            UnityEditor.Handles.Label( Vectors[i], i.ToString() );
            if( i < Vectors.Count - 1 )
                DrawGizmoArrow( Vectors[i], Vectors[i + 1], Color.HSVToRGB( (float) i / Vectors.Count, 1, 1 ) );
        }
    }

    private void DrawGizmoArrow( Vector3 start, Vector3 end, Color color )
    {
        if ( end == start ) return;
        Color c = Gizmos.color;
        Gizmos.color = color;
        Gizmos.DrawLine( start, end );
        Vector3 right = Quaternion.LookRotation( end - start ) * Quaternion.Euler(0, 180 + 20, 0) * new Vector3( 0, 0, 1 );
        Vector3 left = Quaternion.LookRotation( end - start ) * Quaternion.Euler(0, 180 - 20, 0) * new Vector3( 0, 0, 1 );
        Gizmos.DrawRay( end, right * 0.25f );
        Gizmos.DrawRay( end, left * 0.25f );
        Gizmos.color = c;
    }
#endif

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接