- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试确定命中扫描射弹的路径(基本上是一条线,但在我的示例中将其表示为 QPainterPath
)与场景中的项目相交的点。我不确定是否有办法使用 QPainterPath、QLineF 等提供的函数找到这一点。下面的代码说明了我想要做的事情:
#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)
返回空路径。
有办法实现吗?
最佳答案
作为Intersection point of QPainterPath and line (find QPainterPath y by x)的答案指出,QPainterPath::intersected()仅考虑具有填充区域的路径。那里也提到了矩形路径技巧可以这样实现:
#include <QtWidgets>
/*!
Returns the closest element (position) in \a sourcePath to \a target,
using \l{QPoint::manhattanLength()} to determine the distances.
*/
QPointF closestPointTo(const QPointF &target, const QPainterPath &sourcePath)
{
Q_ASSERT(!sourcePath.isEmpty());
QPointF shortestDistance = sourcePath.elementAt(0) - target;
qreal shortestLength = shortestDistance.manhattanLength();
for (int i = 1; i < sourcePath.elementCount(); ++i) {
const QPointF distance(sourcePath.elementAt(i) - target);
const qreal length = distance.manhattanLength();
if (length < shortestLength) {
shortestDistance = sourcePath.elementAt(i);
shortestLength = length;
}
}
return shortestDistance;
}
/*!
Returns \c true if \a projectilePath intersects with any items in \a scene,
setting \a hitPos to the position of the intersection.
*/
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());
// QLineF has normalVector(), which is useful for extending our path to a rectangle.
// The path needs to be a rectangle, as QPainterPath::intersected() only accounts
// for intersections between fill areas, which projectilePath doesn't have.
QLineF pathAsLine(projectileStartPos, projectilePath.elementAt(1));
// Extend the first point in the path out by 1 pixel.
QLineF startEdge = pathAsLine.normalVector();
startEdge.setLength(1);
// Swap the points in the line so the normal vector is at the other end of the line.
pathAsLine.setPoints(pathAsLine.p2(), pathAsLine.p1());
QLineF endEdge = pathAsLine.normalVector();
// The end point is currently pointing the wrong way; move it to face the same
// direction as startEdge.
endEdge.setLength(-1);
// Now we can create a rectangle from our edges.
QPainterPath rectPath(startEdge.p1());
rectPath.lineTo(startEdge.p2());
rectPath.lineTo(endEdge.p2());
rectPath.lineTo(endEdge.p1());
rectPath.lineTo(startEdge.p1());
// Visualize the rectangle that we created.
scene->addPath(rectPath, QPen(QBrush(Qt::blue), 2));
// Visualize the intersection of the rectangle with the item.
scene->addPath(targetShape.intersected(rectPath), QPen(QBrush(Qt::cyan), 2));
// The hit position will be the element (point) of the rectangle that is the
// closest to where the projectile was fired from.
hitPos = closestPointTo(projectileStartPos, targetShape.intersected(rectPath));
return true;
}
return false;
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QGraphicsView view;
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();
}
这具有相当好的精度(± 1 像素,因为 QLineF::length() 是一个整数),但可能有一种更简洁的方法来实现相同的效果。
关于qt - 查找直线和 QPainterPath 之间的交点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17512547/
这一章实现的连接线,目前仅支持直线连接,为了能够不影响原有的其它功能,尝试了2、3个实现思路,最终实测这个实现方式目前来说最为合适了。 请大家动动小手,给我一个免费的 Star 吧~ 大家
本章分享一下如何使用 Konva 绘制基础图形:矩形、直线、折线,希望大家继续关注和支持哈! 请大家动动小手,给我一个免费的 Star 吧~ 大家如果发现了 Bug,欢迎来提 Issue
本章响应小伙伴的反馈,除了算法自动画连接线(仍需优化完善),实现了可以手动绘制直线、折线连接线功能。 请大家动动小手,给我一个免费的 Star 吧~ 大家如果发现了 Bug,欢迎来提 Is
我有一条线(两点(x,y)(x1,y1))和一个带有焦点(rx,ry)的矩形。我需要帮助找出线和矩形之间的碰撞点,C++ 中的一个例子会有所帮助。 最佳答案 我不知道如何仅用“焦点”来表示矩形。您将需
让我们画一条线和一个圆: canvas.create_line(x0,y0,x1,y1) canvas.create_oval(x0,y0,x1,y1) 如何计算每个像素的像素数? 最佳答案 你不能。
本文实例讲述了php使用gd2绘制基本图形。分享给大家供大家参考,具体如下: 应用GD2函数可以绘制的图形有多种,最基本的图形包括条、圆、方形等。无论开发人员绘制多么复杂的图形,都是在这些最基本的
我已经尝试了所有改变颜色的方法: call s:h("Underlined", {"fg": s:norm, "gui": "underline", "cterm": "underline"})
为什么以下行有时会在 chrome 开发控制台中产生消息“未定义不是函数”: (callbackOrUndefined || function() {})(); 这个想法是,如果回调为真,即函数,则执
我想在 MS Chart 中实现直线(分段拟合)图表。 我知道 X 轴上的点 [最小值 = 1.4,最大值 = 2.2]。我已将所有这些点都放在折线图上。 现在的要求是我如何知道 X 轴上的点 [最小
所以我正在使用 charts.js http://www.chartjs.org/我试图使 2 个点之间的线是直线而不是弯曲的,没有明显的原因。 现在看起来是这样的 http://imgur.com/
我已将我的机器人发布到 Azure。现在我要开发自己的聊天用户界面。我想在 JavaScript 中使用 Direct Line API 来调用机器人。 如何在 JavaScript 中使用 Dire
这个有点难解释。我有一个整数列表。因此,例如,[1, 2, 4, 5, 8, 7, 6, 4, 1] - 当根据元素编号绘制时,它类似于凸图。我如何以某种方式从列表中提取此“形状”特征?它不必特别准确
我在学习 javaScript 和 jQuery 的第一周,我正在尝试制作一些动画。 基本上我想要做的就是让这些箭头从屏幕开始,从一侧进入,通过 throw 目标,然后离开屏幕的另一侧。 理想情况下,
假设我有一个向导并且有 3 个部分,假设它是这样的。 [A]----[B]----[C] 假设当前在范围内选择了 A。这是一个彩色的红色圆圈。当 B 在范围内被选中时,就可以移动一个对象(假设一条彩色
我正在使用 botframework-directlinejs NodeJS SDK 为我的机器人前端实现一个新 channel 。该 channel 将提供一些自定义反向 channel 功能;但是
我正在使用直线执行 hql 查询。该作业似乎没有出现在 HDP 2.6 上 Spark History 服务器的资源管理器中。如何让它运行在 Yarn 上? 谢谢 最佳答案 Beeline 是一个 A
我目前正在尝试找到一种如何使用 Google Maps Api V3 获得直线路线的方法。 我已经设法使用地理编码和方向服务来获取从 A 点到 B 点的路线,包括两条替代路线。我还尝试过“无高速公路”
我有一个简单的 Canvas html5。它可以通过选择选项绘制一些形状,如直线、圆形、矩形、多边形,但现在我想让所有绘制都可拖动(如果可能的话)可调整大小,没有 3 部分库,只有纯 JS。
我正在使用在 Kerberos 中添加的帐户启动 beeline 来测试 Sentry: beeline -u "jdbc:hive2://IP:10000/;principal=test_table
我正在使用普通的 html/javascript Canvas (无库)。我有一种从两点计算 Angular 方法,我的理论是,我获取 Angular 并通过循环所需的水平距离 (x) 并对该 Ang
我是一名优秀的程序员,十分优秀!