-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDrawingUtils.h
More file actions
65 lines (54 loc) · 1.92 KB
/
DrawingUtils.h
File metadata and controls
65 lines (54 loc) · 1.92 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
58
59
60
61
62
63
64
65
#ifndef DRAWING_UTILS_H
#define DRAWING_UTILS_H
#include <iostream>
#include <vector>
#include <unordered_map>
#include "Graph.h"
#include <cairo.h>
void drawPathTSP(const std::unordered_map<int, City> &cities, const std::vector<int> &tspPath, std::string title)
{
const int width = 2010;
const int height = 2010;
const int margin = 50;
cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
cairo_t *cr = cairo_create(surface);
cairo_set_source_rgb(cr, 1, 1, 1);
cairo_paint(cr);
cairo_set_line_width(cr, 2);
for (size_t i = 0; i < tspPath.size() - 1; ++i)
{
int currentIndex = tspPath[i];
int nextIndex = tspPath[i + 1];
const City ¤tCity = cities.at(currentIndex);
const City &nextCity = cities.at(nextIndex);
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_move_to(cr, currentCity.x + margin, currentCity.y + margin);
cairo_line_to(cr, nextCity.x + margin, nextCity.y + margin);
cairo_stroke(cr);
}
for (const auto &pair : cities)
{
const City &city = pair.second;
if (pair.first == 0)
{
// Highlight the starting city in blue
cairo_set_source_rgb(cr, 0, 0, 1);
cairo_arc(cr, city.x + margin, city.y + margin, 10, 0, 2 * M_PI);
}
else
{
cairo_set_source_rgb(cr, 1, 0, 0);
cairo_arc(cr, city.x + margin, city.y + margin, 5, 0, 2 * M_PI);
}
cairo_fill(cr);
}
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_select_font_face(cr, "Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
cairo_set_font_size(cr, 24);
cairo_move_to(cr, width / 2 - margin, margin / 2);
cairo_show_text(cr, title.c_str());
cairo_surface_write_to_png(surface, title.append(".png").c_str());
cairo_destroy(cr);
cairo_surface_destroy(surface);
}
#endif // DRAWING_UTILS_H