检查路径是否简单并使用cairo关闭 [英] Checking if path is simple and closed using cairo

查看:125
本文介绍了检查路径是否简单并使用cairo关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用cairomm与gtkmm一起进行GUI项目。我定义了一个 GeometricShape 抽象部件(它继承自 Gtk :: DrawingArea ),以便在需要绘制时继承一个正方形,一个圆圈等等。这个类定义了三个区域:


  1. 填充(或体区域);



  2. $ b

    填充区域为
    $ b

  3. 一个背景区域。
  4. 用于填充具有某种颜色的形状。边框(通常是黑色)定义了形状,背景区域用于用一种颜色填充剩余空间(除边框及其相关填充区域之外的所有区域)。以下是它如何查找圈子的示例:





    在这种情况下,背景颜色为绿色,边框颜色为蓝色,填充区域为红色(仅用于清除)。现在我的一个类不变量需要是由边界表示的路径是一个简单闭合曲线。我怎么能强制这个不变?



    我虽然要添加一些底层的非cairo逻辑来检查这个,例如通过提供某种 SimpleClosedPath 对象传递给cairo(本身会检查不变量),但我不确定这是如何实现的,因为我想允许多边形和基于曲面的形状(如圆形)。

    我虽然关于使用cairo来执行检查,但检查路径的方法似乎相当有限。我觉得开罗并不是为了解决这类问题。



    你有什么想法可以确保这种不变性会得到尊重吗?



    我并不是真的在寻找代码,但更多的是想法,有些事情可以尝试。

    解决方案


    但我不确定这是可能的,因为我想允许多边形和基于曲线的形状,就像一个圆圈。


    您知道 cairo_copy_path_flat ?它得到了一个当前路径的平坦副本,这意味着所有曲线都近似于线。



    所以,你可能会得到一个cairo上下文( cairo_t 在C中),在那里创建你的形状(用 line_to curve_to arc 等)。然后,您不要调用 fill stroke ,而改为 cairo_copy_path_flat

    产生的路径是一系列 move_to line_to close_path 命令,然后您可以(以某种方式...)检查相交。检查两条直线的交点是相对直线的,所以你可以例如只需检查每一行是否与每一行相交。


    I am using cairomm together with gtkmm for a GUI project. I have defined a GeometricShape abstract widget (which inherits from Gtk::DrawingArea) to inherit from if one needs to draw a square, a circle, etc. This class defines three regions:

    1. a fill (or body area);
    2. a border;
    3. a background area.

    The fill area is used to fill in the shape with a certain body color. The border (usually black) defines the shape and the background area is used to fill the remaining space (everything but the border and its related fill area) with a color. Here is an example of how it looks for a circle:

    In this case, the background color is green, the border color is blue and the fill region is red (just to be clear). Now one of my class invariants would need to be that the path represented by the border is a simple closed curve. How could I enforce this invariant?

    I though about adding some underlying non-cairo logic to check this, for example by providing some kind of SimpleClosedPath object to pass on to cairo (which would itself check the invariant), but I'm not sure how this would be possible, since I want to allow both polygons and curved based shapes, like a circle.

    I though about using cairo to perform the check, but the methods for checking paths seem pretty limited. I feel like cairo is not intended to solve this kind of problem.

    Do you have any idea how I could ensure this invariant would be respected?

    I'm not really looking for code, but more for an idea, something to try.

    解决方案

    but I'm not sure how this would be possible, since I want to allow both polygons and curved based shapes, like a circle.

    Do you know about cairo_copy_path_flat? It gets a flattened copy of the current path, meaning that all curves are approximated with lines.

    So, you would somehow get a cairo context (cairo_t in C), create your shape there (with line_to, curve_to, arc etc). Then you do not call fill or stroke, but instead cairo_copy_path_flat.

    The resulting path is a series of move_to, line_to and close_path commands which you could then (somehow...) check for intersecting. Checking two straight lines for intersection is relatively straight forward, so you could e.g. just check each line for intersection with every other line.

    这篇关于检查路径是否简单并使用cairo关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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