从路径中提取点

时间:2012-07-28 08:52:45

标签: qt

使用QPainterPath,我构建了一个图形(路径),画了它,但现在我需要在某个x点查询图形(路径)以得到相应的y点。

我无法想办法。

任何人编码智慧?

3 个答案:

答案 0 :(得分:5)

另一个解决方案是通过将QPainterPath转换为一个/多个QPolygonF来获取QPainterPath path(QPointF(0,0)); path.lineTo(QPointF(100,0)); path.lineTo(QPointF(100,100)); path.lineTo(QPointF(0,100)); path.lineTo(QPointF(0,0)); //change this to toSubpathPolygons to get multiple polygons. QPolygonF polygon = path.toFillPolygon(); QList<QLineF> edgeList; for(int i=0;i < polygon.count(); i++) { QLineF edge; if(i != (polygon.count() - 1)) { edge.setPoints(polygon.at(i),polygon.at(i+1)); } else { edge.setPoints(polygon.at(i),polygon.at(0)); } edgeList<<edge; } 中的所有点,这可以像这样轻松完成:

QLineF

然后,您可以使用扫描线检查与这些边缘的交点。使用foreach和另一个{{1}}循环的交叉函数。

答案 1 :(得分:2)

   import UIKit
   class ViewController:        
 UIViewController,UIPickerViewDelegate,UIPickerViewDataSource
{
@IBOutlet weak var picker: UIPickerView!
@IBOutlet weak var myLabel: UILabel!

let numbers = [0,1,2,3,4,5,6,7,8,9]

override func viewDidLoad()
{
    super.viewDidLoad()
    // A.Select Second Row
    picker.selectRow(1, inComponent: 0, animated: false)
}
func numberOfComponents(in pickerView: UIPickerView) -> Int
{
    return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
{
     //B. Add 2 rows to your array count  
    return numbers.count + 2
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
{
    var rowTitle = ""
    switch row
    {
    case 0:
        // C. Set first row title to last component of the array.
         // This row is not visible to the user as it is hidden by the        

         //selection in ViewDidLoad
        rowTitle = "\(numbers.last!)"
    case numbers.count + 1:
        //D. Set last row title to first array component
        rowTitle = "\(numbers.first!)"
    default:
        // E. Remember [row - 1] to avoid errors
        rowTitle = "\(numbers[row - 1])"
    }
    return rowTitle
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
    var theText = ""
    switch row
    {
    case 0:
      //F. Select Row at array count to load the last row
        pickerView.selectRow(numbers.count , inComponent: 0, animated: false)
        theText = = "\(numbers.last!)"
    case numbers.count + 1:
     //G. This Selection will set the picker to initial state
        pickerView.selectRow(1, inComponent: 0, animated: false)
        theText = "\(numbers.first!)"
    default:
        theText = "\(numbers[row - 1])"
    }
    myLabel.text = theText

}

  }

答案 2 :(得分:0)

QPainterPath没有任何数学函数来计算/求解它作为一个等式,因为它不是一个开始的等式。路径的绘制实际上是特定于绘图引擎。在坚果壳中,QPainterPath是一个绘图对象,甚至不是图形对象。

到达目的地的正确方法是自己用自己的数学绘制曲线。

但是如果你能够以有限的精度生活并且如果你的路径保证对任何可能的X都有一个Y值,那么你可以通过将QPainterPath渲染到黑白QImage(作为QPainter)并扫描来近似它图像中的每一行找到哪一行标有像素X.精度将取决于图像的大小。不完美,但它应该让你到附近。 :)

相关问题