void Editor::onMapReady() { myUi->graphicsView->scene()->setSceneRect(0, 0, myGame->generator()->width(), myGame->generator()->height()); for (unsigned int i = 0; i < myGame->generator()->regions().size(); i++) { QPolygonF qring, sqring; auto ring = boost::geometry::exterior_ring(myGame->generator()->regions()[i].poly); boost::geometry::convert(ring, qring); boost::geometry::simplify(qring, sqring, 1); QPen pen; pen.setColor(QColor(0,0,0)); pen.setStyle(Qt::PenStyle::DashLine); QBrush brush; brush.setStyle(Qt::BrushStyle::SolidPattern); std::string colour = myGame->biomes()[myGame->generator()->regions()[i].biome]->jsonValue["mapColour"].asString(); pen.setColor(QColor(colour.c_str())); brush.setColor(QColor(colour.c_str())); QGraphicsPolygonItem * pi = new RegionPolygonItem(this); pi->setPen(pen); pi->setBrush(brush); pi->setPolygon(sqring); QPolygonF convex; boost::geometry::convex_hull(sqring, convex); double area = boost::geometry::area(convex); pi->setZValue(-area); pi->setData(0, i); pi->setFlag(QGraphicsItem::GraphicsItemFlag::ItemIsFocusable, true); pi->setFlag(QGraphicsItem::GraphicsItemFlag::ItemIsSelectable, true); myUi->graphicsView->scene()->addItem(pi); } }
void MainWindow::slotAddPolygonItem() //在场景中加入一个多边形图元 { QVector<QPoint> v; v<<QPoint(30,-15)<<QPoint(0,-30)<<QPoint(-30,-15)<<QPoint(-30,15)<<QPoint(0,30)<<QPoint(30,15); QGraphicsPolygonItem *item = new QGraphicsPolygonItem(QPolygonF(v)); item->setBrush(QColor(qrand()%256,qrand()%256,qrand()%256)); item->setFlag(QGraphicsItem::ItemIsMovable); scene->addItem(item); item->setPos((qrand()%int(scene->sceneRect().width()))-200,(qrand()%int(scene->sceneRect().height()))-200); }
void Main::addPolygon() { const int size = int(canvas.width()/2); Q3PointArray pa(6); pa[0] = QPoint(0,0); pa[1] = QPoint(size,size/5); pa[2] = QPoint(size*4/5,size); pa[3] = QPoint(size/6,size*5/4); pa[4] = QPoint(size*3/4,size*3/4); pa[5] = QPoint(size*3/4,size/4); QGraphicsPolygonItem* i = canvas.addPolygon(pa); i->setFlag(QGraphicsItem::ItemIsMovable); i->setPen(Qt::NoPen); i->setBrush( QColor(qrand()%32*8,qrand()%32*8,qrand()%32*8) ); i->setPos(qrand()%int(canvas.width()),qrand()%int(canvas.height())); i->setZValue(qrand()%256); }