// **************************************************************************** // // Function Name: RGpDrawingSurface::SolidPolygon( ) // // Description: Fills and frames a solid polygon // // Returns: Nothing // // Exceptions: None // // **************************************************************************** // void RGpDrawingSurface::SolidPolygon( const RIntPoint* pPoints, YPointCount numPoints ) { PolyHandle hPoly; TpsAssert( ( numPoints < kMaxPoints ), "Too many points." ); if ( numPoints == 2 ) Polyline( pPoints, numPoints ); else { hPoly = CreatePolygon( pPoints, numPoints ); CreateBrush( ); ::FillPoly( hPoly, &qd.black ); CreatePen( ); // offset polygon to simulate windows center pixel stroke if ( m_PenWidthOffset != 0 ) ::OffsetPoly( hPoly, -m_PenWidthOffset, -m_PenWidthOffset ); ::FramePoly( hPoly ); ::DisposeHandle( (Handle)hPoly ); } }
// Tao da giac deu voi // iIdentity > 2: nhan dang da giac deu, = 5: ngu giac deu, = 6: luc giac deu ... // dAlpha: diem bat dau cua da giac deu co goc la dAlpha // (x,y) tam cua da giac deu // iRadius: ban kinh // iColor: mau cua duong bien da giac deu void _Polygon::CreateEquilateralPolygon(int iIdentity, double dAlpha, int x, int y, int iRadius, int iColor) { CString _strError(""); if (iIdentity <= 2) { _strError = "_Polygon::CreateEquilateralPolygon() --> iIdentity: Khong phai da giac"; iIdentity = 3; } CArray<POINT> arrTemp; POINT temp; double dAngle = 2*PI / iIdentity; // goc quay // Tao cac dinh cua da giac deu for (int i = 0; i < iIdentity; ++i) { temp.x = iRadius * cos(dAlpha + dAngle * i) + x; temp.y = iRadius * sin(dAlpha + dAngle * i) + y; arrTemp.Add(temp); } CreatePolygon(arrTemp, iColor); strError += _strError; // Gan loi }
void paint( QPainter *painter, const QStyleOptionGraphicsItem * /*option*/, QWidget * /*widget*/ ) { painter->setBrush(m_brush); painter->drawPolygon(CreatePolygon()); }
// **************************************************************************** // // Function Name: RGpDrawingSurface::DeviceInvertPolygon( ) // // Description: Inverts a polygon // // Returns: Nothing // // Exceptions: None // // **************************************************************************** // void RGpDrawingSurface::DeviceInvertPolygon( const RIntPoint* pPoints, YPointCount numPoints ) { PolyHandle hPoly; TpsAssert( ( numPoints < kMaxPoints ), "Too many points." ); if ( numPoints == 2 ) { TpsAssertAlways( "Cannot invert a 2point polygon" ); } else { hPoly = CreatePolygon( pPoints, numPoints ); ::InvertPoly( hPoly ); ::DisposeHandle( (Handle)hPoly ); } }
void TestPolygon() { Point point[7] = { { 100, 100 }, { 100, 600 }, { 200, 600 }, //{ 200, 350 }, { 500, 350 }, { 500, 600 }, { 600, 600 }, { 600, 100 }, }; Polygon* polygon = CreatePolygon(7, point); PrintPolygon(polygon); /* for (int i = 0; i < 50; ++i) { Point point = { (rand() % (800 * 10000) / 10000.f), (rand() % (640 * 10000) / 10000.f) }; printf("circle(%f, %f, %f, \"%s\");\n", point.x, point.y, 5.f, PointInPolygon(&polygon, &point) ? "red" : "yellow"); }*/ Circle circle[200] = { 0 }; for (int i = 0; i < 200; ++i) { float x = (rand() % (800 * 10000) / 10000.f); float y = (rand() % (640 * 10000) / 10000.f); float r = 20.f; circle[i].c.x = x; circle[i].c.y = y; circle[i].r = r; } for (int i = 0; i < 200; ++i) { PrintCircle(circle[i], IsCircleIntersectPolygon(&circle[i], polygon)); } DeletePolygon(&polygon); }
// **************************************************************************** // // Function Name: RGpDrawingSurface::SolidFillPolygon( ) // // Description: Fills a solid polygon // // Returns: Nothing // // Exceptions: None // // **************************************************************************** // void RGpDrawingSurface::SolidFillPolygon( const RIntPoint* pPoints, YPointCount numPoints ) { PolyHandle hPoly; TpsAssert( ( numPoints < kMaxPoints ), "Too many points." ); if ( numPoints == 2 ) { Polyline( pPoints, numPoints ); // REVEIW RAH should use fill color } else { hPoly = CreatePolygon( pPoints, numPoints ); CreateBrush( ); ::FillPoly( hPoly, &qd.black ); ::DisposeHandle( (Handle)hPoly ); } }
// **************************************************************************** // // Function Name: RGpDrawingSurface::DeviceInvertPolyPolygon( ) // // Description: Inverts a poly polygon // // Returns: Nothing // // Exceptions: None // // **************************************************************************** // void RGpDrawingSurface::DeviceInvertPolyPolygon( const RIntPoint* pPoints, YPointCount* pointCountArray, YPolygonCount polygonCount ) { PolyHandle hPoly; int k = *pointCountArray; if ( polygonCount > 1 ) { hPoly = CreatePolygon( pPoints, k ); for ( int i = 1; i < polygonCount; i++ ) { pPoints += k; k = pointCountArray[i]; AppendPolygonToPolygon( hPoly, pPoints, k ); } ::InvertPoly( hPoly ); ::DisposeHandle( (Handle)hPoly ); } else InvertPolygon( pPoints, k ); }
// Tao hinh sao voi // iIdentity > 2: nhan dang hinh sao, = 5: hinh sao 5 canh, = 6: hinh sao 6 canh ... // dAlpha: diem bat dau cua hinh sao co goc la dAlpha // (x,y) tam cua hinh sao // iRadius: ban kinh // iColor: mau cua duong bien hinh sao void _Polygon::CreateStar(int iIdentity, double dAlpha, int x, int y, int iRadius, int iColor) { CString _strError(""); if (iIdentity <= 2) { _strError = "_Polygon::CreateStar() --> iIdentity: Khong du canh de tao hinh sao"; iIdentity = 3; } CArray<POINT> arrTemp; POINT temp; double dAngle = PI / iIdentity; // goc quay double dBeta = (1 - 3.0 / (2*iIdentity)) * PI; double dGamma = PI / (2 * iIdentity); int iRadius2 = iRadius * (sin(dGamma) / sin(dBeta)); // Tao cac dinh cua hinh sao for (int i = 0; i < 2 * iIdentity; ++i) { if (i % 2 == 0) { temp.x = iRadius * cos(dAlpha + dAngle * i) + x; temp.y = iRadius * sin(dAlpha + dAngle * i) + y; } else { temp.x = iRadius2 * cos(dAlpha + dAngle * i) + x; temp.y = iRadius2 * sin(dAlpha + dAngle * i) + y; } arrTemp.Add(temp); } CreatePolygon(arrTemp, iColor); strError += _strError; // Gan loi }
QRectF boundingRect() const { return CreatePolygon().boundingRect(); }
TrianglePolygonItem() { this->setPolygon(CreatePolygon()); }
EntityWindow::EntityWindow(EditorWidget *editor, EntityType type, EntityWindowType windowType) { if(!editor->levelOpen) { close(); QMetaObject::invokeMethod(this, "close", Qt::QueuedConnection); return; } this->editor = editor; QGridLayout* gLayout = new QGridLayout(this); QLabel* nameLabel = new QLabel(QString("Name:")); QLabel* tileLabel = new QLabel(QString("Tile:")); QLabel* paramLabel = new QLabel(QString("Function:")); QLabel* tilesetLabel = new QLabel(QString("Tileset Name:")); QLabel* xLabel = new QLabel(QString("Texture Region X:")); QLabel* yLabel = new QLabel(QString("Texture Region Y:")); QLabel* twLabel = new QLabel(QString("Texture Region Width:")); QLabel* thLabel = new QLabel(QString("Texture Region Height:")); QLabel* wLabel = new QLabel(QString("Width:")); QLabel* hLabel = new QLabel(QString("Height:")); QLabel* oxLabel = new QLabel(QString("Origin X:")); QLabel* oyLabel = new QLabel(QString("Origin Y:")); nameBox = new QLineEdit(); tilesetBox = new QComboBox(); int textureNameCount = editor->data.configuration.textureNames.size(); for(int i = 0; i < textureNameCount; i++) { tilesetBox->addItem(QString(editor->data.configuration.textureNames[i].c_str())); } valueBox = Tools::NewSpinBox(0); paramBox = Tools::NewSpinBox(0); tXBox = Tools::NewSpinBox(INT_MIN); tYBox = Tools::NewSpinBox(INT_MIN); tWBox = Tools::NewSpinBox(INT_MIN); tHBox = Tools::NewSpinBox(INT_MIN); wBox = Tools::NewSpinBox(INT_MIN); hBox = Tools::NewSpinBox(INT_MIN); oxBox = Tools::NewSpinBox(INT_MIN); oyBox = Tools::NewSpinBox(INT_MIN); QPushButton* okButton = new QPushButton(QString("OK")); QPushButton* cancelButton = new QPushButton(QString("Cancel")); gLayout->addWidget(nameLabel,0,0); gLayout->addWidget(nameBox,0,1); connect(cancelButton,SIGNAL(clicked()),this,SLOT(FullClose())); int row = editor->window->manager->entityList->selectionModel()->currentIndex().row(); int bottomLayoutRow = 3; switch(type) { case Tiles: tilePreview = new TilePreviewWidget(editor); gLayout->addWidget(tileLabel,1,0); gLayout->addWidget(valueBox,1,1); gLayout->addWidget(paramLabel,2,0); gLayout->addWidget(paramBox,2,1); gLayout->addWidget(tilePreview,3,0); bottomLayoutRow = 4; connect(valueBox, SIGNAL(valueChanged(int)), tilePreview, SLOT(SetTileIndex(int))); break; case Props: gLayout->addWidget(tilesetLabel,0,2); gLayout->addWidget(tilesetBox,0,3); gLayout->addWidget(xLabel,1,0); gLayout->addWidget(tXBox,1,1); gLayout->addWidget(yLabel,1,2); gLayout->addWidget(tYBox,1,3); gLayout->addWidget(twLabel,2,0); gLayout->addWidget(tWBox,2,1); gLayout->addWidget(thLabel,2,2); gLayout->addWidget(tHBox,2,3); gLayout->addWidget(wLabel,3,0); gLayout->addWidget(wBox,3,1); gLayout->addWidget(hLabel,3,2); gLayout->addWidget(hBox,3,3); gLayout->addWidget(oxLabel,4,0); gLayout->addWidget(oxBox,4,1); gLayout->addWidget(oyLabel,4,2); gLayout->addWidget(oyBox,4,3); bottomLayoutRow = 5; break; case Sprites: bottomLayoutRow = 1; break; } switch(windowType) { case New: switch(type) { case Tiles: connect(okButton,SIGNAL(clicked()),this,SLOT(CreateTile())); break; case Props: connect(okButton,SIGNAL(clicked()),this,SLOT(CreateProp())); break; case Sprites: connect(okButton,SIGNAL(clicked()),this,SLOT(CreateSprite())); break; case Textures: connect(okButton,SIGNAL(clicked()),this,SLOT(CreateTexture())); break; case Polygons: connect(okButton,SIGNAL(clicked()),this,SLOT(CreatePolygon())); break; } break; case Edit: switch(type) { case Tiles: SetEditTileValues(row); connect(okButton,SIGNAL(clicked()),this,SLOT(EditTile())); break; case Props: SetEditPropValues(row); connect(okButton,SIGNAL(clicked()),this,SLOT(EditProp())); break; case Sprites: nameBox->setText(QString(editor->data.configuration.spriteDefinitions[row].name.c_str())); connect(okButton,SIGNAL(clicked()),this,SLOT(EditSprite())); break; case Textures: nameBox->setText(QString(editor->data.configuration.textureNames[row].c_str())); connect(okButton,SIGNAL(clicked()),this,SLOT(EditTexture())); break; case Polygons: nameBox->setText(QString(editor->data.configuration.polygonDefinitions[row].name.c_str())); connect(okButton,SIGNAL(clicked()),this,SLOT(EditPolygon())); break; } break; } gLayout->addWidget(okButton,bottomLayoutRow,0); gLayout->addWidget(cancelButton,bottomLayoutRow,1); }
//遍历链表,分割多边形 //教材上的的做法是用逆时针的多边形裁剪顺时针的多边形 //这里采用的做法是用顺时针的多边形裁剪顺时针的多边形(应为Convex不支持逆时针的多边形凸包) //所以做法和教材上有区别 //教材上的做法是用单向链表即可 //本实现必须采用双向链表 static int GenerateConvexPolygon(ClippingInterPoint* pointList, const int pointCount, Queue* queue, int* bufferForPolygonConverCount, int bufferCount) { bool isGenerate = NULL != queue ? true : false; int resultCount = 0;; if (true != isGenerate) { memset(bufferForPolygonConverCount, 0, sizeof(bufferForPolygonConverCount[0]) * bufferCount); } for (int i = 0; i < pointCount; ++i) { if (PROCESS_STATE_UNPROCESSED != pointList[i].state) { continue; } pointList[i].state = PROCESS_STATE_BEGIN_ADD; ClippingInterPoint* tempPoint = pointList[i].next2ClippedPolygon; ClippingInterPoint* lastPoint = &pointList[i]; MeshPolygon* polygon = isGenerate ? CreatePolygon(bufferForPolygonConverCount[resultCount]) : NULL; if (isGenerate) { AddPointToPolygon(polygon, lastPoint->point.x, lastPoint->point.z); } else { ++bufferForPolygonConverCount[resultCount]; } while (true) { if(PROCESS_STATE_BEGIN_ADD == tempPoint->state) { break; } if(isGenerate) { AddPointToPolygon(polygon, tempPoint->point.x, tempPoint->point.z); } else { ++bufferForPolygonConverCount[resultCount]; } if (INTER_POINT_TYPE_CLIPPED_PYLIGON_POINT == tempPoint->pointType) { tempPoint->state = PROCESS_STATE_ADDED_TO_POLYGON; lastPoint = tempPoint; tempPoint = tempPoint->next2ClippedPolygon; } else if(INTER_POINT_TYPE_CLIPPING_WINDOW_POINT == tempPoint->pointType) { lastPoint = tempPoint; tempPoint = tempPoint->prev2ClippingdWindow; } else if(INTER_POINT_TYPE_INTERSECTION_POINT_FOR_IN == tempPoint->pointType) { lastPoint = tempPoint; tempPoint = tempPoint->next2ClippedPolygon; } else if(INTER_POINT_TYPE_INTERSECTION_POINT_FOR_OUT == tempPoint->pointType) { lastPoint = tempPoint; tempPoint = tempPoint->prev2ClippingdWindow; } else if(INTER_POINT_TYPE_CLIPPING_WINDOW_POINT_AN_INSERCTION_POINT == tempPoint->pointType) { //特殊点 if(INTER_POINT_TYPE_CLIPPING_WINDOW_POINT == lastPoint->pointType || INTER_POINT_TYPE_INTERSECTION_POINT_FOR_OUT == lastPoint->pointType || INTER_POINT_TYPE_CLIPPING_WINDOW_POINT_AN_INSERCTION_POINT == lastPoint->pointType) { lastPoint = tempPoint; tempPoint = tempPoint->next2ClippedPolygon; } else if(INTER_POINT_TYPE_CLIPPED_PYLIGON_POINT == lastPoint->pointType) { lastPoint = tempPoint; tempPoint = tempPoint->prev2ClippingdWindow; } else { assert(0); } } else { assert(0); } } if (isGenerate) { PushDataToQueue(queue, polygon); } ++resultCount; } return resultCount; }
// Ve cac doan thang bang thuat toan Bresenham _Polygon::_Polygon(const CArray<POINT>& arrPoint, int iColor) { CreatePolygon(arrPoint, iColor); }