c++的邻接矩阵图形化实现
- User
c++的邻接矩阵图形化实现
- Assistant
这里提供一种基于QT的实现方式:
-
创建一个QT Widgets Application项目
-
在mainwindow.cpp文件中创建一个邻接矩阵类
class AdjacencyMatrix { public: AdjacencyMatrix(int n) { numVertices = n; data = new int*[numVertices]; for(int i=0; i<numVertices; i++) { data[i] = new int[numVertices]; for(int j=0; j<numVertices; j++) { data[i][j] = 0; } } } void AddEdge(int v1, int v2) { data[v1][v2] = 1; data[v2][v1] = 1; } int GetNumVertices() { return numVertices; } int** GetData() { return data; } private: int numVertices; int** data; };- 在mainwindow.h文件中添加一个QLabel成员变量用于显示矩阵
private: QLabel* matrixLabel;- 在构造函数中初始化matrixLabel,并且添加一个按钮处理点击事件
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { setFixedSize(300, 300); // 创建邻接矩阵 AdjacencyMatrix* matrix = new AdjacencyMatrix(4); matrix->AddEdge(0, 1); matrix->AddEdge(0, 2); matrix->AddEdge(1, 3); matrix->AddEdge(2, 3); // 创建一个QLabel显示矩阵 matrixLabel = new QLabel(this); matrixLabel->setGeometry(10, 10, 200, 200); matrixLabel->setAlignment(Qt::AlignLeft | Qt::AlignTop); matrixLabel->setStyleSheet("font: 12pt"); // 创建一个按钮,并处理点击事件 QPushButton* updateButton = new QPushButton("Update", this); updateButton->setGeometry(220, 10, 70, 30); connect(updateButton, &QPushButton::clicked, [=]() { // 更新矩阵的显示 int** data = matrix->GetData(); QString str; for(int i=0; i<matrix->GetNumVertices(); i++) { for(int j=0; j<matrix->GetNumVertices(); j++) { str += QString::number(data[i][j]); str += " "; } str += "\n"; } matrixLabel->setText(str); }); }- 运行程序,点击Update按钮,即可看到矩阵的显示。
-
- User
我要是想用这个矩阵生成图形化的地图呢?
- Assistant
生成图形化的地图需要用到一些图形库,比如Qt、OpenGL等。这里我们以Qt为例,演示如何将邻接矩阵转换成图形化的地图。
- 首先,我们需要在mainwindow.cpp文件中创建一个MapView类
class MapView : public QGraphicsView { public: MapView(QWidget* parent = NULL) : QGraphicsView(parent) {} void setMap(int** data, int n) { scene = new QGraphicsScene(); for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { if(data[i][j] == 1) { QGraphicsLineItem* line = new QGraphicsLineItem(QLineF(i * 50, j * 50, (i+1) * 50, (j+1) * 50)); line->setPen(QPen(Qt::black, 3)); scene->addItem(line); QGraphicsLineItem* line2 = new QGraphicsLineItem(QLineF((i+1) * 50, j * 50, i * 50, (j+1) * 50)); line2->setPen(QPen(Qt::black, 3)); scene->addItem(line2); } } } setScene(scene); } private: QGraphicsScene* scene; };该类继承自QGraphicsView,用于显示图形化的地图。setMap方法用于将邻接矩阵转换成地图,并在该MapView里面显示。
- 在构造函数中添加一个MapView成员变量,并且添加一个按钮处理点击事件
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { setFixedSize(300, 300); AdjacencyMatrix* matrix = new AdjacencyMatrix(4); matrix->AddEdge(0, 1); matrix->AddEdge(0, 2); matrix->AddEdge(1, 3); matrix->AddEdge(2, 3); // 创建一个QLabel显示矩阵 matrixLabel = new QLabel(this); matrixLabel->setGeometry(10, 10, 200, 200); matrixLabel->setAlignment(Qt::AlignLeft | Qt::AlignTop); matrixLabel->setStyleSheet("font: 12pt"); // 创建一个按钮,并处理点击事件 QPushButton* updateButton = new QPushButton("Update", this); updateButton->setGeometry(220, 10, 70, 30); connect(updateButton, &QPushButton::clicked, [=]() { // 更新矩阵的显示 int** data = matrix->GetData(); QString str; for(int i=0; i<matrix->GetNumVertices(); i++) { for(int j=0; j<matrix->GetNumVertices(); j++) { str += QString::number(data[i][j]); str += " "; } str += "\n"; } matrixLabel->setText(str); // 将矩阵转换成地图,并在MapView里面显示 mapView->setMap(data, matrix->GetNumVertices()); }); // 创建一个MapView用于显示地图 mapView = new MapView(this); mapView->setGeometry(10, 220, 280, 70); }- 运行程序,点击Update按钮,即可在MapView里面显示地图。注意,这里的地图是由线段组成的,可以根据需要进行美化。
- User
不使用类如何实现?
- Assistant
以下是不使用类实现邻接矩阵图形化的一种方法。
- 在头文件中定义常量和函数
#include <iostream> #include <cstdlib> #include <ctime> #include <Windows.h> using namespace std; const int kWidth = 800; const int kHeight = 800; const int kNumVertices = 7; const int kRadius = 20; bool isAdjacent(int** matrix, int u, int v); void drawVertex(HWND hwnd, HDC hdc, int i); void drawEdge(HDC hdc, int** matrix, int a, int b);其中isAdjacent函数用于判断两个顶点是否相邻,drawVertex函数用于绘制顶点,drawEdge函数用于绘制边。
- 在源文件中定义以上函数
bool isAdjacent(int** matrix, int u, int v) { return matrix[u][v] == 1; } void drawVertex(HWND hwnd, HDC hdc, int i) { char c[2] = { i + '0', '\0' }; SetTextColor(hdc, RGB(0, 0, 0)); SetBkMode(hdc, TRANSPARENT); Ellipse(hdc, i * kWidth / kNumVertices, kHeight / 2 - kRadius, i * kWidth / kNumVertices + kRadius, kHeight / 2); TextOut(hdc, i * kWidth / kNumVertices + kRadius / 2 - 3, kHeight / 2 - kRadius / 2, c, 1); } void drawEdge(HDC hdc, int** matrix, int a, int b) { if (isAdjacent(matrix, a, b)) { POINT pt[2]; pt[0].x = a * kWidth / kNumVertices + kRadius / 2; pt[0].y = kHeight / 2 - kRadius / 2; pt[1].x = b * kWidth / kNumVertices + kRadius / 2; pt[1].y = kHeight / 2 + kRadius / 2; Polyline(hdc, pt, 2); } }- 在WinMain函数中创建画布并进行绘制
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { HWND hwnd; MSG Msg; //创建窗口 hwnd = createWindow(hInstance); // 创建邻接矩阵 int** matrix = new int*[kNumVertices]; for (int i = 0; i < kNumVertices; i++) { matrix[i] = new int[kNumVertices]; for (int j = 0; j < kNumVertices; j++) { if (i == j) { matrix[i][j] = 0; } else { matrix[i][j] = (rand() % 2); matrix[j][i] = matrix[i][j]; } } } // 显示窗口,并开始绘制 ShowWindow(hwnd, nShowCmd); while (GetMessage(&Msg, NULL, 0, 0)) { TranslateMessage(&Msg); DispatchMessage(&Msg); HDC hdc; PAINTSTRUCT ps; switch (Msg.message) { case WM_PAINT: hdc = BeginPaint(hwnd, &ps); for (int i = 0; i < kNumVertices; i++) { drawVertex(hwnd, hdc, i); for (int j = i + 1; j < kNumVertices; j++) { drawEdge(hdc, matrix, i, j); } } EndPaint(hwnd, &ps); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd, Msg.message, Msg.wParam, Msg.lParam); } } return Msg.wParam; }- 运行程序,即可看到邻接矩阵图形化的效果。注意,这里的图形化主要是基于Windows操作系统提供的绘图API实现的,如果需要更加美观和高级的效果,可以考虑使用其他图形库。
- User
[Error] 'createWindow' was not declared in this scope
- Assistant
createWindow是一个辅助函数,可以自行编写实现。下面是一个简单的实现,仅供参考:HWND createWindow(HINSTANCE hInstance) { HWND hwnd; WNDCLASSEX wc; ZeroMemory(&wc, sizeof(WNDCLASSEX)); wc.cbSize = sizeof(WNDCLASSEX); wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = WindowProc; wc.hInstance = hInstance; wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wc.lpszClassName = "MainWindowClass"; RegisterClassEx(&wc); hwnd = CreateWindowEx( 0, "MainWindowClass", "Hello World!", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, kWidth, kHeight, NULL, NULL, hInstance, NULL ); return hwnd; }此函数用于创建窗口,需要在调用WinMain函数前声明或定义该函数,或放在头文件中。如果您使用其他方法创建窗口可以不调用此函数。
- User
17 1 F:\tim file\test.cpp [Error] expected unqualified-id before '{' token;103 22 F:\tim file\test.cpp [Error] 'WindowProc' was not declared in this scope
- Assistant
expected unqualified-id before '{' token错误通常是由于语法错误导致的,可以检查一下相邻代码行是否有语法错误,特别是前面是否有未结束的语句。而
WindowProc was not declared in this scope错误则是因为编译器找不到WindowProc函数的定义。这个函数通常是用于处理消息的函数,需要在源文件或头文件中定义或声明该函数。以下是一个示例:LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { // ... 窗口创建等相关代码 ... while (GetMessage(&Msg, NULL, 0, 0)) { TranslateMessage(&Msg); DispatchMessage(&Msg); } return Msg.wParam; } LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_DESTROY: PostQuitMessage(0); return 0; default: return DefWindowProc(hwnd, uMsg, wParam, lParam); } }在上面的代码中,我们先声明了
WindowProc函数,再在WinMain函数后面定义了该函数。这样就能够避免WindowProc was not declared in this scope错误。 - Assistant
需要更具体的错误提示才能判断问题所在,可以提供更详细的错误信息或代码,这样才能更好地帮助您解决问题。
- User
creatwindows中缺少邻接矩阵
- Assistant
createWindow函数是用来创建窗口的,而邻接矩阵可以在WinMain函数中创建。一般来说,在WinMain中创建邻接矩阵,然后在消息处理函数中进行绘制。以下是一个简单的示例代码:#include <windows.h> #include <iostream> using namespace std; const int kNumVertices = 5; int** createMatrix(int numVertices); void drawVertex(HWND hwnd, HDC hdc, int i); void drawEdge(HDC hdc, int** matrix, int a, int b); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { // 创建邻接矩阵 int** matrix = createMatrix(kNumVertices); // 创建窗口并显示 HWND hwnd; MSG Msg; WNDCLASSEX wc; ZeroMemory(&wc, sizeof(WNDCLASSEX)); wc.cbSize = sizeof(WNDCLASSEX); wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = DefWindowProc; wc.hInstance = hInstance; wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wc.lpszClassName = "HelloWorldClass"; RegisterClassEx(&wc); hwnd = CreateWindowEx( 0, "HelloWorldClass", "Hello World", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL ); ShowWindow(hwnd, nShowCmd); // 绘制邻接矩阵 while (GetMessage(&Msg, NULL, 0, 0)) { TranslateMessage(&Msg); DispatchMessage(&Msg); HDC hdc; PAINTSTRUCT ps; switch (Msg.message) { case WM_PAINT: hdc = BeginPaint(hwnd, &ps); for (int i = 0; i < kNumVertices; i++) { drawVertex(hwnd, hdc, i); for (int j = i + 1; j < kNumVertices; j++) { drawEdge(hdc, matrix, i, j); } } EndPaint(hwnd, &ps); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd, Msg.message, Msg.wParam, Msg.lParam); } } return Msg.wParam; } int** createMatrix(int numVertices) { int** matrix = new int*[numVertices]; for (int i = 0; i < numVertices; i++) { matrix[i] = new int[numVertices]; for (int j = 0; j < numVertices; j++) { if (i == j) { matrix[i][j] = 0; } else { matrix[i][j] = rand() % 2; matrix[j][i] = matrix[i][j]; } } } return matrix; } void drawVertex(HWND hwnd, HDC hdc, int i) { char c[2] = { i + '0', '\0' }; SetTextColor(hdc, RGB(0, 0, 0)); SetBkMode(hdc, TRANSPARENT); Ellipse(hdc, i * 100 + 50, 250, i * 100 + 100, 300); TextOut(hdc, i * 100 + 65, 265, c, 1); } void drawEdge(HDC hdc, int** matrix, int a, int b) { if (matrix[a][b] == 1) { POINT pt[2]; pt[0].x = a * 100 + 75; pt[0].y = 280