首先,坐标从整型换为浮点型,
typedef struct VERTEX2DF_TYP
{
int x,y; // the vertex
} VERTEX2DI, *VERTEX2DF_PTR;
// a 2D polygon
typedef struct POLYGON2D_TYP
{
int state; // state of polygon
int num_verts; // number of vertices
int x0,y0; // position of center of polygon
int xv,yv; // initial velocity
DWORD color; // could be index or PALETTENTRY
VERTEX2DF *vlist; // pointer to vertex list
} POLYGON2D, *POLYGON2D_PTR;
平移
int Translate_Polygon2D( POLYGON2D_PTR poly, int dx, int dy )
{
if( !poly )
return;
poly->x0 += dx;
poly->y0 += dy;
return ( 1 );
}
int Rotate_Polygon2D(POLYGON2D_PTR poly, int theta)
{
// this function rotates the local coordinates of the polygon
// test for valid pointer
if (!poly)
return(0);
// loop and rotate each point, very crude, no lookup!!!
for (int curr_vert = 0; curr_vert < poly->num_verts; curr_vert++)
{
// perform rotation
float xr = (float)poly->vlist[curr_vert].x*cos_look[theta] -
(float)poly->vlist[curr_vert].y*sin_look[theta];
float yr = (float)poly->vlist[curr_vert].x*sin_look[theta] +
(float)poly->vlist[curr_vert].y*cos_look[theta];
// store result back
poly->vlist[curr_vert].x = xr;
poly->vlist[curr_vert].y = yr;
} // end for curr_vert
// return success
return(1);
} // end Rotate_Polygon2D
在game_init()中,加上角度正弦余弦数值
先各自数组
float cos_look[360];
float sin_look[360];
for( int ang = 0; ang < 360; ang ++ )
{
float theta = ( float ) ang * PI / ( float ) 180;
cos_look[ang] = cos( theta );
sin_look[ang] = sin( theta );
}
Game_main()中
Translate_Polygon2D( & asteroids[curr_index], asteroids[curr_index].xv, asteroids[curr_index].yv );
Rotate_Polygon2D( & asteroids[cur_index], 5 );
结果如下图所示
现在封装,把那两个函数封装为成员函数即可。
现在看看t3dlib中,没什么好改的。
本文介绍了一种通过平移和旋转实现二维多边形变换的方法,并提供了具体的C语言实现代码。文章展示了如何定义多边形的数据结构,以及如何通过平移和旋转函数更新多边形的位置和方向。

被折叠的 条评论
为什么被折叠?



