bool Logika::sprawdzOtoczki(QPolygonF& otoczka1, QPolygonF& otoczka2) const{ if(!otoczka1.boundingRect().intersects(otoczka2.boundingRect())) return false; for(int i = 0; i < otoczka1.size(); i++){ int j = 1; while(j < otoczka2.size() && this->wyznacznikMacierzyWspolliniowosci(otoczka2[j - 1], otoczka2[j], otoczka1[i]) > 0.0) j++; if(j == otoczka2.size() && this->wyznacznikMacierzyWspolliniowosci(otoczka2.last(), otoczka2.first(), otoczka1[i]) > 0.0) return true; } for(int i = 0; i < otoczka2.size(); i++){ int j = 1; while(j < otoczka1.size() && this->wyznacznikMacierzyWspolliniowosci(otoczka1[j - 1], otoczka1[j], otoczka2[i]) > 0.0) j++; if(j == otoczka1.size() && this->wyznacznikMacierzyWspolliniowosci(otoczka1.last(), otoczka1.first(), otoczka2[i]) > 0.0) return true; } return false; }
void IsometricRenderer::drawTileSelection(QPainter *painter, const QRegion ®ion, const QColor &color, const QRectF &exposed) const { painter->setBrush(color); painter->setPen(Qt::NoPen); foreach (const QRect &r, region.rects()) { QPolygonF polygon = tileRectToPolygon(r); if (QRectF(polygon.boundingRect()).intersects(exposed)) painter->drawConvexPolygon(polygon); } }
QRectF QgsFillSymbolV2::polygonBounds( const QPolygonF& points, const QList<QPolygonF>* rings ) const { QRectF bounds = points.boundingRect(); if ( rings ) { QList<QPolygonF>::const_iterator it = rings->constBegin(); for ( ; it != rings->constEnd(); ++it ) { bounds = bounds.united(( *it ).boundingRect() ); } } return bounds; }
QRectF ModelCurve::rect() const { const QPointF &from = m_points_pos[0]; const QPointF &mid1 = m_points_pos[1]; const QPointF &mid2 = m_points_pos[2]; const QPointF &to = m_points_pos[3]; QPolygonF poly; poly << from << mid1 << mid2 << to; return poly.boundingRect() .normalized(); }
/*! \fn QList<QGraphicsItem *> QGraphicsSceneIndex::items(const QPolygonF &polygon, Qt::ItemSelectionMode mode, Qt::SortOrder order, const QTransform &deviceTransform) const \overload Returns all visible items that, depending on \a mode, are either inside or intersect with the specified \a polygon and return a list sorted using \a order. The default value for \a mode is Qt::IntersectsItemShape; all items whose exact shape intersects with or is contained by \a polygon are returned. \a deviceTransform is the transformation apply to the view. This method use the estimation of the index (estimateItems) and refine the list to get an exact result. If you want to implement your own refinement algorithm you can reimplement this method. \sa estimateItems() */ QList<QGraphicsItem *> QGraphicsSceneIndex::items(const QPolygonF &polygon, Qt::ItemSelectionMode mode, Qt::SortOrder order, const QTransform &deviceTransform) const { Q_D(const QGraphicsSceneIndex); QList<QGraphicsItem *> itemList; QRectF exposeRect = polygon.boundingRect(); _q_adjustRect(&exposeRect); QPainterPath path; path.addPolygon(polygon); d->pathIntersector->scenePath = path; d->items_helper(exposeRect, d->pathIntersector, &itemList, deviceTransform, mode, order); return itemList; }
QRectF OrthogonalRenderer::boundingRect(const MapObject *object) const { const QRectF bounds = object->bounds(); QRectF boundingRect; if (!object->cell().isEmpty()) { const QPointF bottomLeft = bounds.topLeft(); const Tile *tile = object->cell().tile; const QSize imgSize = tile->image().size(); const QPoint tileOffset = tile->tileset()->tileOffset(); boundingRect = QRectF(bottomLeft.x() + tileOffset.x(), bottomLeft.y() + tileOffset.y() - imgSize.height(), imgSize.width(), imgSize.height()).adjusted(-1, -1, 1, 1); } else { const qreal extraSpace = qMax(objectLineWidth() / 2, qreal(1)); switch (object->shape()) { case MapObject::Ellipse: case MapObject::Rectangle: if (bounds.isNull()) { boundingRect = bounds.adjusted(-10 - extraSpace, -10 - extraSpace, 10 + extraSpace + 1, 10 + extraSpace + 1); } else { const int nameHeight = object->name().isEmpty() ? 0 : 15; boundingRect = bounds.adjusted(-extraSpace, -nameHeight - extraSpace, extraSpace + 1, extraSpace + 1); } break; case MapObject::Polygon: case MapObject::Polyline: { const QPointF &pos = object->position(); const QPolygonF polygon = object->polygon().translated(pos); QPolygonF screenPolygon = pixelToScreenCoords(polygon); boundingRect = screenPolygon.boundingRect().adjusted(-extraSpace, -extraSpace, extraSpace + 1, extraSpace + 1); break; } } } return boundingRect; }
QTransform ImageTransformation::calcPostRotateXform(double const degrees) { QTransform xform; if (degrees != 0.0) { QPointF const origin(m_cropArea.boundingRect().center()); xform.translate(-origin.x(), -origin.y()); xform *= QTransform().rotate(degrees); xform *= QTransform().translate(origin.x(), origin.y()); // Calculate size changes. QPolygonF const pre_rotate_poly(m_cropXform.map(m_cropArea)); QRectF const pre_rotate_rect(pre_rotate_poly.boundingRect()); QPolygonF const post_rotate_poly(xform.map(pre_rotate_poly)); QRectF const post_rotate_rect(post_rotate_poly.boundingRect()); xform *= QTransform().translate( pre_rotate_rect.left() - post_rotate_rect.left(), pre_rotate_rect.top() - post_rotate_rect.top() ); } return xform; }
QgsComposerNodesItem::QgsComposerNodesItem( QString tagName, QPolygonF polygon, QgsComposition* c ) : QgsComposerItem( c ) , mTagName( tagName ) , mSelectedNode( -1 ) , mDrawNodes( false ) { const QRectF boundingRect = polygon.boundingRect(); setSceneRect( boundingRect ); const QPointF topLeft = boundingRect.topLeft(); mPolygon = polygon.translated( -topLeft ); }
QRectF Stroke::boundingRect() const { QPolygonF tmpPoints = points; QRectF bRect = tmpPoints.boundingRect(); qreal maxPressure = 0.0; for (qreal p : pressures) { if (p > maxPressure) { maxPressure = p; } } qreal pad = maxPressure * penWidth; return bRect.adjusted(-pad, -pad, pad, pad); }
QRegion KRITAIMAGE_EXPORT splitTriangles(const QPointF ¢er, const QVector<QPointF> &points) { Q_ASSERT(points.size()); Q_ASSERT(!(points.size() & 1)); QVector<QPolygonF> triangles; QRect totalRect; for (int i = 0; i < points.size(); i += 2) { QPolygonF triangle; triangle << center; triangle << points[i]; triangle << points[i+1]; totalRect |= triangle.boundingRect().toAlignedRect(); triangles << triangle; } const int step = 64; const int right = totalRect.x() + totalRect.width(); const int bottom = totalRect.y() + totalRect.height(); QRegion dirtyRegion; for (int y = totalRect.y(); y < bottom;) { int nextY = qMin((y + step) & ~(step-1), bottom); for (int x = totalRect.x(); x < right;) { int nextX = qMin((x + step) & ~(step-1), right); QRect rect(x, y, nextX - x, nextY - y); foreach(const QPolygonF &triangle, triangles) { if(checkInTriangle(rect, triangle)) { dirtyRegion |= rect; break; } } x = nextX; } y = nextY; } return dirtyRegion; }
QPainterPath BallItem::path() const { QPainterPath path; QPolygonF collidingPlgn = collidingPolygon(); QMatrix m; m.rotate(rotation()); QPointF firstP = collidingPlgn.at(0); collidingPlgn.translate(-firstP.x(), -firstP.y()); path.addEllipse(collidingPlgn.boundingRect()); path = m.map(path); path.translate(firstP.x(), firstP.y()); return path; }
void QgsFillSymbolLayerV2::_renderPolygon( QPainter* p, const QPolygonF& points, const QList<QPolygonF>* rings, QgsSymbolV2RenderContext& context ) { if ( !p ) { return; } // Disable 'Antialiasing' if the geometry was generalized in the current RenderContext (We known that it must have least #5 points). if ( points.size() <= 5 && ( context.renderContext().vectorSimplifyMethod().simplifyHints() & QgsVectorSimplifyMethod::AntialiasingSimplification ) && QgsAbstractGeometrySimplifier::isGeneralizableByDeviceBoundingBox( points, context.renderContext().vectorSimplifyMethod().threshold() ) && ( p->renderHints() & QPainter::Antialiasing ) ) { p->setRenderHint( QPainter::Antialiasing, false ); p->drawRect( points.boundingRect() ); p->setRenderHint( QPainter::Antialiasing, true ); return; } // polygons outlines are sometimes rendered wrongly with drawPolygon, when // clipped (see #13343), so use drawPath instead. if ( !rings && p->pen().style() == Qt::NoPen ) { // simple polygon without holes p->drawPolygon( points ); } else { // polygon with holes must be drawn using painter path QPainterPath path; QPolygonF outerRing = points; path.addPolygon( outerRing ); if ( rings ) { QList<QPolygonF>::const_iterator it = rings->constBegin(); for ( ; it != rings->constEnd(); ++it ) { QPolygonF ring = *it; path.addPolygon( ring ); } } p->drawPath( path ); } }
QRectF clipRect() const { // Start with an invalid rect. QRectF resultRect(0, 0, -1, -1); for (const QSGClipNode* clip = clipList(); clip; clip = clip->clipList()) { QMatrix4x4 clipMatrix; if (pageNode()->devicePixelRatio() != 1.0) { clipMatrix.scale(pageNode()->devicePixelRatio()); if (clip->matrix()) clipMatrix *= (*clip->matrix()); } else if (clip->matrix()) clipMatrix = *clip->matrix(); QRectF currentClip; if (clip->isRectangular()) currentClip = clipMatrix.mapRect(clip->clipRect()); else { const QSGGeometry* geometry = clip->geometry(); // Assume here that clipNode has only coordinate data. const QSGGeometry::Point2D* geometryPoints = geometry->vertexDataAsPoint2D(); // Clip region should be at least triangle to make valid clip. if (geometry->vertexCount() < 3) continue; QPolygonF polygon; for (int i = 0; i < geometry->vertexCount(); i++) polygon.append(clipMatrix.map(QPointF(geometryPoints[i].x, geometryPoints[i].y))); currentClip = polygon.boundingRect(); } if (currentClip.isEmpty()) continue; if (resultRect.isValid()) resultRect &= currentClip; else resultRect = currentClip; } return resultRect; }
void StaggeredRenderer::drawTileSelection(QPainter *painter, const QRegion ®ion, const QColor &color, const QRectF &exposed) const { painter->setBrush(color); painter->setPen(Qt::NoPen); foreach (const QRect &r, region.rects()) { for (int y = r.top(); y <= r.bottom(); ++y) { for (int x = r.left(); x <= r.right(); ++x) { const QPolygonF polygon = tileToPolygon(x, y); if (QRectF(polygon.boundingRect()).intersects(exposed)) painter->drawConvexPolygon(polygon); } } } }
bool DataConverter::digitalPattern_Scale(QPolygonF &points, qreal width, qreal height) { QRectF boundingRect; qreal originalWidth, originalHeight; qreal widthScaleRate, heightScaleRate; int i; if (width <= 0.0 || height <= 0.0) { qDebug("[DataConverter::digitalPattern_Scale] Error: Width: [%f], Height: [%f]", width, height); return false; } int count = points.size(); if (count < 10) { qDebug("[DataConverter::digitalPattern_Scale] Error: Polygon Point Count: [%d]", points.size()); return false; } boundingRect = points.boundingRect(); originalWidth = boundingRect.width(); originalHeight = boundingRect.height(); widthScaleRate = width / originalWidth; heightScaleRate = height / originalHeight; if (width == originalWidth && height == originalHeight) return true; for (i = 0; i < count; i ++) { points[i].setX(points.at(i).x() * widthScaleRate); points[i].setY(points.at(i).y() * heightScaleRate); } //qDebug("*** Width: [%f] -> [%f]", originalWidth, points.boundingRect().width()); //qDebug("*** Height: [%f] -> [%f]", originalHeight, points.boundingRect().height()); return true; }
// ============================================================================ /// Splits polygon in two QList<QPolygonF> splitPolygonRandomly( const QPolygonF& polygon, QLineF* pdebugout ) { QRectF br = polygon.boundingRect(); // get random angle on cutting double angle = random01()* 6.28; // cut throught the center QPointF center = br.center(); double upper = br.width() + br.height(); // upper bound for any of the polygon's dimensions QPointF p1( center.x() - upper*sin(angle), center.y() - upper*cos(angle) ); QPointF p2( center.x() + upper*sin(angle), center.y() + upper*cos(angle) ); // debug if ( pdebugout ) *pdebugout = QLineF( p1, p2 ); return splitPolygon( polygon, QLineF( p1, p2 ) ); }
QRectF OrthogonalRenderer::boundingRect(const MapObject *object) const { const QRectF bounds = object->bounds(); const QRectF rect(tileToPixelCoords(bounds.topLeft()), tileToPixelCoords(bounds.bottomRight())); QRectF boundingRect; if (!object->cell().isEmpty()) { const QPointF bottomLeft = rect.topLeft(); const Tile *tile = object->cell().tile; const QSize imgSize = tile->image().size(); const QPoint tileOffset = tile->tileset()->tileOffset(); boundingRect = QRectF(bottomLeft.x() + tileOffset.x(), bottomLeft.y() + tileOffset.y() - imgSize.height(), imgSize.width(), imgSize.height()).adjusted(-1, -1, 1, 1); } else { // The -2 and +3 are to account for the pen width and shadow switch (object->shape()) { case MapObject::Ellipse: case MapObject::Rectangle: if (rect.isNull()) { boundingRect = rect.adjusted(-10 - 2, -10 - 2, 10 + 3, 10 + 3); } else { const int nameHeight = object->name().isEmpty() ? 0 : 15; boundingRect = rect.adjusted(-2, -nameHeight - 2, 3, 3); } break; case MapObject::Polygon: case MapObject::Polyline: { const QPointF &pos = object->position(); const QPolygonF polygon = object->polygon().translated(pos); QPolygonF screenPolygon = tileToPixelCoords(polygon); boundingRect = screenPolygon.boundingRect().adjusted(-2, -2, 3, 3); break; } } } return boundingRect; }
void QGraphicsVolumeView::moveToVolume(int i) { if (i >= _volumeDisplays.size()) { return; } QGraphicsPixmapItem* firstItem = _volumeDisplays[i].GetSliceData<QGraphicsPixmapItem>(0); if (firstItem == NULL) { return; } QRect contentsRect = viewport()->contentsRect(); QPolygonF visibleScene = mapToScene(contentsRect); QRectF visibleRect = visibleScene.boundingRect(); QPointF firstPosition = firstItem->pos(); visibleRect.moveTop(firstPosition.y()); this->ensureVisible(visibleRect, 0, 0); // QPoint displacement = mapFromScene(0, firstPosition.y() - visibleRect.top()); // this->scroll(0, displacement.y()); }
Box2DRobot::Box2DRobot(Box2DPhysicsEngine *engine, twoDModel::model::RobotModel * const robotModel , b2Vec2 pos, float angle) : mModel(robotModel) , mEngine(engine) , mWorld(engine->box2DWorld()) { b2BodyDef bodyDef; bodyDef.position = pos; bodyDef.angle = angle; bodyDef.type = b2_dynamicBody; mBody = mWorld.CreateBody(&bodyDef); b2FixtureDef robotFixture; b2PolygonShape polygonShape; QPolygonF collidingPolygon = mModel->info().collidingPolygon(); QPointF localCenter = collidingPolygon.boundingRect().center(); mPolygon = new b2Vec2[collidingPolygon.size()]; for (int i = 0; i < collidingPolygon.size(); ++i) { mPolygon[i] = engine->positionToBox2D(collidingPolygon.at(i) - localCenter); } polygonShape.Set(mPolygon, collidingPolygon.size()); robotFixture.shape = &polygonShape; robotFixture.density = engine->computeDensity(collidingPolygon, mModel->info().mass()); robotFixture.friction = mModel->info().friction(); robotFixture.restitution = 0.6; mBody->CreateFixture(&robotFixture); mBody->SetUserData(this); mBody->SetAngularDamping(1.0f); mBody->SetLinearDamping(1.0f); connectWheels(); for (int i = 0; i < polygonShape.GetVertexCount(); ++i) { mDebuggingDrawPolygon.append(engine->positionToScene(polygonShape.GetVertex(i) + mBody->GetPosition())); } if (!mDebuggingDrawPolygon.isEmpty() & !mDebuggingDrawPolygon.isClosed()) { mDebuggingDrawPolygon.append(mDebuggingDrawPolygon.first()); } }
QRectF IsometricRenderer::boundingRect(const MapObject *object) const { if (object->tile()) { const QPointF bottomCenter = tileToPixelCoords(object->position()); const QImage img = object->toImage(); return QRectF(bottomCenter.x() - img.width() / 2, bottomCenter.y() - img.height(), img.width(), img.height()).adjusted(-1, -1, 1, 1); } else if (!object->polygon().isEmpty()) { const QPointF &pos = object->position(); const QPolygonF polygon = object->polygon().translated(pos); const QPolygonF screenPolygon = tileToPixelCoords(polygon); return screenPolygon.boundingRect().adjusted(-2, -2, 3, 3); } else { // Take the bounding rect of the projected object, and then add a few // pixels on all sides to correct for the line width. const QRectF base = tileRectToPolygon(object->bounds()).boundingRect(); return base.adjusted(-2, -3, 2, 2); } }
void RandomLayout::layout() { int margin = 2*vertexdraws[0]->radius(); QRect viewRect = graphdraw->rect().adjusted(margin, margin, -margin, -margin); // TODO: adjust for scroll bars also... QPolygonF polygon = graphdraw->mapToScene(viewRect); QRectF rect = polygon.boundingRect(); QPointF point; foreach(VertexDraw* vertexdraw, vertexdraws) { do { point = QPointF(qrand() / (RAND_MAX + 1.0) * (rect.right() + 1 - rect.left()) + rect.left(), qrand() / (RAND_MAX + 1.0) * (rect.bottom() + 1 - rect.top()) + rect.top()); } while(!polygon.containsPoint(point, Qt::OddEvenFill)); vertexdraw->setPos(point); } }
QRectF IsometricRenderer::boundingRect(const MapObject *object) const { if (!object->cell().isEmpty()) { const QPointF bottomCenter = pixelToScreenCoords(object->position()); const Tile *tile = object->cell().tile; const QSize imgSize = tile->image().size(); const QPoint tileOffset = tile->offset(); const QSizeF objectSize = object->size(); const QSizeF scale(objectSize.width() / imgSize.width(), objectSize.height() / imgSize.height()); return QRectF(bottomCenter.x() + (tileOffset.x() * scale.width()) - objectSize.width() / 2, bottomCenter.y() + (tileOffset.y() * scale.height()) - objectSize.height(), objectSize.width(), objectSize.height()).adjusted(-1, -1, 1, 1); } else if (!object->polygon().isEmpty()) { qreal extraSpace = qMax(objectLineWidth(), qreal(1)); // Make some more room for the starting dot extraSpace += objectLineWidth() * 4; const QPointF &pos = object->position(); const QPolygonF polygon = object->polygon().translated(pos); const QPolygonF screenPolygon = pixelToScreenCoords(polygon); return screenPolygon.boundingRect().adjusted(-extraSpace, -extraSpace - 1, extraSpace, extraSpace); } else { // Take the bounding rect of the projected object, and then add a few // pixels on all sides to correct for the line width. const QRectF base = pixelRectToScreenPolygon(object->bounds()).boundingRect(); const qreal extraSpace = qMax(objectLineWidth() / 2, qreal(1)); return base.adjusted(-extraSpace, -extraSpace - 1, extraSpace, extraSpace); } }
void MirrorItem::sourceChanged() { // find out the item's polygon in scene coordinates QRectF itemRect = m_source->boundingRect(); QPolygonF itemScenePolygon = m_source->mapToScene(itemRect); QRect itemSceneRect = itemScenePolygon.boundingRect().toRect(); // reposition setPos(itemSceneRect.bottomLeft()); // find out the new bounding rect QRectF newBr(0.0, 0.0, itemSceneRect.width(), qMin(itemSceneRect.height(), MIRROR_HEIGHT)); // if the bounding rect changed, resize if (newBr != m_boundingRect) { prepareGeometryChange(); m_boundingRect = newBr; } // invalidate current rendering m_dirty = true; update(); }
void QPicturePaintEngine::drawPolygon(const QPointF *points, int numPoints, PolygonDrawMode mode) { Q_D(QPicturePaintEngine); #ifdef QT_PICTURE_DEBUG qDebug() << " -> drawPolygon(): size=" << numPoints; #endif int pos; QPolygonF polygon; for (int i=0; i<numPoints; ++i) polygon << points[i]; if (mode == PolylineMode) { SERIALIZE_CMD(QPicturePrivate::PdcDrawPolyline); d->s << polygon; } else { SERIALIZE_CMD(QPicturePrivate::PdcDrawPolygon); d->s << polygon; d->s << (qint8)(mode == OddEvenMode ? 0 : 1); } writeCmdLength(pos, polygon.boundingRect(), true); }
void Box2DRobot::reinitSensor(const twoDModel::view::SensorItem &sensor) { // box2d doesn't rotate or shift elements, which are connected to main robot body via joints in case // when we manually use method SetTransform. // So we need to handle elements such as sensors by hand. // We use this method in case when user shifts or rotates sensor(s). auto box2dSensor = mSensors[&sensor]; box2dSensor->getBody()->SetLinearVelocity({0, 0}); box2dSensor->getBody()->SetAngularVelocity(0); if (const b2JointEdge *jointList = box2dSensor->getBody()->GetJointList()) { auto joint = jointList->joint; mJoints.removeAll(joint); mWorld.DestroyJoint(joint); } QPolygonF collidingPolygon = sensor.collidingPolygon(); QPointF localCenter = collidingPolygon.boundingRect().center(); QPointF deltaToCenter = mModel->rotationCenter() - mModel->position(); QPointF localPos = sensor.pos() - deltaToCenter; QTransform transform; QPointF dif = mModel->rotationCenter(); transform.translate(-dif.x(), -dif.y()); transform.rotate(mModel->rotation()); localPos = transform.map(localPos); transform.reset(); transform.translate(dif.x(), dif.y()); localPos = transform.map(localPos); const b2Vec2 pos = mEngine->positionToBox2D(localPos - localCenter + mModel->rotationCenter()); // IMPORTANT: we connect every sensor with box2d circle item. // So rotation of sensor doesn't matter, we set rotation corresponding to robot. // if in future it will be changed, you'll see some strange behavior, because of joints. See connectSensor method. mSensors[&sensor]->getBody()->SetTransform(pos, mBody->GetAngle()); connectSensor(*mSensors[&sensor]); }
QPolygonF caGraphics::rotateObject(int degrees, int w, int h, int linesize, const QPolygonF& object) { // rotate double resizeWidth=999.0, resizeHeight=999.0; QTransform transform = QTransform().rotate(degrees); QPolygonF rotated = transform.map(object); //printf("w,h of object %f %f\n",object.boundingRect().width(), object.boundingRect().height()); //printf("w,h of rotated %f %f\n",rotated.boundingRect().width(), rotated.boundingRect().height()); // calculate resize factor so that polygon will fit into the designer rectangle if(rotated.boundingRect().width() > 0) { resizeWidth = (double)(w) / rotated.boundingRect().width(); } if(rotated.boundingRect().height() > 0) { resizeHeight = (double)(h) / rotated.boundingRect().height(); } double resize = qMin(resizeWidth, resizeHeight); //printf("resize %f %f final=%f\n",resizeWidth, resizeHeight, resize); transform = QTransform().scale(resize, resize); rotated = transform.map(rotated); // get center of rotated polygon double centerX = rotated.boundingRect().x() + rotated.boundingRect().width()/2.0; double centerY = rotated.boundingRect().y() + rotated.boundingRect().height()/2.0; //printf("center of rotated polygon %f %f\n", centerX, centerY); // get center of canvas double centerXorg = w/2.0; double centerYorg = h/2.0; //printf("center of canvas %f %f\n", centerXorg, centerYorg); // calculate translation double translateX = centerX - centerXorg - linesize/2; double translateY = centerY - centerYorg - linesize/2; //printf("translate %f %f\n", translateX, translateY); transform = QTransform().translate(-translateX, -translateY); rotated = transform.map(rotated); return rotated; }
qreal FlyThroughTask::calculateFlightPerformance(const QList<Position> &positions, const QPolygonF &geoPoly, const UAVParameters &) { //First, see if one of the points is within the polygon foreach(const Position& pos, positions) { if (geoPoly.containsPoint(pos.lonLat(), Qt::OddEvenFill)) return this->maxTaskPerformance(); } //if that fails, take the distance to the last point Position goalPos(geoPoly.boundingRect().center(), positions.first().altitude()); const Position& last = positions.last(); QVector3D enuPos = Conversions::lla2enu(last, goalPos); qreal dist = enuPos.length(); const qreal stdDev = 90.0; qreal toRet = 100*FlightTask::normal(dist,stdDev,2000); return qMin<qreal>(toRet,this->maxTaskPerformance()); }
void QgsComposerItem::sizeChangedByRotation( double& width, double& height, double rotation ) { if ( rotation == 0.0 ) { return; } //vector to p1 double x1 = -width / 2.0; double y1 = -height / 2.0; rotate( rotation, x1, y1 ); //vector to p2 double x2 = width / 2.0; double y2 = -height / 2.0; rotate( rotation, x2, y2 ); //vector to p3 double x3 = width / 2.0; double y3 = height / 2.0; rotate( rotation, x3, y3 ); //vector to p4 double x4 = -width / 2.0; double y4 = height / 2.0; rotate( rotation, x4, y4 ); //double midpoint QPointF midpoint( width / 2.0, height / 2.0 ); QPolygonF rotatedRectPoly; rotatedRectPoly << QPointF( midpoint.x() + x1, midpoint.y() + y1 ); rotatedRectPoly << QPointF( midpoint.x() + x2, midpoint.y() + y2 ); rotatedRectPoly << QPointF( midpoint.x() + x3, midpoint.y() + y3 ); rotatedRectPoly << QPointF( midpoint.x() + x4, midpoint.y() + y4 ); QRectF boundingRect = rotatedRectPoly.boundingRect(); width = boundingRect.width(); height = boundingRect.height(); }
void IsometricRenderer::drawMapObject(QPainter *painter, const MapObject *object, const QColor &color) const { painter->save(); QPen pen(Qt::black); pen.setCosmetic(true); const Cell &cell = object->cell(); if (!cell.isEmpty()) { const Tile *tile = cell.tile; const QSize imgSize = tile->size(); const QPointF pos = pixelToScreenCoords(object->position()); const QPointF tileOffset = tile->tileset()->tileOffset(); // Draw the name before the transform is applied const QFontMetrics fm = painter->fontMetrics(); QString name = fm.elidedText(object->name(), Qt::ElideRight, imgSize.width() + 2); if (!name.isEmpty()) { const QPointF textPos = pos + tileOffset - QPointF(imgSize.width() / 2, 5 + imgSize.height()); painter->drawText(textPos + QPointF(1, 1), name); painter->setPen(color); painter->drawText(textPos, name); } CellRenderer(painter).render(cell, pos, CellRenderer::BottomCenter); if (testFlag(ShowTileObjectOutlines)) { QRectF rect(QPointF(pos.x() - imgSize.width() / 2 + tileOffset.x(), pos.y() - imgSize.height() + tileOffset.y()), imgSize); pen.setStyle(Qt::SolidLine); painter->setPen(pen); painter->drawRect(rect); pen.setStyle(Qt::DotLine); pen.setColor(color); painter->setPen(pen); painter->drawRect(rect); } } else { const qreal lineWidth = objectLineWidth(); const qreal scale = painterScale(); const qreal shadowOffset = (lineWidth == 0 ? 1 : lineWidth) / scale; QColor brushColor = color; brushColor.setAlpha(50); QBrush brush(brushColor); pen.setJoinStyle(Qt::RoundJoin); pen.setCapStyle(Qt::RoundCap); pen.setWidth(lineWidth); painter->setPen(pen); painter->setRenderHint(QPainter::Antialiasing); // TODO: Draw the object name // TODO: Do something sensible to make null-sized objects usable switch (object->shape()) { case MapObject::Ellipse: { QPointF topLeft(pixelToScreenCoords(object->bounds().topLeft())); QPointF bottomLeft(pixelToScreenCoords(object->bounds().bottomLeft())); QPointF topRight(pixelToScreenCoords(object->bounds().topRight())); const qreal headerX = bottomLeft.x(); const qreal headerY = topLeft.y(); QRectF rect(bottomLeft, topRight); const QFontMetrics fm = painter->fontMetrics(); QString name = fm.elidedText(object->name(), Qt::ElideRight, rect.width() + 2); QPolygonF polygon = pixelRectToScreenPolygon(object->bounds()); float tw = map()->tileWidth(); float th = map()->tileHeight(); QPointF transformScale(1, 1); if (tw > th) transformScale = QPointF(1, th/tw); else transformScale = QPointF(tw/th, 1); QPointF l1 = polygon.at(1) - polygon.at(0); QPointF l2 = polygon.at(3) - polygon.at(0); QTransform trans; trans.scale(transformScale.x(), transformScale.y()); trans.rotate(45); QTransform iTrans = trans.inverted(); QPointF l1x = iTrans.map(l1); QPointF l2x = iTrans.map(l2); QSizeF ellipseSize(l1x.manhattanLength(), l2x.manhattanLength()); if (ellipseSize.width() > 0 && ellipseSize.height() > 0) { painter->save(); painter->setPen(pen); painter->translate(polygon.at(0)); painter->scale(transformScale.x(), transformScale.y()); painter->rotate(45); painter->drawEllipse(QRectF(QPointF(0, 0), ellipseSize)); painter->restore(); } painter->setBrush(Qt::NoBrush); painter->drawPolygon(polygon); if (!name.isEmpty()) painter->drawText(QPoint(headerX, headerY - 5), name); pen.setColor(color); painter->setPen(pen); painter->setBrush(Qt::NoBrush); painter->translate(QPointF(0, -shadowOffset)); painter->drawPolygon(polygon); painter->setBrush(brush); if (ellipseSize.width() > 0 && ellipseSize.height() > 0) { painter->save(); painter->translate(polygon.at(0)); painter->scale(transformScale.x(), transformScale.y()); painter->rotate(45); painter->drawEllipse(QRectF(QPointF(0, 0), ellipseSize)); painter->restore(); } if (!name.isEmpty()) painter->drawText(QPoint(headerX, headerY - 5), name); break; } case MapObject::Rectangle: { QPointF topLeft(pixelToScreenCoords(object->bounds().topLeft())); QPointF bottomLeft(pixelToScreenCoords(object->bounds().bottomLeft())); QPointF topRight(pixelToScreenCoords(object->bounds().topRight())); const qreal headerX = bottomLeft.x(); const qreal headerY = topLeft.y(); QRectF rect(bottomLeft, topRight); const QFontMetrics fm = painter->fontMetrics(); QString name = fm.elidedText(object->name(), Qt::ElideRight, rect.width() + 2); QPolygonF polygon = pixelRectToScreenPolygon(object->bounds()); painter->drawPolygon(polygon); if (!name.isEmpty()) painter->drawText(QPointF(headerX, headerY - 5 + shadowOffset), name); pen.setColor(color); painter->setPen(pen); painter->setBrush(brush); polygon.translate(0, -shadowOffset); painter->drawPolygon(polygon); if (!name.isEmpty()) painter->drawText(QPointF(headerX, headerY - 5), name); break; } case MapObject::Polygon: { const QPointF &pos = object->position(); const QPolygonF polygon = object->polygon().translated(pos); QPolygonF screenPolygon = pixelToScreenCoords(polygon); const QRectF polygonBoundingRect = screenPolygon.boundingRect(); const QFontMetrics fm = painter->fontMetrics(); QString name = fm.elidedText(object->name(), Qt::ElideRight, polygonBoundingRect.width() + 2); if (!name.isEmpty()) painter->drawText(QPointF(polygonBoundingRect.left(), polygonBoundingRect.top() - 5 + shadowOffset), name); painter->drawPolygon(screenPolygon); pen.setColor(color); painter->setPen(pen); painter->setBrush(brush); screenPolygon.translate(0, -shadowOffset); painter->drawPolygon(screenPolygon); if (!name.isEmpty()) painter->drawText(QPointF(polygonBoundingRect.left(), polygonBoundingRect.top() - 5), name); break; } case MapObject::Polyline: { const QPointF &pos = object->position(); const QPolygonF polygon = object->polygon().translated(pos); QPolygonF screenPolygon = pixelToScreenCoords(polygon); painter->drawPolyline(screenPolygon); pen.setColor(color); painter->setPen(pen); screenPolygon.translate(0, -shadowOffset); painter->drawPolyline(screenPolygon); break; } } } painter->restore(); }
void TransformableGraphicsGuide::update() { if (isVisible()) { FigureEditor::EditMode mode = editor->mode(); bool scaleMode( mode == FigureEditor::Scale ); topRightRect.setVisible(scaleMode); topLeftRect.setVisible(scaleMode); bottomRightRect.setVisible(scaleMode); bottomLeftRect.setVisible(scaleMode); if (scaleMode) { QPointF cen; QPolygonF poly; if (editor->hasSelection()) { QGraphicsPolygonItem* item = editor->selection(); if (item == 0) return; cen = mapFromScene(editor->selectionTransformPos()); poly = mapFromScene(item->polygon()); } else { QGraphicsPolygonItem* item = dynamic_cast<QGraphicsPolygonItem*>(parentItem()); cen = editor->triangleTransformPos(); poly = item->polygon(); } QRectF f( poly.boundingRect() ); qreal xmax = qMax(qAbs(f.left() - cen.x()), qAbs(f.right() - cen.x())); qreal ymax = qMax(qAbs(f.top() - cen.y()), qAbs(f.bottom() - cen.y())); QPointF pmax(xmax, ymax); QRectF r(pmax, -pmax); r.moveCenter(cen); outerRect = r; QRectF l = parentItem()->mapRectFromScene(QRectF(QPointF(0.0, 0.0), QSizeF(10, 10))); QPen pen( editor->guideColor() ); l.moveBottomLeft(r.topRight()); topRightRect.setPen(pen); topRightRect.setRect(l); l.moveBottomRight(r.topLeft()); topLeftRect.setPen(pen); topLeftRect.setRect(l); l.moveTopLeft(r.bottomRight()); bottomRightRect.setPen(pen); bottomRightRect.setRect(l); l.moveTopRight(r.bottomLeft()); bottomLeftRect.setPen(pen); bottomLeftRect.setRect(l); } else if (mode == FigureEditor::Rotate) { QGraphicsPolygonItem* item; QPointF cen; QPolygonF poly; if (editor->hasSelection()) { item = editor->selection(); if (item == 0) return; cen = mapFromScene(editor->selectionTransformPos()); poly = mapFromScene(item->polygon()); } else { item = dynamic_cast<QGraphicsPolygonItem*>(parentItem()); poly = item->polygon(); cen = editor->triangleTransformPos(); } qreal rmax = 0.0; foreach (QPointF p, poly) { QLineF l(p, cen); qreal len(l.length()); if (len > rmax) rmax = len; } qreal height = rmax * 2.0; outerRect = QRectF(cen.x() - rmax, cen.y() - rmax, height, height); }