了解cairo_rotate [英] Understanding cairo_rotate

查看:243
本文介绍了了解cairo_rotate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解 cairo_rotate 函数。

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

Consider the following code. I would have expected a cross to be drawn, but only the horizontal line (before the rotation) is drawn. What is my mistake?

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


推荐答案

您正在旋转围绕原点(位于图像的左上角)。要围绕图像的中心旋转,还必须平移:

You are rotating around the origin, which is at the top-left corner of the image. To rotate around the center of the image, you must translate as well:

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);

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

Depending on your application, it might also make sense to actually draw everything relative to the center:

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

这篇关于了解cairo_rotate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