二维形状顺时针旋转
我刚接触Python和图形编程,但之前有过编程经验。根据这个链接,
如果要围绕原点逆时针旋转一个角度θ,公式是:x' = xcosθ − ysinθ,y' = xsinθ + ycosθ。
但是下面的Python代码却是顺时针旋转。有人能解释一下吗?另外,把矩形移动到原点再移回中心似乎有点麻烦,有没有办法避免这样做呢?谢谢大家。
附言:我看过pygame.transform.rotate
这个方法,它可以做到这一点,但我想从头开始学习,以便更好地理解图形编程。有没有办法在Python解释器中查看这个方法的源代码呢?
import pygame, sys, time
from math import *
from pygame.locals import *
co_ordinates =((200,200),(400,200),(400,300),(200,300))
window_surface = pygame.display.set_mode((500,500),0,32)
BLACK=(0,0,0)
GREEN=(0,255,0)
RED=(255,0,0)
window_surface.fill(BLACK)
ang=radians(30)
"""orig=pygame.draw.polygon(window_surface,GREEN,co_ordinates)
n_co_ordinates = tuple([(((x[0])*cos(ang)-(x[1])*sin(ang)),((x[0])*sin(ang)+(x[1])*cos(ang))) for x in n_co_ordinates])
n_co_ordinates = tuple([((x[0]+300),(x[1]+250)) for x in n_co_ordinates])
print(n_co_ordinates)
pygame.draw.polygon(window_surface,RED,n_co_ordinates)"""
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
for i in range(360):
ang=radians(i)
if i>=360:
i=0
n_co_ordinates = tuple([((x[0]-300),(x[1]-250)) for x in co_ordinates])
n_co_ordinates = tuple([((x[0]*cos(ang)-x[1]*sin(ang)),(x[0]*sin(ang)+x[1]*cos(ang))) for x in n_co_ordinates])
n_co_ordinates = tuple([((x[0]+300),(x[1]+250)) for x in n_co_ordinates])
window_surface.fill(BLACK)
pygame.draw.polygon(window_surface,RED,n_co_ordinates)
pygame.display.update()
time.sleep(0.02)
4 个回答
关于先平移、再旋转、最后再平移的过程,其实你确实需要这样做。不过,如果你能先计算出每一步的变换矩阵,然后把它们乘在一起,得到一个包含这两次平移和一次旋转的总变换矩阵,那你就只需要用这个总矩阵去乘每一个顶点了。要在矩阵变换中包含平移,你需要用到“齐次坐标”——可以在维基百科的文章中找到更多信息。简单来说,就是用坐标 (x,y,1) 来代替 (x,y),然后使用一个 3x3 的矩阵。这个额外的数字可以帮助进行变换。
Pygame使用的是一种坐标系统,其中[0,0]表示左上角。你的旋转在一种坐标系统中是正常的,在那种系统里,点越高,y坐标就越大,但在Pygame中正好相反:点越低,y坐标就越大。这就导致了一切都“翻转”了,所以你物体看起来旋转的角度和你实际旋转的角度是相反的。解决这个问题最简单的方法就是输入你想要旋转的角度的相反数。
如果你想要反向旋转,只需要把 ang
改成 -ang
。我觉得你可能在旋转矩阵里搞错了符号,但我总是记不住。 (补充说明:这其实就是把 sin
的符号改了,因为 sin(-x)==-sin(x)
和 cos(-x)==cos(x)
。)
你无法避免先把点移动到中心。原因是你的变换是固定在原点 (0,0)
的(因为 0*cos(...)==0
),所以你总是在原点附近旋转。因此,如果你想围绕其他地方旋转,必须先把那个点移动到原点。
这里是 rotate
的源代码,来自 pygame 的 transform.c
文件。这个代码是用 C 语言写的。
static void
rotate (SDL_Surface *src, SDL_Surface *dst, Uint32 bgcolor, double sangle,
double cangle)
{
int x, y, dx, dy;
Uint8 *srcpix = (Uint8*) src->pixels;
Uint8 *dstrow = (Uint8*) dst->pixels;
int srcpitch = src->pitch;
int dstpitch = dst->pitch;
int cy = dst->h / 2;
int xd = ((src->w - dst->w) << 15);
int yd = ((src->h - dst->h) << 15);
int isin = (int)(sangle * 65536);
int icos = (int)(cangle * 65536);
int ax = ((dst->w) << 15) - (int)(cangle * ((dst->w - 1) << 15));
int ay = ((dst->h) << 15) - (int)(sangle * ((dst->w - 1) << 15));
int xmaxval = ((src->w) << 16) - 1;
int ymaxval = ((src->h) << 16) - 1;
switch (src->format->BytesPerPixel)
{
case 1:
for (y = 0; y < dst->h; y++)
{
Uint8 *dstpos = (Uint8*)dstrow;
dx = (ax + (isin * (cy - y))) + xd;
dy = (ay - (icos * (cy - y))) + yd;
for (x = 0; x < dst->w; x++)
{
if(dx < 0 || dy < 0 || dx > xmaxval || dy > ymaxval)
*dstpos++ = bgcolor;
else
*dstpos++ = *(Uint8*)
(srcpix + ((dy >> 16) * srcpitch) + (dx >> 16));
dx += icos;
dy += isin;
}
dstrow += dstpitch;
}
break;
case 2:
for (y = 0; y < dst->h; y++)
{
Uint16 *dstpos = (Uint16*)dstrow;
dx = (ax + (isin * (cy - y))) + xd;
dy = (ay - (icos * (cy - y))) + yd;
for (x = 0; x < dst->w; x++)
{
if (dx < 0 || dy < 0 || dx > xmaxval || dy > ymaxval)
*dstpos++ = bgcolor;
else
*dstpos++ = *(Uint16*)
(srcpix + ((dy >> 16) * srcpitch) + (dx >> 16 << 1));
dx += icos;
dy += isin;
}
dstrow += dstpitch;
}
break;
case 4:
for (y = 0; y < dst->h; y++)
{
Uint32 *dstpos = (Uint32*)dstrow;
dx = (ax + (isin * (cy - y))) + xd;
dy = (ay - (icos * (cy - y))) + yd;
for (x = 0; x < dst->w; x++)
{
if (dx < 0 || dy < 0 || dx > xmaxval || dy > ymaxval)
*dstpos++ = bgcolor;
else
*dstpos++ = *(Uint32*)
(srcpix + ((dy >> 16) * srcpitch) + (dx >> 16 << 2));
dx += icos;
dy += isin;
}
dstrow += dstpitch;
}
break;
default: /*case 3:*/
for (y = 0; y < dst->h; y++)
{
Uint8 *dstpos = (Uint8*)dstrow;
dx = (ax + (isin * (cy - y))) + xd;
dy = (ay - (icos * (cy - y))) + yd;
for (x = 0; x < dst->w; x++)
{
if (dx < 0 || dy < 0 || dx > xmaxval || dy > ymaxval)
{
dstpos[0] = ((Uint8*) &bgcolor)[0];
dstpos[1] = ((Uint8*) &bgcolor)[1];
dstpos[2] = ((Uint8*) &bgcolor)[2];
dstpos += 3;
}
else
{
Uint8* srcpos = (Uint8*)
(srcpix + ((dy >> 16) * srcpitch) + ((dx >> 16) * 3));
dstpos[0] = srcpos[0];
dstpos[1] = srcpos[1];
dstpos[2] = srcpos[2];
dstpos += 3;
}
dx += icos; dy += isin;
}
dstrow += dstpitch;
}
break;
}
}