Skip to main content
 首页 » 编程设计

c#之比例平移

2024年08月06日45langtianya

我想通过执行旋转(围绕新原点)并将每个点平移与其当前与原点的距离成比例的量(因此不是绝对平移)来更新点列表(PointFs) .

我目前依次为每个点执行此操作,但移动多个点时性能很差。

我想让转换更有效率,所以想使用矩阵。旋转没问题,就是不知道怎么按比例平移。

我可以用仿射矩阵来做这个吗?有没有其他方法可以更有效地进行转换?

已更新

这是我当前的代码。我对它做了一些改动,所以至少它确实使用了一个矩阵来进行旋转。请注意,转换是基于比率的,因此靠近中心的点不会移动到远离中心的点:

    private void DragPointsAroundCentre(PointF centre, PointF priorLocation, PointF newLocation, PointF[] otherPoints) 
    { 
        // calculate the angle and length of the transformation from the original location 
        var priorLength = Maths.Distance(centre, priorLocation); 
        var newLength = Maths.Distance(centre, newLocation); 
 
        var lengthRatio = newLength / priorLength; 
        var rotationAngle = (float)Maths.Angle(centre, priorLocation, newLocation); 
 
        // apply the rotation to the other points 
        Rotate(otherPoints, rotationAngle, centre); 
 
        // apply an equivalent translation to the other points 
        for (int i = 0; i < otherPoints.Length ; i++) 
        { 
            var translation = GetPointOnLine(centre, otherPoints[i], (float) lengthRatio); 
            otherPoints[i].X = translation.X; 
            otherPoints[i].Y = translation.Y; 
        } 
    } 
 
    private static void Rotate(PointF[] points, float angle, PointF center) 
    { 
        using (Matrix m = new Matrix()) 
        { 
            m.RotateAt(angle, center); 
            m.TransformPoints(points); 
        } 
    } 
 
    // gets a point from a relative position on a line using the specified ratio 
    private static PointF GetPointOnLine(PointF origin, PointF point, float ratio) 
    { 
        return new PointF( 
            origin.X + (point.X - origin.X) * ratio, 
            origin.Y + (point.Y - origin.Y) * ratio); 
    } 

请您参考如下方法:

这是我用于转换的代码。希望对您有所帮助:

class Program 
{ 
    static void Main(string[] args) 
    { 
        PointF[] points = new PointF[]  
        {  
            new PointF(1, 0),  
            new PointF(0, 1)  
        }; 
 
        float angle = 90; // in degrees 
        PointF center = new PointF(1, 1); 
        Rotate(points, angle, center); 
 
        float offset = 10; 
        PointF vector = new PointF(1, 1); 
        Translate(points, offset, vector); 
    } 
 
    static void Rotate(PointF[] points, float angle, PointF center) 
    { 
        using (Matrix m = new Matrix()) 
        { 
            m.RotateAt(angle, center); 
            m.TransformPoints(points); 
        } 
    } 
 
    // Translates point along the specified vector. 
    static void Translate(PointF[] points, float offset, PointF vector) 
    { 
        float magnitude = (float)Math.Sqrt((vector.X * vector.X) + (vector.Y * vector.Y)); // = length 
        vector.X /= magnitude; 
        vector.Y /= magnitude; 
        PointF translation = new PointF() 
        { 
            X = offset * vector.X, 
            Y = offset * vector.Y 
        }; 
        using (Matrix m = new Matrix()) 
        { 
            m.Translate(translation.X, translation.Y); 
            m.TransformPoints(points); 
        } 
    } 
} 

如果您需要非常高效的转换,您可以将两个转换矩阵合并为一个,并且只转换所有点一次。

编辑:

例如,您可以使用一个简单的并行循环来加快速度。但即使是 30.000.000 点,在这种情况下差异也不会太大(我的情况是 4 个 cpu 内核)。但这当然取决于您处理它们的频率。

