如何找到覆盖旋转图像的新固定坐标?

时间:2017-03-23 14:09:09

标签: javascript c# rotation

具有从其原始位置旋转的PDF页面的渲染图像,我需要手动显示和旋转(通过HTML)PDF位置中的所有表单域以与旋转的图像对齐。下面的示例显示了所有内容的原始起始位置(绿色区域)以及旋转完成后我需要显示的字段(红色字段)。那里有很多旋转示例,但它们似乎专注于从0,0轴而不是可变中心点(在我的场景中是文档的中心)旋转。

Example

Here's the demostration in Plunker

这是C#代码似乎无法正常工作,因为它围绕0,0轴或其他东西旋转。它不断地将矩形重新定位在低位和右侧。

public static Point RotatePoint(Point pointToRotate, Point centerPoint, double angleInDegrees)
{
    double angleInRadians = angleInDegrees * (Math.PI / 180);
    double cosTheta = Math.Cos(angleInRadians);
    double sinTheta = Math.Sin(angleInRadians);
    double x = (cosTheta * (pointToRotate.X - centerPoint.X) - sinTheta * (pointToRotate.Y - centerPoint.Y) + centerPoint.X);
    double y = (sinTheta * (pointToRotate.X - centerPoint.X) + cosTheta * (pointToRotate.Y - centerPoint.Y) + centerPoint.Y);
    return new Point(x, y);
}

1 个答案:

答案 0 :(得分:0)

我想我弄清楚了。整个cos / sin的东西太过分了。

Here's the plunker doing the same thing in javascript

public static CoordinatesAndDimensionsModel RotatePoint(CoordinatesAndDimensionsModel page, CoordinatesAndDimensionsModel field, int angleInDegrees)
{
    double newX, newY;

    switch (angleInDegrees) {

        case 90:
        case -270:
            newX = page.Width - field.Y - field.Height;
            newY = field.X;
            return new CoordinatesAndDimensionsModel(newX, newY, field.Height, field.Width);

        case 180:
        case -180:
            newX = page.Width - field.X - field.Width;
            newY = page.Height - field.Y - field.Height;
            return new CoordinatesAndDimensionsModel(newX, newY, field.Width, field.Height);

        case -90:
        case 270:
            newX = field.Y;
            newY = page.Height - field.X - field.Width;
            return new CoordinatesAndDimensionsModel(newX, newY, field.Height, field.Width);

        default:
            return new CoordinatesAndDimensionsModel(field.X, field.Y, field.Width, field.Height);
    }
}

public class CoordinatesAndDimensionsModel
{
    public CoordinatesAndDimensionsModel() {}

    public CoordinatesAndDimensionsModel(double x, double y, double width, double height)
    {
        this.X = x;
        this.Y = y;
        this.Width = width;
        this.Height = height;
    }

    public double X { get; set; }
    public double Y { get; set; }
    public double Width { get; set; }
    public double Height { get; set; }
}