-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisual.cpp
More file actions
57 lines (44 loc) · 1.52 KB
/
visual.cpp
File metadata and controls
57 lines (44 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>
#include <graphviz/gvc.h>
struct Edge {
int source;
int target;
double weight;
int color;
};
std::vector<Edge> edges; // List of edges
void generateDotFile(const std::vector<Edge>& edges, const std::string& dotFileName) {
std::ofstream dotFile(dotFileName);
if (!dotFile.is_open()) {
std::cerr << "Failed to open DOT file for writing." << std::endl;
return;
}
dotFile << "graph G {" << std::endl;
for (const Edge& edge : edges) {
dotFile << edge.source << " -- " << edge.target << " [label=\"" << edge.weight << "\", color=\"" << edge.color << "\"];" << std::endl;
}
dotFile << "}" << std::endl;
dotFile.close();
}
void renderDotFile(const std::string& dotFileName, const std::string& outputFileName) {
std::string command = "dot -Tpng " + dotFileName + " -o " + outputFileName;
system(command.c_str());
}
int main() {
// Add your edges with weights and colors to the 'edges' vector
edges.push_back({0, 1, 2.0, 1});
edges.push_back({0, 2, 3.0, 2});
edges.push_back({1, 3, 4.0, 1});
edges.push_back({1, 4, 5.0, 2});
// Add more edges as needed
// Generate a DOT file
generateDotFile(edges, "graph.dot");
// Render the DOT file to a PNG image
renderDotFile("graph.dot", "graph.png");
std::cout << "Graph visualization saved as 'graph.png'." << std::endl;
return 0;
}