// $LAN=C++$ // ============================================================================= // 程式原始碼合併檔 (Merged source code) - Voronoi Diagram // Author: 李俊諺 Jun-Yan Li // Student ID: M143040074 // Language: C++ (C++17) // Build environment: Qt 6.10.1 (Qt Widgets), MinGW-w64 GCC 13.1.0 (64-bit), qmake // Project file: VoronoiDiagram.pro // OS: Windows 11 // Files: // 1. include/canvaswidget.h // 2. include/mainwindow.h // 3. include/VoronoiDiagram.h // 4. src/canvaswidget.cpp // 5. src/main.cpp // 6. src/mainwindow.cpp // 7. src/VoronoiDiagram.cpp // ============================================================================= // ============================================================================= // File 1/7: include/canvaswidget.h // ============================================================================= // $LAN=C++$ // File: canvaswidget.h // Author: 李俊諺 Jun-Yan Li // Student ID: M143040074 // Date: 2025/12/5 #ifndef CANVASWIDGET_H #define CANVASWIDGET_H #include #include #include #include #include #include const int WIDTH = 610, HEIGHT = 610; class CanvasWidget : public QWidget { Q_OBJECT // Qt macros for Signals and Slots public: CanvasWidget(QWidget *parent = nullptr); // The canvas size QSize minimumSizeHint() const override { return QSize(WIDTH, HEIGHT); } QVector getPoints() { return points; } QVector getEdges(); signals: void pointsModified(const QVector& points); void mouseMoved(QPointF pos); void stepChanged(const QString& description); // Shown step changed, empty when no step is shown public slots: void clearCanvas(); void setPoints(const QVector& newPoints); void setDiagram(const QVector& newPoints, const QVector& newEdges); void stepByStep(); void runCalculation(); void addPoint(const QPointF& p); protected: void paintEvent(QPaintEvent *event) override; void mousePressEvent(QMouseEvent *event) override; void mouseDoubleClickEvent(QMouseEvent *event) override; void mouseMoveEvent(QMouseEvent *event) override; private: QVector points; QVector edges; int currentStepIndex; void setStepIndex(int index); void updateState(); }; #endif // CANVASWIDGET_H // ============================================================================= // File 2/7: include/mainwindow.h // ============================================================================= // $LAN=C++$ // File: mainwindow.h // Author: 李俊諺 Jun-Yan Li // Student ID: M143040074 // Date: 2025/12/5 #ifndef MAINWINDOW_H #define MAINWINDOW_H #include "canvaswidget.h" #include #include #include #include #include // Forward declarations QT_BEGIN_NAMESPACE class QAction; class QFile; QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private slots: /* 1. Input text file with comment 2. Input text file without comment 3. Ouput text file */ void readFile(); void onReadInput(); void onReadOutput(); void onNextTestCase(); void onClear(); void saveFile(); void updateMousePos(QPointF pos); void onAddPointClicked(); void updatePointList(const QVector& points); private: void createActions(); void createMenus(); void createToolBar(); void readInputFormat(QFile *file); void readOutputFormat(QFile *file); void resetTestCases(); // End the test cases of the last input file CanvasWidget *canvas; QListWidget *pointListWidget; QSpinBox *inputX; QSpinBox *inputY; QPushButton *btnAddPoint; QAction *openAct; QAction *readInputAct; QAction *readOutputAct; QAction *nextCaseAct; QAction *saveAct; QAction *stepAct; QAction *runAct; QAction *clearAct; QLabel *coordLabel; QLabel *caseLabel; QLabel *stepLabel; // Test cases of the last input file, shown one at a time with "Next test case" QVector> testCases; int currentCase = -1; bool zeroPending = false; // The file ends with 0, show the stop message after the last test case }; #endif // MAINWINDOW_H // ============================================================================= // File 3/7: include/VoronoiDiagram.h // ============================================================================= // $LAN=C++$ // File: VoronoiDiagram.h // Author: 李俊諺 Jun-Yan Li // Student ID: M143040074 // Date: 2025/12/5 #ifndef VORONOIDIAGRAM_H #define VORONOIDIAGRAM_H #include #include #include #include #include // Points are referred to by their index in the sorted points struct VoronoiResult { QVector edges; // Voronoi edges QVector> sites; // The two points that generated edges[i] // Edges of a region in counterclockwise order: next[i][k] (prev[i][k]) is the next (previous) edge // around the region of sites[i].first (k = 0) or sites[i].second (k = 1) QVector> next, prev; int firstSite = 0; // The points are firstSite, firstSite + 1, ... QVector anyEdge; // anyEdge[s - firstSite]: an edge of the region of point s, -1 if it has none QVector convexHull; // Sort in counterclockwise order }; struct VoronoiStep { QVector currentEdges; // All edges at the current step (including left, right, and hyperplanes) QVector currentHull; // The merged convext hull, the left one, the right one QVector edgeTypes; // Indicates the edge type (0: Normal, 1: Left voronoi, 2: Right voronoi, 3: Hyperplane) QString description; // For debugging int split; // 0: Merged convex hull, not 0: lefthull = currentHull.mid(0, split), righthull = currentHull.mid(split) }; class VoronoiDiagram { public: // Compare lexicographically static bool comparePoints(const QPointF& a, const QPointF& b); static bool compareEdges(const QLineF& e1, const QLineF& e2); // Clip a line segment to a rectangle, return false if it lies outside (Liang-Barsky) static bool clipLine(const QLineF& line, const QRectF& rect, QLineF& clipped); static QVector sortAndUnique(const QVector& points); static QVector getVoronoiEdges(QVector& points); // For step by step static QVector steps; static void clearSteps() { steps.clear(); } static void addStep(const QVector& edges, const QVector& types, const QVector& hull, QString desc = "", int split = 0); private: static QVector P; // The sorted points of the current diagram static VoronoiResult voronoi3(int first); // End point of a ray from origin in direction dir, far enough away to stand for infinity static QPointF rayEnd(const QPointF& origin, const QPointF& dir); static QLineF getPerpBisector(const QPointF& p1, const QPointF& p2); static QPointF calculateCircumcenter(const QPointF& p1, const QPointF& p2, const QPointF& p3); static qreal crossProduct(const QPointF& o, const QPointF& a, const QPointF& b); static VoronoiResult divideAndConquer(int first, int last); // Points first, ..., last - 1 static VoronoiResult merge(const VoronoiResult& left, const VoronoiResult& right); static QVector mergeConvexHull(const QVector& left, const QVector& right, const QPair& upperTan, const QPair& lowerTan); static QVector hullPoints(const QVector& hull); static QPair getUpperTangent(const QVector& leftHull, const QVector& rightHull); static QPair getLowerTangent(const QVector& leftHull, const QVector& rightHull); static bool areAllCollinear(int first, int last); static VoronoiResult solveCollinear(int first, int last); // Base cases: the edges of each region, in counterclockwise order static void linkRegions(VoronoiResult& res, int first, int last); }; #endif // VORONOIDIAGRAM_H // ============================================================================= // File 4/7: src/canvaswidget.cpp // ============================================================================= // $LAN=C++$ // File: canvaswidget.cpp // Author: 李俊諺 Jun-Yan Li // Student ID: M143040074 // Date: 2025/12/5 #include "../include/canvaswidget.h" #include "../include/VoronoiDiagram.h" #include #include CanvasWidget::CanvasWidget(QWidget *parent) : QWidget(parent) { setAutoFillBackground(true); QPalette pal = palette(); pal.setColor(QPalette::Window, Qt::white); setPalette(pal); currentStepIndex = -1; setMouseTracking(true); // Rays are clipped to the canvas border when saving, so the canvas keeps its size setFixedSize(WIDTH, HEIGHT); } QVector CanvasWidget::getEdges() { return VoronoiDiagram::getVoronoiEdges(points); } void CanvasWidget::setStepIndex(int index) { currentStepIndex = index; // The status bar shows the step, so the text does not cover the diagram bool valid = index >= 0 && index < VoronoiDiagram::steps.size(); emit stepChanged(valid ? VoronoiDiagram::steps[index].description : QString()); } void CanvasWidget::updateState() { // Points changed, the old steps are no longer valid VoronoiDiagram::clearSteps(); setStepIndex(-1); repaint(); emit pointsModified(points); } void CanvasWidget::clearCanvas() { points.clear(); edges.clear(); updateState(); } // A repeated point is the same site, keep only its first occurrence (in input order) static QVector removeRepeated(const QVector& points) { QVector result; for (const QPointF& p : points) if (!result.contains(p)) result.append(p); return result; } void CanvasWidget::setPoints(const QVector& newPoints) { points = removeRepeated(newPoints); edges.clear(); updateState(); } void CanvasWidget::setDiagram(const QVector& newPoints, const QVector& newEdges) { points = removeRepeated(newPoints); edges = newEdges; updateState(); } void CanvasWidget::stepByStep() { if (VoronoiDiagram::sortAndUnique(points).size() <= 1) return; // The first step if (VoronoiDiagram::steps.isEmpty() || currentStepIndex >= VoronoiDiagram::steps.size() - 1) { edges = VoronoiDiagram::getVoronoiEdges(points); currentStepIndex = -1; } setStepIndex(currentStepIndex + 1); repaint(); } void CanvasWidget::runCalculation() { // No Voronoi edges for 0 or 1 distinct point if (VoronoiDiagram::sortAndUnique(points).size() <= 1) { edges.clear(); setStepIndex(-1); repaint(); return; } edges = VoronoiDiagram::getVoronoiEdges(points); setStepIndex(VoronoiDiagram::steps.size() - 1); repaint(); } void CanvasWidget::addPoint(const QPointF& p) { if (points.contains(p)) return; // Already on the canvas, the diagram does not change points.append(p); edges.clear(); updateState(); } void CanvasWidget::mousePressEvent(QMouseEvent *event) { // Positions are fractional under display scaling, round to integer coordinates if (event->button() == Qt::LeftButton) addPoint(event->position().toPoint()); } void CanvasWidget::mouseDoubleClickEvent(QMouseEvent *event) { // The first click already added the point (the default implementation would add it again) Q_UNUSED(event); } void CanvasWidget::mouseMoveEvent(QMouseEvent *event) { emit mouseMoved(event->position()); QWidget::mouseMoveEvent(event); } void CanvasWidget::paintEvent(QPaintEvent *event) { Q_UNUSED(event); QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); // Draw Points QPen pointPen(Qt::black); pointPen.setWidth(4); pointPen.setCapStyle(Qt::RoundCap); painter.setPen(pointPen); painter.setBrush(Qt::black); painter.drawPoints(points); // Rays end very far away, clip lines to the visible area before drawing const QRectF area = QRectF(rect()).adjusted(-10, -10, 10, 10); auto drawClippedLine = [&](const QLineF& line) { QLineF clipped; if (VoronoiDiagram::clipLine(line, area, clipped)) painter.drawLine(clipped); }; // Draw Lines if (currentStepIndex >= 0 && currentStepIndex < VoronoiDiagram::steps.size()) { // Step-by-Step const VoronoiStep& step = VoronoiDiagram::steps[currentStepIndex]; QPen linePen; linePen.setWidth(2); for (int i = 0; i < step.currentEdges.size(); ++i) { QLineF line = step.currentEdges[i]; int type = (i < step.edgeTypes.size()) ? step.edgeTypes[i] : 0; switch (type) { case 1: // Left voronoi linePen.setColor(Qt::blue); break; case 2: // Right voronoi linePen.setColor(Qt::green); break; case 3: // Hyperplane linePen.setColor(Qt::magenta); break; default: // Result/Normal linePen.setColor(Qt::red); break; } painter.setPen(linePen); drawClippedLine(line); } // Convex hull QPen hullPen(Qt::cyan); hullPen.setWidth(2); painter.setPen(hullPen); painter.setBrush(Qt::NoBrush); auto drawHull = [&](const QVector& hullPoints) { if (hullPoints.size() >= 2) painter.drawPolygon(QPolygonF(hullPoints)); }; if (step.split == 0) drawHull(step.currentHull); else { drawHull(step.currentHull.mid(0, step.split)); // Left drawHull(step.currentHull.mid(step.split)); // Right } } else { // Run painter.setPen(QPen(Qt::red, 2)); painter.setBrush(Qt::NoBrush); for (const QLineF& line : std::as_const(edges)) drawClippedLine(line); } } // ============================================================================= // File 5/7: src/main.cpp // ============================================================================= // $LAN=C++$ // File: main.cpp // Author: 李俊諺 Jun-Yan Li // Student ID: M143040074 // Date: 2025/12/5 #include "../include/mainwindow.h" #include int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); } // ============================================================================= // File 6/7: src/mainwindow.cpp // ============================================================================= // $LAN=C++$ // File: mainwindow.cpp // Author: 李俊諺 Jun-Yan Li // Student ID: M143040074 // Date: 2025/12/5 #include "../include/mainwindow.h" #include "../include/VoronoiDiagram.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include // Draws a 64x64 icon in fixed colors, so it stays visible in both light and dark themes static QIcon paintIcon(const std::function &draw) { QPixmap pixmap(64, 64); pixmap.fill(Qt::transparent); QPainter painter(&pixmap); painter.setRenderHint(QPainter::Antialiasing); painter.setPen(Qt::NoPen); draw(painter); return QIcon(pixmap); } // A sheet of paper, shared by the read input / read output icons static void drawPage(QPainter &painter) { painter.setPen(QPen(QColor(120, 120, 120), 3)); painter.setBrush(Qt::white); painter.drawRoundedRect(QRectF(12, 4, 40, 56), 4, 4); } // Show a status bar label only while it has text static void setStatusText(QLabel *label, const QString& text) { label->setText(text); label->setVisible(!text.isEmpty()); } MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { // 1. Setup Central Widget QWidget *centralWidget = new QWidget(this); setCentralWidget(centralWidget); // Main Layout (Horizontal) QHBoxLayout *mainLayout = new QHBoxLayout(centralWidget); // 2. Left Side: Canvas canvas = new CanvasWidget(this); mainLayout->addWidget(canvas, 1); // 3. Right Side: Container for List and Input Area QWidget *rightPanel = new QWidget(this); rightPanel->setMaximumWidth(200); QVBoxLayout *rightLayout = new QVBoxLayout(rightPanel); rightLayout->setContentsMargins(0, 0, 0, 0); // 3-1. Point List pointListWidget = new QListWidget(this); rightLayout->addWidget(pointListWidget, 1); // 3-2. Input Area (Group Box) QGroupBox *inputGroup = new QGroupBox(tr("Add Point"), this); QVBoxLayout *inputGroupLayout = new QVBoxLayout(inputGroup); // Input X Coordinate QHBoxLayout *xLayout = new QHBoxLayout(); xLayout->addWidget(new QLabel("X:")); inputX = new QSpinBox(this); inputX->setRange(0, WIDTH); // Points must lie on the canvas xLayout->addWidget(inputX); inputGroupLayout->addLayout(xLayout); // Input Y Coordinate QHBoxLayout *yLayout = new QHBoxLayout(); yLayout->addWidget(new QLabel("Y:")); inputY = new QSpinBox(this); inputY->setRange(0, HEIGHT); yLayout->addWidget(inputY); inputGroupLayout->addLayout(yLayout); // Add Button btnAddPoint = new QPushButton(tr("Add"), this); inputGroupLayout->addWidget(btnAddPoint); // Add the group box to the right panel layout rightLayout->addWidget(inputGroup, 0); // Add the right panel to the main layout mainLayout->addWidget(rightPanel, 0); // 4. Signal Connections // Connect canvas modification signal to update the point list UI connect(canvas, &CanvasWidget::pointsModified, this, &MainWindow::updatePointList); // Connect "Add" button click signal connect(btnAddPoint, &QPushButton::clicked, this, &MainWindow::onAddPointClicked); // 5. Status Bar Setup coordLabel = new QLabel("Pos: (0, 0)", this); coordLabel->setMinimumWidth(150); // Prevent text jitter by setting min width stepLabel = new QLabel(this); // Which step of the merge is shown, kept off the canvas stepLabel->setContentsMargins(0, 0, 8, 0); // Space before the separator caseLabel = new QLabel(this); // Which test case of the input file is shown caseLabel->setContentsMargins(0, 0, 8, 0); statusBar()->addPermanentWidget(stepLabel); statusBar()->addPermanentWidget(caseLabel); stepLabel->hide(); // Empty labels stay hidden, so the status bar shows no empty separators caseLabel->hide(); statusBar()->addPermanentWidget(coordLabel); // Connect mouse move signal from Canvas to update coordinates in StatusBar connect(canvas, &CanvasWidget::mouseMoved, this, &MainWindow::updateMousePos); connect(canvas, &CanvasWidget::stepChanged, this, [this](const QString& description) { setStatusText(stepLabel, description); }); // 6. Initialize Actions and Toolbars createActions(); createMenus(); createToolBar(); setWindowTitle(tr("Voronoi Diagram")); } MainWindow::~MainWindow() {} void MainWindow::updateMousePos(QPointF pos) { // Rounded the same way as a clicked point QString text = QString("Pos: (%1, %2)").arg(qRound(pos.x())).arg(qRound(pos.y())); coordLabel->setText(text); } void MainWindow::onAddPointClicked() { double x = inputX->value(), y = inputY->value(); canvas->addPoint(QPointF(x, y)); } void MainWindow::updatePointList(const QVector& points) { pointListWidget->clear(); for (int i = 0; i < points.size(); i++) { QString str = QString("(%1, %2)").arg(points[i].x()).arg(points[i].y()); pointListWidget->addItem(str); } } void MainWindow::createActions() { // Open openAct = new QAction(tr("&Open"), this); openAct->setIcon(style()->standardIcon(QStyle::SP_DialogOpenButton)); openAct->setStatusTip(tr("Open")); connect(openAct, &QAction::triggered, this, &MainWindow::readFile); // Read Input Text readInputAct = new QAction(tr("Read input text"), this); readInputAct->setIcon(paintIcon([](QPainter &p) { drawPage(p); p.setPen(Qt::NoPen); p.setBrush(QColor(30, 136, 229)); p.drawEllipse(QPointF(24, 20), 5, 5); p.drawEllipse(QPointF(40, 28), 5, 5); p.drawEllipse(QPointF(27, 45), 5, 5); })); readInputAct->setStatusTip(tr("Read input text")); connect(readInputAct, &QAction::triggered, this, &MainWindow::onReadInput); // Read Output Text readOutputAct = new QAction(tr("Read output text"), this); readOutputAct->setIcon(paintIcon([](QPainter &p) { drawPage(p); p.setPen(QPen(QColor(229, 57, 53), 4, Qt::SolidLine, Qt::RoundCap)); p.drawLine(QPointF(32, 34), QPointF(18, 16)); p.drawLine(QPointF(32, 34), QPointF(46, 18)); p.drawLine(QPointF(32, 34), QPointF(32, 52)); })); readOutputAct->setStatusTip(tr("Read output text")); connect(readOutputAct, &QAction::triggered, this, &MainWindow::onReadOutput); // Next test case of the input file nextCaseAct = new QAction(tr("Next test case"), this); nextCaseAct->setIcon(paintIcon([](QPainter &p) { p.setBrush(QColor(251, 140, 0)); // Orange, so it is not mistaken for the blue Step icon p.drawPolygon(QPolygonF({QPointF(6, 12), QPointF(30, 32), QPointF(6, 52)})); p.drawPolygon(QPolygonF({QPointF(32, 12), QPointF(56, 32), QPointF(32, 52)})); })); nextCaseAct->setStatusTip(tr("Next test case")); nextCaseAct->setEnabled(false); // Enabled after reading an input file with more test cases connect(nextCaseAct, &QAction::triggered, this, &MainWindow::onNextTestCase); // Save saveAct = new QAction(tr("&Save"), this); saveAct->setIcon(style()->standardIcon(QStyle::SP_DialogSaveButton)); saveAct->setStatusTip(tr("Save")); connect(saveAct, &QAction::triggered, this, &MainWindow::saveFile); // Step by step stepAct = new QAction(tr("&Step"), this); stepAct->setIcon(paintIcon([](QPainter &p) { p.setBrush(QColor(30, 136, 229)); p.drawPolygon(QPolygonF({QPointF(8, 10), QPointF(40, 32), QPointF(8, 54)})); p.drawRoundedRect(QRectF(44, 10, 10, 44), 2, 2); })); stepAct->setStatusTip(tr("Step by step")); connect(stepAct, &QAction::triggered, canvas, &CanvasWidget::stepByStep); // Run runAct = new QAction(tr("&Run"), this); runAct->setIcon(paintIcon([](QPainter &p) { p.setBrush(QColor(67, 160, 71)); p.drawPolygon(QPolygonF({QPointF(14, 8), QPointF(56, 32), QPointF(14, 56)})); })); runAct->setStatusTip(tr("Run")); connect(runAct, &QAction::triggered, canvas, &CanvasWidget::runCalculation); // Clear clearAct = new QAction(tr("&Clear"), this); clearAct->setIcon(style()->standardIcon(QStyle::SP_DialogResetButton)); clearAct->setStatusTip(tr("Clear")); connect(clearAct, &QAction::triggered, this, &MainWindow::onClear); } void MainWindow::createMenus() { QMenu *fileMenu = menuBar()->addMenu(tr("&File")); fileMenu->addAction(readInputAct); fileMenu->addAction(readOutputAct); fileMenu->addSeparator(); fileMenu->addAction(saveAct); QMenu *runMenu = menuBar()->addMenu(tr("&Execute")); runMenu->addAction(stepAct); runMenu->addAction(runAct); runMenu->addSeparator(); runMenu->addAction(clearAct); } void MainWindow::createToolBar() { QToolBar *toolBar = addToolBar(tr("Main Toolbar")); toolBar->setToolButtonStyle(Qt::ToolButtonIconOnly); toolBar->addAction(openAct); toolBar->addAction(nextCaseAct); toolBar->addAction(saveAct); toolBar->addSeparator(); toolBar->addAction(stepAct); toolBar->addAction(runAct); toolBar->addAction(clearAct); } void MainWindow::readFile() { QString fileName = QFileDialog::getOpenFileName(this, tr("Read File"), "", tr("Text File (*.txt)")); if (fileName.isEmpty()) return; QFile checkFile(fileName); if (!checkFile.open(QIODevice::ReadOnly | QIODevice::Text)) { QMessageBox::warning(this, tr("Error"), tr("Cannot open file for reading")); return; } // The first non-empty line of an output file starts with "P" QTextStream checkIn(&checkFile); QString firstLine; while (firstLine.isEmpty() && !checkIn.atEnd()) firstLine = checkIn.readLine().trimmed(); checkFile.close(); QFile file(fileName); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { QMessageBox::warning(this, tr("Error"), tr("Cannot open file for reading")); return; } if (firstLine.startsWith('P')) readOutputFormat(&file); else readInputFormat(&file); } void MainWindow::onReadInput() { QString fileName = QFileDialog::getOpenFileName(this, tr("Read Input File"), "", tr("Text File (*.txt)")); if (fileName.isEmpty()) return; QFile file(fileName); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { QMessageBox::warning(this, tr("Error"), tr("Cannot open file for reading")); return; } readInputFormat(&file); file.close(); } void MainWindow::onReadOutput() { QString fileName = QFileDialog::getOpenFileName(this, tr("Read Output File"), "", tr("Text File (*.txt)")); if (fileName.isEmpty()) return; QFile file(fileName); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { QMessageBox::warning(this, tr("Error"), tr("Cannot open file for reading")); return; } readOutputFormat(&file); file.close(); } void MainWindow::readInputFormat(QFile *file) { QTextStream textStream(file); int n = -1; bool ok = false; // Read all test cases first, then show them one at a time resetTestCases(); while (!textStream.atEnd()) { // 1. Loop until we find the next number "n" ok = false; while (!textStream.atEnd()) { // Ignore comments (also at the end of a line) and empty lines QString line = textStream.readLine().section('#', 0, 0).trimmed(); if (line.isEmpty()) continue; // Non-comment line, try to convert it to an integer n = line.toInt(&ok); if (ok) break; } // No more test cases (file ends without "0") if (!ok) break; // 2. Number of points is zero, file test stopped (shown after the last test case) if (n == 0) { zeroPending = true; break; } // 3. Read n points QVector points; while (points.size() < n && !textStream.atEnd()) { // Coordinates may be separated by spaces or tabs QString dataLine = textStream.readLine().section('#', 0, 0).trimmed(); QStringList parts = dataLine.split(QRegularExpression("\\s+"), Qt::SkipEmptyParts); if (parts.size() >= 2) { bool okX, okY; double x = parts[0].toDouble(&okX), y = parts[1].toDouble(&okY); if (okX && okY) points.append(QPointF(x, y)); } } testCases.append(points); } // 4. Show the first test case onNextTestCase(); } void MainWindow::onNextTestCase() { if (currentCase + 1 < testCases.size()) { ++currentCase; canvas->setPoints(testCases[currentCase]); canvas->runCalculation(); setStatusText(caseLabel, tr("Test case %1 / %2").arg(currentCase + 1).arg(testCases.size())); } else if (zeroPending) { zeroPending = false; QMessageBox::information(this, tr("Test Finished"), tr("讀入點數為零,檔案測試停止")); } // Stays enabled after the last test case if the stop message is still to be shown nextCaseAct->setEnabled(currentCase + 1 < testCases.size() || zeroPending); } void MainWindow::resetTestCases() { testCases.clear(); currentCase = -1; zeroPending = false; setStatusText(caseLabel, QString()); nextCaseAct->setEnabled(false); } void MainWindow::onClear() { resetTestCases(); canvas->clearCanvas(); } void MainWindow::readOutputFormat(QFile *file) { QVector points; QVector edges; QTextStream textStream(file); resetTestCases(); // The canvas no longer shows a test case of the input file while (!textStream.atEnd()) { QString line = textStream.readLine().trimmed(); QStringList parts = line.split(QRegularExpression("\\s+"), Qt::SkipEmptyParts); if (parts.isEmpty()) continue; else if (parts[0] == "P" && parts.size() >= 3) points.append(QPointF(parts[1].toDouble(), parts[2].toDouble())); else if (parts[0] == "E" && parts.size() >= 5) { QPointF p1(parts[1].toDouble(), parts[2].toDouble()); QPointF p2(parts[3].toDouble(), parts[4].toDouble()); edges.append(QLineF(p1, p2)); } } canvas->setDiagram(points, edges); // Pass both points and edges to the canvas for (const QPointF& p : std::as_const(points)) qDebug() << "P:" << p.x() << p.y(); for (const QLineF& e : std::as_const(edges)) { QPointF p1 = e.p1(), p2 = e.p2(); qDebug() << "E:" << p1.x() << p1.y() << p2.x() << p2.y(); } } void MainWindow::saveFile() { QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), "", tr("Text File (*.txt)")); if (fileName.isEmpty()) return; QFile file(fileName); if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { QMessageBox::warning(this, tr("Error"), tr("Cannot open file for writing")); return; } // Sort points by lexical order, a repeated point is written once QVector points = VoronoiDiagram::sortAndUnique(canvas->getPoints()); QVector edges = canvas->getEdges(); QTextStream out(&file); for (const QPointF& p : std::as_const(points)) out << "P " << p.x() << " " << p.y() << "\n"; // Clip segments to the canvas border, coordinates are truncated to integers (as in the spec example). // A clipped end point is computed from a far away ray end and is off by about 1e-6, while border // crossings of integer points are multiples of 1/1220, so it gets a larger tolerance const QRectF outputRect(0, 0, WIDTH, HEIGHT); auto toInt = [](double v, double tolerance) { return (int)std::floor(v + tolerance); }; QVector outEdges; for (const QLineF& e : std::as_const(edges)) { QLineF clipped; if (!VoronoiDiagram::clipLine(e, outputRect, clipped)) continue; double tol1 = (clipped.p1() == e.p1()) ? 1e-6 : 1e-4; double tol2 = (clipped.p2() == e.p2()) ? 1e-6 : 1e-4; QPointF p1(toInt(clipped.x1(), tol1), toInt(clipped.y1(), tol1)), p2(toInt(clipped.x2(), tol2), toInt(clipped.y2(), tol2)); if (p1 == p2) continue; // Only touches the canvas // Sort segments by lexical order if (!VoronoiDiagram::comparePoints(p1, p2)) std::swap(p1, p2); outEdges.append(QLineF(p1, p2)); } std::sort(outEdges.begin(), outEdges.end(), VoronoiDiagram::compareEdges); for (const QLineF& e : std::as_const(outEdges)) { QPointF p1 = e.p1(), p2 = e.p2(); out << "E " << (int)p1.x() << " " << (int)p1.y() << " " << (int)p2.x() << " " << (int)p2.y() << "\n"; } } // ============================================================================= // File 7/7: src/VoronoiDiagram.cpp // ============================================================================= // $LAN=C++$ // File: VoronoiDiagram.cpp // Author: 李俊諺 Jun-Yan Li // Student ID: M143040074 // Date: 2025/12/5 #include "../include/VoronoiDiagram.h" #include #include #include #include #include const double EPSILON = 1e-6; const double FAR = 1e10; // Length of rays to infinity, much larger than any Voronoi vertex const double ANGLE_EPSILON = 1e-9; // For sines and cosines of angles between unit vectors bool VoronoiDiagram::comparePoints(const QPointF& a, const QPointF& b) { if (abs(a.x() - b.x()) > EPSILON) return a.x() < b.x(); return a.y() < b.y(); } bool VoronoiDiagram::compareEdges(const QLineF& e1, const QLineF& e2) { QPointF a1 = e1.p1(), b1 = e1.p2(); QPointF a2 = e2.p1(), b2 = e2.p2(); if (abs(a1.x() - a2.x()) > EPSILON) return a1.x() < a2.x(); if (abs(a1.y() - a2.y()) > EPSILON) return a1.y() < a2.y(); if (abs(b1.x() - b2.x()) > EPSILON) return b1.x() < b2.x(); return b1.y() < b2.y(); } QPointF VoronoiDiagram::rayEnd(const QPointF& origin, const QPointF& dir) { double len = std::sqrt(QPointF::dotProduct(dir, dir)); double reach = FAR + std::abs(origin.x()) + std::abs(origin.y()); return origin + dir / len * reach; } bool VoronoiDiagram::clipLine(const QLineF& line, const QRectF& rect, QLineF& clipped) { double x0 = line.x1(), y0 = line.y1(), dx = line.dx(), dy = line.dy(); double t0 = 0, t1 = 1; // Each boundary: p * t <= q const double p[4] = {-dx, dx, -dy, dy}; const double q[4] = {x0 - rect.left(), rect.right() - x0, y0 - rect.top(), rect.bottom() - y0}; int side0 = -1, side1 = -1; // Which boundary the clipped end points lie on for (int i = 0; i < 4; ++i) { if (p[i] == 0) { if (q[i] < 0) return false; // Parallel and outside continue; } double t = q[i] / p[i]; if (p[i] < 0 && t > t0) { t0 = t; side0 = i; } else if (p[i] > 0 && t < t1) { t1 = t; side1 = i; } } if (t0 > t1) return false; // Put clipped end points exactly on the boundary auto pointAt = [&](double t, int side) { QPointF pt(x0 + t * dx, y0 + t * dy); if (side == 0) pt.setX(rect.left()); else if (side == 1) pt.setX(rect.right()); else if (side == 2) pt.setY(rect.top()); else if (side == 3) pt.setY(rect.bottom()); return pt; }; clipped = QLineF(pointAt(t0, side0), pointAt(t1, side1)); return true; } QVector VoronoiDiagram::sortAndUnique(const QVector& points) { QVector newPoints = points; std::sort(newPoints.begin(), newPoints.end(), comparePoints); auto last = std::unique(newPoints.begin(), newPoints.end()); newPoints.erase(last, newPoints.end()); return newPoints; } // --- Initialize static variables --- QVector VoronoiDiagram::steps; QVector VoronoiDiagram::P; void VoronoiDiagram::addStep(const QVector& edges, const QVector& types, const QVector& hull, QString desc, int split) { VoronoiStep step; step.currentEdges = edges; step.edgeTypes = types; step.currentHull = hull; step.description = desc; step.split = split; steps.append(step); } // The point on the other side of edge e, seen from the region of point s static int otherSite(const QVector>& sites, int e, int s) { return sites[e].first == s ? sites[e].second : sites[e].first; } void VoronoiDiagram::linkRegions(VoronoiResult& res, int first, int last) { int n = last - first; res.firstSite = first; res.anyEdge = QVector(n, -1); res.next = res.prev = QVector>(res.edges.size(), {-1, -1}); QVector> around(n); for (int i = 0; i < res.edges.size(); ++i) { around[res.sites[i].first - first].append(i); around[res.sites[i].second - first].append(i); } for (int s = 0; s < n; ++s) { QVector& list = around[s]; if (list.isEmpty()) continue; int site = first + s; // The edges of a region are in the order of the directions to the other points. // Counterclockwise on the screen (+y points down) is decreasing atan2 auto angle = [&](int e) { QPointF d = P[otherSite(res.sites, e, site)] - P[site]; return std::atan2(d.y(), d.x()); }; std::sort(list.begin(), list.end(), [&](int a, int b) { return angle(a) > angle(b); }); for (int k = 0; k < list.size(); ++k) { int e = list[k], side = (res.sites[e].first == site) ? 0 : 1; res.next[e][side] = list[(k + 1) % list.size()]; res.prev[e][side] = list[(k - 1 + list.size()) % list.size()]; } res.anyEdge[s] = list[0]; } } VoronoiResult VoronoiDiagram::voronoi3(int first) { VoronoiResult res; const QVector points = P.mid(first, 3); QPointF ccenter = calculateCircumcenter(points[0], points[1], points[2]); // Collinear if (std::abs(crossProduct(points[0], points[1], points[2])) <= EPSILON) { res.edges.append(getPerpBisector(points[0], points[1])); res.sites.append(qMakePair(first, first + 1)); res.edges.append(getPerpBisector(points[1], points[2])); res.sites.append(qMakePair(first + 1, first + 2)); } else { // Draw three rays starting from the circumcenter // The direction of vec is right, the direction of normal is up // 如果法向量方向與中點往第三點方向相同,射線方向要改為反方向 QPointF mid01 = (points[0] + points[1]) / 2; QPointF vec01 = points[1] - points[0]; QPointF normal01(vec01.y(), -vec01.x()); if (QPointF::dotProduct(normal01, points[2] - mid01) > 0) normal01 = -normal01; res.edges.append(QLineF(ccenter, rayEnd(ccenter, normal01))); res.sites.append(qMakePair(first, first + 1)); QPointF mid12 = (points[1] + points[2]) / 2; QPointF vec12 = points[2] - points[1]; QPointF normal12(vec12.y(), -vec12.x()); if (QPointF::dotProduct(normal12, points[0] - mid12) > 0) normal12 = -normal12; res.edges.append(QLineF(ccenter, rayEnd(ccenter, normal12))); res.sites.append(qMakePair(first + 1, first + 2)); QPointF mid02 = (points[0] + points[2]) / 2; QPointF vec02 = points[2] - points[0]; QPointF normal02(vec02.y(), -vec02.x()); if (QPointF::dotProduct(normal02, points[1] - mid02) > 0) normal02 = -normal02; res.edges.append(QLineF(ccenter, rayEnd(ccenter, normal02))); res.sites.append(qMakePair(first, first + 2)); } // Determine whether the order is clockwise or counter-clockwise, or which point is in the middle qreal cp = crossProduct(points[0], points[1], points[2]); res.convexHull.append(first); if (cp < 0) { // p2 is to the left of p0->p1, p0 -> p1 -> p2 res.convexHull.append(first + 1); res.convexHull.append(first + 2); } else if (cp > 0) { // p2 is to the right of p0->p1, p0 -> p2 -> p1 res.convexHull.append(first + 2); res.convexHull.append(first + 1); } else res.convexHull.append(first + 2); // Collinear linkRegions(res, first, first + 3); return res; } QLineF VoronoiDiagram::getPerpBisector(const QPointF& p1, const QPointF& p2) { QPointF mid = (p1 + p2) / 2; QPointF vec = p2 - p1; QPointF normal(vec.y(), -vec.x()); // Allow extending beyond the window return QLineF(rayEnd(mid, normal), rayEnd(mid, -normal)); } QPointF VoronoiDiagram::calculateCircumcenter(const QPointF& p1, const QPointF& p2, const QPointF& p3) { /* (x, y) is a point on the perpendicular bisector. (x - p1.x)^2 + (y - p1.y)^2 = (x - p2.x)^2 + (y - p2.y)^2 => 2(p2.x - p1.x)x + 2(p2.y - p1.y)y = p2.x^2 + p2.y^2 - p1.x^2 - p1.y^2 => Ax + By = C (x - p2.x)^2 + (y - p2.y)^2 = (x - p3.x)^2 + (y - p3.y)^2 => 2(p3.x - p2.x)x + 2(p3.y - p2.y)y = p3.x^2 + p3.y^2 - p2.x^2 - p2.y^2 => Dx + Ey = F */ // Cramer's rule double A = 2 * (p2.x() - p1.x()); double B = 2 * (p2.y() - p1.y()); double C = p2.x() * p2.x() + p2.y() * p2.y() - p1.x() * p1.x() - p1.y() * p1.y(); double D = 2 * (p3.x() - p2.x()); double E = 2 * (p3.y() - p2.y()); double F = p3.x() * p3.x() + p3.y() * p3.y() - p2.x() * p2.x() - p2.y() * p2.y(); double delta = A*E - B*D; double x = (C*E - B*F) / delta; double y = (A*F - C*D) / delta; return QPointF(x, y); } qreal VoronoiDiagram::crossProduct(const QPointF& o, const QPointF& a, const QPointF& b) { return (a.x() - o.x()) * (b.y() - o.y()) - (a.y() - o.y()) * (b.x() - o.x()); } QVector VoronoiDiagram::getVoronoiEdges(QVector& points) { P = sortAndUnique(points); int n = P.size(); VoronoiResult result; clearSteps(); if (areAllCollinear(0, n)) { result = solveCollinear(0, n); addStep(result.edges, QVector(), QVector(), "Collinear Case"); addStep(result.edges, QVector(), hullPoints(result.convexHull), "Collinear Case"); } else if (n == 3) { result = voronoi3(0); addStep(result.edges, QVector(), QVector()); addStep(result.edges, QVector(), hullPoints(result.convexHull)); } else result = divideAndConquer(0, n); return result.edges; } VoronoiResult VoronoiDiagram::divideAndConquer(int first, int last) { VoronoiResult res; int n = last - first; if (n == 1) { res.convexHull.append(first); linkRegions(res, first, last); } else if (n == 2 || areAllCollinear(first, last)) // Collinear sub-problems are solved directly res = solveCollinear(first, last); else if (n == 3) res = voronoi3(first); else { // Divide int m = first + n / 2; VoronoiResult leftRes = divideAndConquer(first, m), rightRes = divideAndConquer(m, last); // Merge res = merge(leftRes, rightRes); } return res; } VoronoiResult VoronoiDiagram::merge(const VoronoiResult& left, const VoronoiResult& right) { QVector stepEdges; QVector stepHull; QVector stepTypes; // Step 1. Left voronoi, type 1 (blue) stepEdges.append(left.edges); stepTypes.append(QVector(left.edges.size(), 1)); addStep(stepEdges, stepTypes, QVector(), "Step 1"); // Step 2. Right voronoi, type 2 (green) stepEdges.append(right.edges); stepTypes.append(QVector(right.edges.size(), 2)); addStep(stepEdges, stepTypes, QVector(), "Step 2"); // Step 3. Left convex hull, cyan const QVector leftHull = hullPoints(left.convexHull), rightHull = hullPoints(right.convexHull); stepHull = leftHull; addStep(stepEdges, stepTypes, stepHull, "Step 3"); // Step 4. Right convex hull, cyan stepHull = leftHull + rightHull; addStep(stepEdges, stepTypes, stepHull, "Step 4", leftHull.size()); // Find the upper tangent and the lower tangent QPair upperTan = getUpperTangent(leftHull, rightHull); QPair lowerTan = getLowerTangent(leftHull, rightHull); // Step 5. Merged convex hull, cyan const QVector mergedHull = mergeConvexHull(left.convexHull, right.convexHull, upperTan, lowerTan); stepHull = hullPoints(mergedHull); addStep(stepEdges, stepTypes, stepHull, "Step 5"); // Step 6. Scan for Hyperplane (scan line), type 3 (magenta) // Edges of both diagrams, the right edges after the left ones. edges are clipped in place const int nLeft = left.edges.size(), nEdges = nLeft + right.edges.size(); QVector edges = left.edges + right.edges; const QVector> sites = left.sites + right.sites; QVector> next = left.next, prev = left.prev; for (int i = 0; i < right.edges.size(); ++i) { next.append({right.next[i][0] + nLeft, right.next[i][1] + nLeft}); prev.append({right.prev[i][0] + nLeft, right.prev[i][1] + nLeft}); } const int firstSite = left.firstSite, nSites = left.anyEdge.size() + right.anyEdge.size(); QVector anyEdge = left.anyEdge; for (int e : right.anyEdge) anyEdge.append(e < 0 ? -1 : e + nLeft); // removed: the edge lies on the wrong side, hit: the hyperplane met the edge, // passed: walking around a region, the edge came before the point where the hyperplane leaves it QVector removed(nEdges, false), hit(nEdges, false), passed(nEdges, false); // Walking around the region of point s: clockwise (turn = 1) or counterclockwise (turn = -1) auto step = [&](int e, int s, int turn) { int side = (sites[e].first == s) ? 0 : 1; return (turn > 0) ? prev[e][side] : next[e][side]; }; // Unit vector from point s to the point on the other side of edge e, perpendicular to the edge auto normal = [&](int e, int s) { QPointF d = P[otherSite(sites, e, s)] - P[s]; return d / std::sqrt(QPointF::dotProduct(d, d)); }; // Between consecutive edges u and w (turning from u to w) the region goes to infinity, // when the angle from the normal of u to that of w is at least 180 degrees auto isGap = [&](int u, int w, int s, int turn) { if (u == w) return true; QPointF a = normal(u, s), b = normal(w, s); double c = turn * crossProduct(QPointF(), a, b); // Positive when turning the way of the walk return c < -ANGLE_EPSILON || (std::abs(c) <= ANGLE_EPSILON && QPointF::dotProduct(a, b) < 0); }; // A ray in direction dir goes to infinity through the gap between u and w: it moves away from both // edges. When they are parallel (a strip), the gap on this side is the one turned to from u auto throughGap = [&](int u, int w, int s, int turn, const QPointF& dir) { QPointF a = normal(u, s); if (QPointF::dotProduct(a, dir) > ANGLE_EPSILON) return false; if (u == w) return true; QPointF b = normal(w, s); if (QPointF::dotProduct(b, dir) > ANGLE_EPSILON) return false; return std::abs(crossProduct(QPointF(), a, b)) > ANGLE_EPSILON || turn * crossProduct(QPointF(), a, dir) > 0; }; // An intersection of the hyperplane with an edge of the region of L (or R) struct Hit { bool isLeft; int edgeIdx; int other; // The other point that generated the edge QPointF point; // Intersection double t; // Position along the hyperplane direction bool atEndPoint; // Intersection is an existing Voronoi vertex }; int L = left.convexHull[upperTan.first], R = right.convexHull[upperTan.second]; QPointF mid, dir; double currentT = 0; // Whether the bisector of L and R, below the current position, meets edge e of the region of point s auto meets = [&](int e, int s, bool isLeft, Hit& out) { int other = otherSite(sites, e, s); // The edge lies on the bisector of s and other, so it meets the bisector of L and R // at the circumcenter of L, R and other (parallel if they are collinear) if (std::abs(crossProduct(P[L], P[R], P[other])) <= EPSILON) return false; QPointF p = calculateCircumcenter(P[L], P[R], P[other]); double t = QPointF::dotProduct(p - mid, dir); if (t <= currentT + EPSILON) return false; // The intersection must lie on the (clipped) edge const QLineF& line = edges[e]; QPointF eDir = line.p2() - line.p1(); eDir /= std::sqrt(QPointF::dotProduct(eDir, eDir)); if (QPointF::dotProduct(p - line.p1(), eDir) < -EPSILON || QPointF::dotProduct(line.p2() - p, eDir) < -EPSILON) return false; // Snap to an existing vertex so that the same vertex keeps the same coordinates bool atEndPoint = true; if (QLineF(p, line.p1()).length() < EPSILON) p = line.p1(); else if (QLineF(p, line.p2()).length() < EPSILON) p = line.p2(); else atEndPoint = false; out = {isLeft, e, other, p, t, atEndPoint}; return true; }; // Find where the hyperplane leaves the region of point s, walking around it from edge ptr // (atGap: from the gap before ptr, where the hyperplane last went to infinity or came from). // Going down, the hyperplane leaves each region at points further along its boundary (clockwise for // a left region, counterclockwise for a right one), so ptr only moves forward. The edges walked // past lie on the side the hyperplane cuts off, and each one is walked past once. // Returns 1 when it meets edges, 0 when it goes to infinity, -1 if it is not found (unexpected) auto walk = [&](int s, bool isLeft, int& ptr, bool& atGap, QVector& hits) { if (ptr < 0) return 0; // The region has no edges const int turn = isLeft ? 1 : -1; if (atGap && throughGap(step(ptr, s, -turn), ptr, s, turn, dir)) return 0; // Through the same gap QVector walked; for (int e = ptr;;) { Hit h; if (!removed[e] && meets(e, s, isLeft, h)) { hits.append(h); ptr = e; atGap = false; // The next edges may meet it at the same vertex for (int f = e, g = step(f, s, turn); g != e && !isGap(f, g, s, turn) && !removed[g] && meets(g, s, isLeft, h); f = g, g = step(f, s, turn)) hits.append(h); for (int w : std::as_const(walked)) passed[w] = true; return 1; } int w = step(e, s, turn); walked.append(e); if (isGap(e, w, s, turn) && throughGap(e, w, s, turn, dir)) { for (int x : std::as_const(walked)) passed[x] = true; ptr = w; atGap = true; return 0; } e = w; if (e == ptr) return -1; } }; // The hyperplane comes from infinity: the walk around the region of s starts after the gap it comes through auto entry = [&](int s, bool isLeft, const QPointF& up, bool& atGap) { atGap = false; const int turn = isLeft ? 1 : -1, start = anyEdge[s - firstSite]; if (start < 0) return -1; int e = start; do { int w = step(e, s, turn); if (isGap(e, w, s, turn) && throughGap(e, w, s, turn, up)) { atGap = true; return w; } e = w; } while (e != start); return start; // Not found (unexpected): walk the whole region }; int ptrL, ptrR; bool gapL, gapR; { QPointF vec = P[R] - P[L]; QPointF up(vec.y(), -vec.x()); ptrL = entry(L, true, up, gapL); ptrR = entry(R, false, up, gapR); } QVector hyperPlane; QVector> hyperPlaneSites; bool atStart = true; // The hyperplane starts from infinity above the upper tangent QPointF currentPos; bool unexpected = false; // --- Hyperplane Scan Loop --- int loopSafety = 0; const int MAX_LOOP = nEdges + 10; while (true) { if (loopSafety++ > MAX_LOOP) { qWarning() << "Hyperplane scan loop limit reached!"; break; } // 1. The perpendicular bisector of current L and R, dir points downwards along the hyperplane mid = (P[L] + P[R]) / 2; QPointF vec = P[R] - P[L]; dir = QPointF(-vec.y(), vec.x()); dir /= std::sqrt(QPointF::dotProduct(dir, dir)); currentT = atStart ? -qInf() : QPointF::dotProduct(currentPos - mid, dir); // 2. Find where it leaves the regions of L and R QVector hits; if (walk(L, true, ptrL, gapL, hits) < 0) unexpected = true; if (walk(R, false, ptrR, gapR, hits) < 0) unexpected = true; // No more intersections: the rest of the hyperplane goes to infinity if (hits.isEmpty()) { QPointF start = atStart ? rayEnd(mid, -dir) : currentPos; hyperPlane.append(QLineF(start, rayEnd(atStart ? mid : currentPos, dir))); hyperPlaneSites.append(qMakePair(L, R)); break; } // 3. Determine Hit Event: all intersections at the nearest point are handled together // (more than one when 4 or more points lie on the same circle) double bestT = hits[0].t; for (const Hit& h : std::as_const(hits)) bestT = std::min(bestT, h.t); QVector nearHits; for (const Hit& h : std::as_const(hits)) if (h.t <= bestT + EPSILON) nearHits.append(h); QPointF v = nearHits[0].point; for (const Hit& h : std::as_const(nearHits)) if (h.atEndPoint) { v = h.point; break; } hyperPlane.append(QLineF(atStart ? rayEnd(v, -dir) : currentPos, v)); hyperPlaneSites.append(qMakePair(L, R)); // 4. Find the next L and R // All generating points lie on a circle centered at v. Measured from the incoming hyperplane // and turning towards L, the angles go: L (smallest) ... next L, next R ... R (largest) QPointF back = -dir; double side = (crossProduct(QPointF(), back, P[L] - v) > 0) ? 1 : -1; auto angleOf = [&](const QPointF& w) { double a = std::atan2(side * crossProduct(QPointF(), back, w), QPointF::dotProduct(back, w)); return (a < 0) ? a + 2 * M_PI : a; }; int nextL = L, nextR = R; double maxAngleL = angleOf(P[L] - v), minAngleR = angleOf(P[R] - v); for (const Hit& h : std::as_const(nearHits)) { double a = angleOf(P[h.other] - v); if (h.isLeft && a > maxAngleL) { maxAngleL = a; nextL = h.other; } if (!h.isLeft && a < minAngleR) { minAngleR = a; nextR = h.other; } } QPointF nextVec = P[nextR] - P[nextL]; double outAngle = angleOf(QPointF(-nextVec.y(), nextVec.x())); // Outgoing hyperplane // 5. Clip Edges: keep the parts on their own side of the hyperplane // (left side: between the incoming and the outgoing hyperplane, right side: the rest) for (const Hit& h : std::as_const(nearHits)) { QLineF &e = edges[h.edgeIdx]; auto keepPart = [&](const QPointF& endPoint) { if (QLineF(v, endPoint).length() < EPSILON) return false; return (angleOf(endPoint - v) < outAngle) == h.isLeft; }; bool keep1 = keepPart(e.p1()), keep2 = keepPart(e.p2()); if (keep1 && !keep2) e.setP2(v); else if (!keep1 && keep2) e.setP1(v); else if (!keep1 && !keep2) removed[h.edgeIdx] = true; // Entirely on the wrong side hit[h.edgeIdx] = true; } // 6. The walk around the region of a new L (or R) starts at the edge the hyperplane crossed into it for (const Hit& h : std::as_const(nearHits)) { if (h.isLeft && nextL != L && h.other == nextL) { ptrL = h.edgeIdx; gapL = false; } if (!h.isLeft && nextR != R && h.other == nextR) { ptrR = h.edgeIdx; gapR = false; } } L = nextL; R = nextR; currentPos = v; atStart = false; } stepEdges.append(hyperPlane); stepTypes.append(QVector(hyperPlane.size(), 3)); addStep(stepEdges, stepTypes, stepHull, "Step 6"); // --- Combine Results --- // Remove the edges on the wrong side: those clipped away entirely and those walked past. // Append the remaining edges and the new hyperplane VoronoiResult res; QVector newIndex(nEdges, -1); QVector changed(nSites, false); // The region lost edges for (int i = 0; i < nEdges; ++i) { if (removed[i] || (passed[i] && !hit[i])) { changed[sites[i].first - firstSite] = changed[sites[i].second - firstSite] = true; continue; } newIndex[i] = res.edges.size(); res.edges.append(edges[i]); res.sites.append(sites[i]); } const int firstHyperPlane = res.edges.size(); res.edges.append(hyperPlane); res.sites.append(hyperPlaneSites); res.convexHull = mergedHull; res.firstSite = firstSite; res.anyEdge = QVector(nSites, -1); res.next = res.prev = QVector>(res.edges.size(), {-1, -1}); // Hyperplane edges of each region, in the order along the hyperplane QVector> added(nSites); for (int k = 0; k < hyperPlane.size(); ++k) { added[hyperPlaneSites[k].first - firstSite].append(firstHyperPlane + k); added[hyperPlaneSites[k].second - firstSite].append(firstHyperPlane + k); } // Regions the hyperplane did not touch keep their edges and order for (int i = 0; i < nEdges; ++i) { if (newIndex[i] < 0) continue; for (int k = 0; k < 2; ++k) { int s = ((k == 0) ? sites[i].first : sites[i].second) - firstSite; if (changed[s] || !added[s].isEmpty()) continue; res.next[newIndex[i]][k] = newIndex[next[i][k]]; res.prev[newIndex[i]][k] = newIndex[prev[i][k]]; } } // The other regions: the remaining edges in their old order and the hyperplane edges are both sorted // counterclockwise by the direction to the other point, so merge them. Along the hyperplane, a right // region is passed counterclockwise and a left region clockwise auto direction = [&](int e, int s) { QPointF d = P[otherSite(res.sites, e, s)] - P[s]; return std::atan2(d.y(), d.x()); // Counterclockwise on the screen (+y points down) is decreasing }; // Rotate a list sorted counterclockwise (cyclically) to start at the largest angle; false if it is not sorted auto startAtLargest = [&](QVector& list, int s) { if (list.size() < 2) return true; int top = 0; for (int i = 1; i < list.size(); ++i) if (direction(list[i], s) > direction(list[top], s)) top = i; std::rotate(list.begin(), list.begin() + top, list.end()); for (int i = 1; i < list.size(); ++i) if (direction(list[i], s) > direction(list[i - 1], s)) return false; return true; }; for (int s = 0; s < nSites; ++s) { const int site = firstSite + s; if (!changed[s] && added[s].isEmpty()) { if (anyEdge[s] >= 0) res.anyEdge[s] = newIndex[anyEdge[s]]; continue; } QVector kept; if (anyEdge[s] >= 0) { int e = anyEdge[s], count = 0; do { if (newIndex[e] >= 0) kept.append(newIndex[e]); e = next[e][(sites[e].first == site) ? 0 : 1]; } while (e != anyEdge[s] && ++count <= nEdges); } QVector& more = added[s]; if (site < right.firstSite) std::reverse(more.begin(), more.end()); QVector order; auto later = [&](int a, int b) { return direction(a, site) > direction(b, site); }; if (startAtLargest(kept, site) && startAtLargest(more, site)) std::merge(kept.begin(), kept.end(), more.begin(), more.end(), std::back_inserter(order), later); else { unexpected = true; order = kept + more; std::sort(order.begin(), order.end(), later); } int m = order.size(); for (int k = 0; k < m; ++k) { int e = order[k], side = (res.sites[e].first == site) ? 0 : 1; res.next[e][side] = order[(k + 1) % m]; res.prev[e][side] = order[(k - 1 + m) % m]; } if (m > 0) res.anyEdge[s] = order[0]; } if (unexpected) qWarning() << "Merge: unexpected hyperplane"; // Step 7. Final result stepEdges = res.edges; stepTypes = QVector(res.edges.size(), 0); // 0: Normal/Result, red addStep(stepEdges, stepTypes, stepHull, "Step 7"); return res; } QVector VoronoiDiagram::mergeConvexHull(const QVector& left, const QVector& right, const QPair& upperTan, const QPair& lowerTan) { QVector mergedHull; int nL = left.size(), nR = right.size(); // Left convex hull: Collect points counterclockwise from upperTan.first to lowerTan.first int idx = upperTan.first; mergedHull.append(left[idx]); while (idx != lowerTan.first) { idx = (idx + 1) % nL; // Move counterclockwise mergedHull.append(left[idx]); } // Right convex hull: Collect points counterclockwise from lowerTan.second to upperTan.second idx = lowerTan.second; mergedHull.append(right[idx]); while (idx != upperTan.second) { idx = (idx + 1) % nR; mergedHull.append(right[idx]); } return mergedHull; } QVector VoronoiDiagram::hullPoints(const QVector& hull) { QVector points; for (int i : hull) points.append(P[i]); return points; } QPair VoronoiDiagram::getUpperTangent(const QVector& leftHull, const QVector& rightHull) { int nL = leftHull.size(), nR = rightHull.size(); // Find rightmost of left hull (lexicographically, so ties in x pick the point next to the right hull) int l = 0; for (int i = 1; i < nL; ++i) if (comparePoints(leftHull[l], leftHull[i])) l = i; // Find leftmost of right hull int r = 0; for (int i = 1; i < nR; ++i) if (comparePoints(rightHull[i], rightHull[r])) r = i; // Adjust points using the Walking Algorithm bool done = false; while (!done) { done = true; // Adjust left point: If cross product > EPSILON (positive), it means the line cuts into the hull // We need to move counterclockwise (l + 1) to find the tangent while (crossProduct(rightHull[r], leftHull[l], leftHull[(l + 1) % nL]) > EPSILON) { l = (l + 1) % nL; done = false; } // Adjust right point: If cross product < -EPSILON (negative), it means the line cuts into the hull // We need to move clockwise (r - 1) to find the tangent while (crossProduct(leftHull[l], rightHull[r], rightHull[(r - 1 + nR) % nR]) < -EPSILON) { r = (r - 1 + nR) % nR; done = false; } } return qMakePair(l, r); } QPair VoronoiDiagram::getLowerTangent(const QVector& leftHull, const QVector& rightHull) { int nL = leftHull.size(), nR = rightHull.size(); int l = 0; for (int i = 1; i < nL; ++i) if (comparePoints(leftHull[l], leftHull[i])) l = i; int r = 0; for (int i = 1; i < nR; ++i) if (comparePoints(rightHull[i], rightHull[r])) r = i; bool done = false; while (!done) { done = true; while (crossProduct(rightHull[r], leftHull[l], leftHull[(l - 1 + nL) % nL]) < -EPSILON) { l = (l - 1 + nL) % nL; done = false; } while (crossProduct(leftHull[l], rightHull[r], rightHull[(r + 1) % nR]) > EPSILON) { r = (r + 1) % nR; done = false; } } return qMakePair(l, r); } bool VoronoiDiagram::areAllCollinear(int first, int last) { if (last - first < 3) return true; const QPointF& p1 = P[first]; const QPointF& p2 = P[first + 1]; // Check if every subsequent point lies on the line defined by p1 and p2 (cross product is 0) for (int i = first + 2; i < last; ++i) if (std::abs(crossProduct(p1, p2, P[i])) > EPSILON) return false; return true; } VoronoiResult VoronoiDiagram::solveCollinear(int first, int last) { VoronoiResult res; for (int i = first; i < last - 1; ++i) { // Directly calculate the perpendicular bisector of adjacent points res.edges.append(getPerpBisector(P[i], P[i + 1])); res.sites.append(qMakePair(i, i + 1)); } // Adding just the first and last points if (last > first) { res.convexHull.append(first); res.convexHull.append(last - 1); } linkRegions(res, first, last); return res; }