我该如何绘制基于一个简单的多边形的形象呢?多边形、我该、形象、简单

2023-09-02 20:49:17 作者:给你一口甜甜

我想一个大致矩形区域复制到一个矩形区域。例如:

I'd like to copy a roughly rectangular area to a rectangular area. Example:

这两个领域是他们的角点定义。大方向是保留(不翻转等)。

Both areas are defined by their corner points. The general direction is kept (no flipping etc).

只需旋转源图像不起作用,因为相对侧可以是不同的长度。

Simply rotating the source image does not work since opposing sides may be of different length.

到目前为止,我发现没有办法做到这一点在纯C#(除手动像素复制),所以我想我不得不求助于Windows的API或一些第三方库?

So far I found no way to do this in pure C# (except manual pixel copying), so I guess I have to resort to the Windows API or some 3rd party library?

推荐答案

一般来说,你想要做的是映射目标的坐标,通过变换函数到源坐标:

Generally speaking, what you want to do is map the destination coordinates to the source coordinates through a transform function:

for (int y = 0; y < destHeight; y++) {
    for (x=0; x < destWidth; x++) {
        Color c = Transform(x, y, sourceImage, sourceTransform);
        SetPixel(destImage, x, y, c);
    }
}

假设sourceTransform是一个对象,它封装了一个转变,从源头到dest坐标(反之亦然)。

Let's assume that sourceTransform is an object that encapsulates a transformation from source to dest coordinates (and vice versa).

工作在dest坐标将使它更容易避免曲线,你再转源图像,将让你更好的反锯齿,因为你可以在DEST像素的边角映射到其中的源图像和采样插值/外推

Working in dest coordinates will make it easier to avoid that curve in your retransformed source image and will allow you to better antialias, as you can map the corners of the dest pixel to the source image and sample within it and interpolate/extrapolate.

在你的情况,你就要有一组线性方程式做的映射 - 在这种情况下,这个被称为四边形变形 - see这previous问题。

In your case you're going to have a set of linear equations that do the mapping - in this case this is known as quadrilateral warping - see this previous question.