c++ - 开罗翻转绘图

标签 c++ cairo

我想像这样使用 C++ 和 Cairo 绘制三角形和文本:

|\
| \
|PP\
|___\

如果我使用 Cairo 添加三角形和文本,我会得到:

 ___
|   /
|PP/
| /
|/

所以 y 轴是从上到下,但我希望它是从下到上。所以我尝试更改视点矩阵 (cairo_transform(p, &mat);) 或缩放数据 (cairo_scale(p, 1.0, -1.0);)。我得到:

|\
| \
|bb\
|___\

现在三角形是我想要的方式,但文本是镜像的,我不想镜像。

知道如何处理这个问题吗?

最佳答案

我遇到了与 OP 类似的情况,要求我更改笛卡尔坐标系中的各种坐标,原点在左下角。 (我不得不移植一个用不同于开罗的坐标系开发的旧视频游戏,并且由于时间限制/可能的计算错误/移植精度我决定最好不要重写整个一堆)幸运的是,我找到了一个好的改变开罗坐标系的方法。该方法基于 Cairo 的内部转换矩阵,将 Cairo 的输入转换为用户设备。解决方案是将此矩阵更改为反射矩阵,该矩阵通过 x 轴镜像它的输入,如下所示:

cairo_t *cr;
cairo_matrix_t x_reflection_matrix; 
cairo_matrix_init_identity(&x_reflection_matrix); // could not find a oneliner
/* reflection through the x axis equals the identity matrix with the bottom 
left value negated  */
x_reflection_matrix.yy = -1.0;
cairo_set_matrix(cr, &x_reflection_matrix);
// This would result in your drawing being done on top of the destination 
// surface, so we translate the surface down the full height
cairo_translate(cr, 0, SURFACE_HEIGHT); // replace SURFACE_HEIGHT
// ... do your drawing

但是有一个问题:文本也会被镜像。要解决这个问题,可以更改字体转换矩阵。所需的代码是:

cairo_matrix_t font_reflection_matrix;
// We first set the size, and then change it to a reflection matrix
cairo_set_font_size(cr, YOUR_SIZE);
cairo_get_font_matrix(cr, &font_reflection_matrix);
// reverse mirror the font drawing matrix
font_reflection_matrix.yy = font_reflection_matrix.yy * -1;
cairo_set_font_matrix(cr, &font_reflection_matrix);

关于c++ - 开罗翻转绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26848694/

相关文章:

c++ - 在 Gdk::Pixbuf 上获取像素值,使用 Gdk::Cairo 设置像素值

gtk - 如何在 Cairo 表面(内部)上显示 Gtk Widget?

c - 多线程、gtk3 和 cairo : drawing in a cairo surface

c++ - 断言、-NDEBUG 和段错误

java - 如何使用 JNI 将字段从 C++ 设置为 Java

javascript - 从 C++ 填充 Node v8 Float32Array

清除 gtk_window 中的 cairo 文本

c - 如何使用 gtk+ 在 cairo 中重绘文本

c++ - 如何从 C++ <random> 获得一致的行为

c++ - 学习如何使用 UML