How do I draw an image based on a simple polygon?

前端 未结 2 423
梦谈多话
梦谈多话 2021-02-01 09:09

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

\"\"

Both areas are defined by

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-01 10:02

    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);
        }
    }
    

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

    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.

    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.

提交回复
热议问题