2

我有包含页面 x 和 y 位置的点列表。我想在所有这些点上相对于页面的任何枢轴点应用旋转(目前假设它的中心)。

var points = new List<Point>();
points.Add(1,1);
points.Add(15,18);
points.Add(25,2);
points.Add(160,175);
points.Add(150,97);

const int pageHeight = 300;
const int pageWidth = 400;
var pivotPoint = new Point(200, 150); //Center

var angle = 45; // its in degree.

// Apply rotation.

我需要一些公式吗?

4

3 回答 3

6
public static Point Rotate(Point point, Point pivot, double angleDegree)
{
    double angle = angleDegree * Math.PI / 180;
    double cos = Math.Cos(angle);
    double sin = Math.Sin(angle);
    int dx = point.X - pivot.X;
    int dy = point.Y - pivot.Y;
    double x = cos * dx - sin * dy + pivot.X;
    double y = sin * dx + cos * dy + pivot.X;

    Point rotated = new Point((int)Math.Round(x), (int)Math.Round(y));
    return rotated;
}
static void Main(string[] args)
{
    Console.WriteLine(Rotate(new Point(1, 1), new Point(0, 0), 45));
}
于 2013-06-27T11:33:35.287 回答
3

如果您有大量的点要旋转,您可能需要预先计算旋转矩阵……</p>

[C  -S  U]
[S   C  V]
[0   0  1]

…在哪里…</p>

C = cos(θ)
S = sin(θ)
U = (1 - C) * pivot.x + S * pivot.y
V = (1 - C) * pivot.y - S * pivot.x

然后按如下方式旋转每个点:

rotated.x = C * original.x - S * original.y + U;
rotated.x = S * original.x + C * original.y + V;

上式是三个变换组合的结果……</p>

rotated = translate(pivot) * rotate(θ) * translate(-pivot) * original

…在哪里…</p>

translate([x y]) = [1 0 x]
                   [0 1 y]
                   [0 0 1]

rotate(θ) = [cos(θ) -sin(θ) 0]
            [sin(θ)  cos(θ) 0]
            [  0       0    1]
于 2013-06-27T11:47:41.447 回答
1

如果您将点(x,y)围绕点(x1,y1)旋转某个角度,那么您需要一个公式...

x2 = cos(a) * (x-x1) - sin(a) * (y-y1) + x1
y2 = sin(a) * (x-x1) + cos(a) * (y-y1) + y1
Point newRotatedPoint = new Point(x2,y2)
于 2013-06-27T11:34:12.947 回答