我正在尝试理解 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
关于c - 了解 cairo_rotate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22960353/