class Program 
{ 
    static void Main(string[] args) 
    { 
        int pointCount = 30000000; 
        PointF[] otherPoints = new PointF[pointCount]; 
        Random rnd = new Random(); 
        for (int i = 0; i < pointCount; i++) 
        { 
            otherPoints[i] = new Point(rnd.Next(), rnd.Next()); 
        } 
 
        PointF centre = new PointF(3, 3); 
        float lengthRatio = 7.3f; 
 
        // apply an equivalent translation to the other points 
        Stopwatch sw = new Stopwatch(); 
 
        sw.Start(); 
        for (int i = 0; i < otherPoints.Length; i++) 
        { 
            var translation = GetPointOnLine(centre, otherPoints[i], (float)lengthRatio); 
            otherPoints[i].X = translation.X; 
            otherPoints[i].Y = translation.Y; 
        } 
        sw.Stop(); 
        Console.WriteLine("Single thread: {0} sec.", sw.Elapsed.TotalSeconds); 
 
        sw.Reset(); 
        sw.Start(); 
        Parallel.For(0, pointCount, i => 
        { 
            var translation = GetPointOnLine(centre, otherPoints[i], (float)lengthRatio); 
            otherPoints[i].X = translation.X; 
            otherPoints[i].Y = translation.Y; 
 
        }); 
        sw.Stop(); 
        Console.WriteLine("Multi thread: {0} sec.", sw.Elapsed.TotalSeconds); 
        Console.ReadKey(); 
    } 
 
    // gets a point from a relative position on a line using the specified ratio 
    private static PointF GetPointOnLine(PointF origin, PointF point, float ratio) 
    { 
        return new PointF( 
            origin.X + (point.X - origin.X) * ratio, 
            origin.Y + (point.Y - origin.Y) * ratio); 
    } 
} 

EDIT-2:

我发现了一个与您的完全相同的转换,它使用单个矩阵仅在一个循环中转换点。以下是旧转换和新转换的代码:

class Program 
{ 
    static void Main(string[] args) 
    { 
        PointF[] points1 = new PointF[]  
        {  
            new PointF(1f, 0f), 
            new PointF(0f, 1f), 
            new PointF(1f, 1f), 
            new PointF(2f, 2f), 
        }; 
        PointF[] points2 = new PointF[] 
        {  
            new PointF(1f, 0f), 
            new PointF(0f, 1f), 
            new PointF(1f, 1f), 
            new PointF(2f, 2f), 
        }; 
 
        PointF center = new PointF(2f, 2f); 
 
        float priorLength = 4f; 
        float newLength = 5f; 
 
        float lengthRatio = newLength / priorLength; 
 
        float rotationAngle = 45f; 
 
        Transformation_old(points1, rotationAngle, center, lengthRatio); 
        Transformation_new(points2, rotationAngle, center, lengthRatio); 
 
        Console.ReadKey(); 
    } 
 
    static void Transformation_old(PointF[] points, float rotationAngle, PointF center, float lengthRatio) 
    { 
        Rotate(points, rotationAngle, center); 
 
        for (int i = 0; i < points.Length; i++) 
        { 
            var translation = GetPointOnLine(center, points[i], lengthRatio); 
            points[i].X = translation.X; 
            points[i].Y = translation.Y; 
        } 
    } 
 
    static void Rotate(PointF[] points, float angle, PointF center) 
    { 
        using (Matrix m = new Matrix()) 
        { 
            m.RotateAt(angle, center); 
            m.TransformPoints(points); 
        } 
    } 
 
    private static PointF GetPointOnLine(PointF origin, PointF point, float ratio) 
    { 
        return new PointF( 
            origin.X + (point.X - origin.X) * ratio, 
            origin.Y + (point.Y - origin.Y) * ratio); 
    } 
 
    // Uses only a single matrix and a single transformation: 
    static void Transformation_new(PointF[] points, float rotationAngle, PointF center, float lengthRatio) 
    { 
        using (Matrix m = new Matrix()) 
        { 
            m.RotateAt(rotationAngle, center, MatrixOrder.Prepend); 
 
            // Replaces GetPointOnLine 
            m.Translate(center.X, center.Y, MatrixOrder.Prepend); 
            m.Scale(lengthRatio, lengthRatio, MatrixOrder.Prepend); 
            m.Translate(-center.X, -center.Y, MatrixOrder.Prepend); 
 
            m.TransformPoints(points); 
        } 
    } 
}