如何计算仿射矩形的顶点

时间:2019-08-08 17:37:24

标签: c# graphics imagesharp

我正在尝试在图像上方绘制仿射矩形(带有旋转和倾斜),但不知道如何计算顶点。我没有数学或矩阵运算的背景。

我的仿射类定义如下:

public class AffineRectangle
{
public double X { get; set; }
public double Y { get; set; }
public double Width { get; set; }
public double Height { get; set; }
public double Rotation { get; set; }
public double Skew { get; set; }
}

我计划使用类似于以下代码的ImageSharp库来绘制多边形:

using (Image<Rgba32> image = new Image<Rgba32>(800, 800))
{
  image.Mutate(x => x.DrawPolygon(
      Rgba32.Red,
      10,
      new SixLabors.Primitives.PointF[] {
          new Vector2(10, 10),
          new Vector2(550, 50),
          etc...
      }));
  image.Save("test.bmp");
}

编辑:这是我正在编写的dotnet核心Web服务。

2 个答案:

答案 0 :(得分:1)

任何线性变换的组合都可以组合成一个矩阵并立即应用。在System.Windows.Media命名空间中,可以使用一些类为您做数学运算。您需要对PresentationCore.dll的引用,如果您正在编写WPF应用程序,则可能已经有了。

示例:

DELETE FROM address;
DELETE FROM location;
DELETE FROM city;
DELETE FROM country;
DELETE FROM performer;
DELETE FROM category;
DELETE FROM subcategory;
DELETE FROM location_category;

INSERT INTO city VALUES (1,null,null,'London');
INSERT INTO country VALUES (1,null,null,'England');
INSERT INTO performer VALUES (1, null,null,'a');
INSERT INTO performer VALUES (2, null,null,'b');
INSERT INTO performer VALUES (3, null,null,'c');
INSERT INTO performer VALUES (4, null,null,'d');
INSERT INTO category VALUES (1, null,null,'Music');
INSERT INTO subcategory VALUES(1,null,null,'Concert');
INSERT INTO location_category VALUES(1,null,null,'INDOOR');
INSERT INTO location values (1,null,null,1,1,1);
INSERT INTO address values (1,null,null,15,'test 12A',1);

编辑:对于面向.NET Core的项目,可以引用System.Drawing.Common包以实现类似的功能。下面的示例代码:

using System.Windows.Media;

var myPoint = new Point(10, 10);
var transforms = new TransformGroup();
transforms.Children.Add(new RotateTransform(90));
transforms.Children.Add(new SkewTransform(0, 30));
var transformedPoint = transforms.Transform(myPoint);

答案 1 :(得分:0)

当前的API(beta6)应该是这样的。

using (var image = new Image<Rgba32>(800, 800))
{
    var builder = new AffineTransformBuilder()
            .AppendSkewDegrees(10F, 5F)
            .AppendRotationDegrees(45F);

    var rectangle = new Rectangle(0, 0, 150, 150);
    var polygon = new RectangularPolygon(rectangle);
    Matrix3x2 matrix = builder.BuildMatrix(rectangle);

    image.Mutate(x => x.Draw(
        Rgba32.Red,
        10,
        polygon.Transform(matrix)));

    image.Save("test.bmp");
}