-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstruct.cpp
More file actions
55 lines (54 loc) · 1.7 KB
/
Copy pathconstruct.cpp
File metadata and controls
55 lines (54 loc) · 1.7 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
/* Constructs an image based on a voronoi diagram */
#include "voronoi.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>
int WIDTH = 5000;
int HEIGHT = 5000;
std::pair<ld, ld> convertToPixels(ld x, ld y, ld width, ld height, VoronoiDiagram* v)
{
ld a = (x - v->mnx)/(v->mxx - v->mnx);
a *= width;
ld b = (y - v->mny)/(v->mxy - v->mny);
b *= height;
return { a, b };
}
void makeImage(std::string imagename, VoronoiDiagram* v, bool colour)
{
srand(time(nullptr));
cv::Mat image = cv::Mat(HEIGHT, WIDTH, CV_8UC3, cv::Scalar(255,255,255));
if (colour) // Colour the cells
{
for (auto cell : v->cells)
{
cell->sortPoints();
cv::Point*
ppt[1];
ppt[0] = new cv::Point[cell->points.size()];
for (unsigned int i = 0; i < cell->points.size(); i++)
{
std::pair<ld, ld> p = convertToPixels(cell->points[i].x, cell->points[i].y, WIDTH, HEIGHT, v);
ppt[0][i] = cv::Point(p.first, p.second);
}
int npt[1];
npt[0] = cell->points.size();
const cv::Point* PPT[1] = { ppt[0] };
cv::fillPoly(image, PPT, npt, 1, cv::Scalar(rand()%256, rand()%256, rand()%256), 8);
delete[] ppt[0];
}
}
// Draw points
for (auto a : v->points)
{
std::pair<ld, ld> p = convertToPixels(a.x, a.y, WIDTH, HEIGHT, v);
cv::circle(image, cv::Point(p.first, p.second), 20.0, cv::Scalar( 0, 0, 0 ), -1, 8);
}
// Draw lines
for (auto a : v->edges)
{
std::pair<ld, ld> fir = convertToPixels(a.first.x, a.first.y, WIDTH, HEIGHT, v);
std::pair<ld, ld> sec = convertToPixels(a.second.x, a.second.y, WIDTH, HEIGHT, v);
cv::line(image, cv::Point(fir.first, fir.second), cv::Point(sec.first, sec.second), cv::Scalar(0,0,0), 4);
}
cv::imwrite(imagename, image);
}