有一个从原始位置旋转的 PDF 页面的渲染图像,我需要手动显示和旋转(通过 HTML)PDF 位置中的所有表单字段,以与旋转的图像对齐。下面的示例显示了所有内容的原始开始位置(绿色字段)以及我需要在旋转完成后显示该字段的位置(红色字段)。有很多旋转示例,但它们似乎专注于从 0,0 轴旋转,而不是可变中心点(在我的场景中是文档的中心)。
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);
}
最佳答案
我想我已经明白了。整个 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; }
}
关于javascript - 如何找到覆盖旋转图像的新固定坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42978503/