-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
75 lines (63 loc) · 2.3 KB
/
main.cpp
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
66
67
68
69
70
71
72
73
74
75
#include <vector>
#include <cstdlib>
#include <cmath>
#include <limits>
#include <omp.h>
#include <iostream>
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
struct Point
{
int x, y;
unsigned char r, g, b;
};
int main(int argc, char **argv)
{
if (argc < 2)
{
std::cout << "Usage: " << argv[0] << " [width] [height] [numPoints] [seed]\n";
std::cout << "width: Width of the image (default: 1000)\n";
std::cout << "height: Height of the image (default: 1000)\n";
std::cout << "numPoints: Number of points (default: 100)\n";
std::cout << "seed: Seed for the random number generator (default: 0)\n";
std::cout << "Example: " << argv[0] << " 2000 2000 1000 10\n";
return 0;
}
int width = argc > 1 ? atoi(argv[1]) : 1000;
int height = argc > 2 ? atoi(argv[2]) : 1000;
int numPoints = argc > 3 ? atoi(argv[3]) : 100;
int seed = argc > 4 ? atoi(argv[4]) : 0;
srand(seed);
std::vector<unsigned char> img(width * height * 3, 255);
std::vector<Point> points;
for (int i = 0; i < numPoints; ++i)
{
points.push_back({rand() % width, rand() % height, static_cast<unsigned char>(rand() % 256), static_cast<unsigned char>(rand() % 256), static_cast<unsigned char>(rand() % 256)});
}
#pragma omp parallel for
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
Point p{x, y};
float minDist = std::numeric_limits<float>::max();
Point closestPoint;
for (const Point &point : points)
{
//float dist = std::sqrt(std::pow(p.x - point.x, 2) + std::pow(p.y - point.y, 2));
//sqrt can be dropped if dist is squared to the power of 2 and compared to minDist
float dist = std::pow(p.x - point.x, 2) + std::pow(p.y - point.y, 2);
if (dist < minDist)
{
minDist = dist;
closestPoint = point;
}
}
img[(y * width + x) * 3 + 0] = closestPoint.r;
img[(y * width + x) * 3 + 1] = closestPoint.g;
img[(y * width + x) * 3 + 2] = closestPoint.b;
}
}
stbi_write_png("voronoi.png", width, height, 3, img.data(), width * 3);
return 0;
}