【问题标题】:Intersection point of QPainterPath and line (find QPainterPath y by x)QPainterPath 和直线的交点(找到 QPainterPath y by x)
【发布时间】:2012-03-12 17:08:55
【问题描述】:

我有 QPainterPath。我需要通过 x 找到 QPainterPath 的 y 坐标。

我在 QPainterPath 中找到了 intersected() 方法。所以,我创建了新的 QPainterPath,它是我的路径边界矩形的左到右边缘与 x 坐标的线,以找到相交的结果点。

intersects() 方法返回 true。但是 intersected() 返回空路径。

如果我使用高度 = 1 的矩形而不是线,一切正常。

也许您对如何找到 QPainterPath 与线的交点有更好的想法?

【问题讨论】:

标签: qt point qpainter line-intersection


【解决方案1】:

根据文档:

QPainterPath QPainterPath::intersected ( const QPainterPath & p ) const

返回一个路径,该路径是该路径的填充区域和 p 的填充区域的交集。 由于贝塞尔曲线交点的数值不稳定,贝塞尔曲线可能会被展平为线段。

由于您的线没有填充区域,因此此功能似乎不适合您。

如果您使用 QGraphicsScene 来显示您的 QPainterPath,您可以使用方法 collidingItems:

QList QGraphicsScene::collidingItems ( const QGraphicsItem * item, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape ) const

返回与项目冲突的所有项目的列表。碰撞是通过调用 QGraphicsItem::collidesWithItem(); 来确定的。碰撞检测由模式决定。默认情况下,返回形状与 item 相交或包含在 item 形状内的所有项目。 项按堆叠降序返回(即列表中的第一项为最上项,最后一项为最下项)。

不幸的是,QPainter 似乎没有相同的功能。我认为您创建长矩形的方法可能是一种更简单的方法。

【讨论】:

  • 感谢您的回答。不幸的是,我不使用 QGraphicsScene。而且我发现了一个intersected()方法导致堆栈溢出的情况:(所以看来我只需要找到带y的QPainterPath::Element,这是最接近我需要的。
  • 你应该切换到QGraphicsScene,它最适合处理多个项目。否则,创建一个 QList<QPainterPath*> 并跟踪您在 QPainter 上抛出的每条路径,然后您可以迭代列表并搜索具有适当 x 坐标的路径。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-20
  • 1970-01-01
  • 1970-01-01
  • 2023-02-23
  • 2013-10-31
相关资源
最近更新 更多