के बीच छेड़छाड़ का बिंदु ढूँढना मैं उस बिंदु को निर्धारित करने की कोशिश कर रहा हूं जहां हिशकैन प्रोजेक्टाइल का पथ (मूल रूप से एक रेखा है, लेकिन मैंने इसे अपने उदाहरण में QPainterPath
के रूप में दर्शाया है) में एक आइटम के साथ छेड़छाड़ करता है मेरा दृश्य। अगर वहाँ इस बिंदु QPainterPath
, QLineF
द्वारा प्रदान की काम करता है, का उपयोग कर खोजने के लिए एक तरीका है मुझे यकीन है कि नहीं कर रहा हूँ आदि नीचे कोड दिखाता है कि मैं क्या करने की कोशिश कर रहा हूँ:एक रेखा और एक QPainterPath
#include <QtWidgets>
bool hit(const QPainterPath &projectilePath, QGraphicsScene *scene, QPointF &hitPos)
{
const QList<QGraphicsItem *> itemsInPath = scene->items(projectilePath, Qt::IntersectsItemBoundingRect);
if (!itemsInPath.isEmpty()) {
const QPointF projectileStartPos = projectilePath.elementAt(0);
float shortestDistance = std::numeric_limits<float>::max();
QGraphicsItem *closest = 0;
foreach (QGraphicsItem *item, itemsInPath) {
QPointF distanceAsPoint = item->pos() - projectileStartPos;
float distance = abs(distanceAsPoint.x() + distanceAsPoint.y());
if (distance < shortestDistance) {
shortestDistance = distance;
closest = item;
}
}
QPainterPath targetShape = closest->mapToScene(closest->shape());
// hitPos = /* the point at which projectilePath hits targetShape */
hitPos = closest->pos(); // incorrect; always gives top left
qDebug() << projectilePath.intersects(targetShape); // true
qDebug() << projectilePath.intersected(targetShape); // QPainterPath: Element count=0
// To show that they do actually intersect..
QPen p1(Qt::green);
p1.setWidth(2);
QPen p2(Qt::blue);
p2.setWidth(2);
scene->addPath(projectilePath, p1);
scene->addPath(targetShape, p2);
return true;
}
return false;
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QGraphicsView view;
view.setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
QGraphicsScene *scene = new QGraphicsScene;
view.setScene(scene);
view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
QGraphicsItem *target = scene->addRect(0, 0, 25, 25);
target->setTransformOriginPoint(QPointF(12.5, 12.5));
target->setRotation(35);
target->setPos(100, 100);
QPainterPath projectilePath;
projectilePath.moveTo(200, 200);
projectilePath.lineTo(0, 0);
projectilePath.lineTo(200, 200);
QPointF hitPos;
if (hit(projectilePath, scene, hitPos)) {
scene->addEllipse(hitPos.x() - 2, hitPos.y() - 2, 4, 4, QPen(Qt::red));
}
scene->addPath(projectilePath, QPen(Qt::DashLine));
scene->addText("start")->setPos(180, 150);
scene->addText("end")->setPos(20, 0);
view.show();
return app.exec();
}
projectilePath.intersects(targetShape)
रिटर्न true
, लेकिन projectilePath.intersected(targetShape)
रिटर्न एक खाली रास्ता
क्या यह हासिल करने का कोई तरीका है?
आप क्यूटी 4 या क्यूटी 5. का उपयोग कर यह अच्छा हो सकता है आपके सवाल का अधिक विशिष्ट टैग को जोड़ने के लिए कर रहे हैं:
यहाँ तय कार्य है। –
का डुप्लिकेट: http://stackoverflow.com/questions/9393672/intersection-point-of-qpainterpath-and-line-find-qpainterpath-y-by-x – Mitch