开发者

Help with rotating xy-pixel coordinates, unsure about my function

开发者 https://www.devze.com 2023-02-06 06:11 出处:网络
Ok I have a small set of pixels (a character) stored with xy-coordinates. Now I want to rotate the image around its middlepoint with, let\'s say, a 105 degrees.

Ok I have a small set of pixels (a character) stored with xy-coordinates. Now I want to rotate the image around its middlepoint with, let's say, a 105 degrees.

Is this the right way to do it?:

Each pixels distance from the middlepoint is its radius. Then in python code

new_x = avg_pos_x+(math.cos(math.radians(105))*radius)
new_y = avg_pos_y-(math.sin(math.radians(105))*radius)

It seems to work, but then again some pixels appear to be a bit random.

Edit: Forgot that to rotate each pixel 105 degrees I also calculated the angle between the 开发者_开发百科middlepoint and pixel and added 105 degrees to that.


Rather than figuring out where each pixel in the original image goes, you should figure out where each pixel in the new image came from. You can do this just by rotating negative 105 degrees.

old_x = avg_pos_x+(math.cos(math.radians(-105))*radius)
old_y = avg_pos_y-(math.sin(math.radians(-105))*radius)

Then copy the color of (old_x, old_y) to (x, y). Here's where you could do interpolation if you like -- e.g. taking a weighted average color of the four pixels whose centers form a square containing (old_x, old_y).

By doing the transformation in reverse, you're sure to figure out the color for each pixel of the new image exactly once. If you do it "forwards", some pixels in the new image will be hit more than once, and some won't be hit at all. These pixels not being hit at all might be your "random" pixels -- holes in your final image that end up with a default color or somesuch.


Looks right. The problem is of accuracy. Pixels near the center get bunched up and pixels far away get spread out.

What you need to do is interpolate the pixels so that it looks smooth.

e.g., when you rotate a pixel and it ends up half way between one pixel and another but you plot it on one you have an issue when it should really be half on one and half on the other. (if that makes any sense).

Since the screen has discrete pixels you have to determine how much of the original pixel contributes to the new pixel.

You do this by looking at the fractional part of the computed coordinates. Say your new_x is an integer, then you know it contributes all to the pixel. BUT say it has fractional part 0.25. This means some 25% of the pixel is actually in the next location. (depending on how you define the pixel origin)

Since your dealing with 2D is is more complex as you have 8 possible pixels. There are algorithms out that do it differently and may work better(faster) but this method will work.

0

精彩评论

暂无评论...
验证码 换一张
取 消