了解cairo_rotate

我想了解cairo_rotate函数。

请考虑以下代码。 我希望绘制一个十字,但只绘制水平线(旋转前)。 我的错是什么?

 cairo_move_to(cr,0,HEIGHT/2.); cairo_line_to(cr,WIDTH,HEIGHT/2.); cairo_stroke(cr); //horizontal line cairo_rotate(cr,90.*(M_PI/180.)); cairo_move_to(cr,0,HEIGHT/2.); cairo_line_to(cr,WIDTH,HEIGHT/2.); cairo_stroke(cr); //this line isn't painted 

您正在原点周围旋转,原点位于图像的左上角。 要围绕图像的中心旋转,您还必须翻译:

 cairo_move_to(cr,0,HEIGHT/2.); cairo_line_to(cr,WIDTH,HEIGHT/2.); cairo_stroke(cr); cairo_translate(cr,WIDTH/2,HEIGHT/2); // translate origin to the center cairo_rotate(cr,90.*(M_PI/180.)); cairo_translate(cr,-WIDTH/2,-HEIGHT/2); // translate origin back cairo_move_to(cr,0,HEIGHT/2.); cairo_line_to(cr,WIDTH,HEIGHT/2.); cairo_stroke(cr); 

根据您的应用程序,实际绘制相对于中心的所有内容也是有意义的:

 int half_w = WIDTH/2; int half_h = HEIGHT/2; cairo_translate(cr, half_w, half_h); cairo_move_to(cr, -half_w, 0); cairo_line_to(cr, half_w, 0); cairo_stroke(cr); // horizontal line cairo_rotate(cr, 90.*(M_PI/180.)); cairo_move_to(cr, -half_w, 0); cairo_line_to(cr, half_w, 0); cairo_stroke(cr); // vertical line