More things. So many things.

This commit is contained in:
2026-01-03 11:00:55 -05:00
parent 010bde50f5
commit d6fe969ce8
7 changed files with 168 additions and 9 deletions

View File

@@ -1,6 +1,7 @@
#include "include/gen.h"
#include "include/geom.h"
#include <stddef.h>
#include <stdlib.h>
#include <time.h>
@@ -33,9 +34,91 @@ void gen_asth() {
}
}
size_t* min_idx(int* a, size_t n, size_t* out_n) {
if (!a || n == 0) return NULL;
int min_val = a[0];
for (size_t i = 1; i < n; i++) {
if (a[i] < min_val) min_val = a[i];
}
size_t o = 0;
for (size_t i = 0; i < n; i++) {
if (a[i] == min_val) o++;
}
size_t* idxs = malloc(o * sizeof(size_t));
if (!idxs) {
*out_n = 0;
return NULL;
}
size_t k = 0;
for (size_t i = 0; i < n; i++) {
if (a[i] == min_val) idxs[k++] = i;
}
*out_n = o;
return idxs;
}
void gen_lith() {
srand(time(NULL));
Pt centers[NPLATES]; // The centers of each plate to be generated.
// Generate random plate centers.
for (int i = 0; i < NPLATES; i++) {
Pt p = (Pt){rand() % WORLD_SZ, rand() % WORLD_SZ};
centers[i] = p;
}
Pt* plareas[NPLATES];
int plareas_count[NPLATES];
for (int i = 0; i < NPLATES; i++) {
plareas[i] = (Pt*)malloc(
WORLD_SZ * WORLD_SZ * sizeof(Pt)
); // Points in the plate area.
plareas_count[i] = 0; // Number of points added to the plate area.
}
for (int y = 0; y < WORLD_SZ; y++) {
for (int x = 0; x < WORLD_SZ; x++) {
Pt pt = (Pt){x, y};
int distances[NPLATES]; // Distances from each plate to the current
// point.
for (int i = 0; i < NPLATES; i++) {
distances[i] = pt_cmp(&pt, &centers[i]);
}
size_t n;
size_t* idxs = min_idx(
distances, NPLATES, &n
); // The index(es) of the closest plate(s) in the centers array.
for (int i = 0; i < n; i++) {
plareas[idxs[i]][plareas_count[idxs[i]]++] = pt;
}
}
}
Pt* plhulls[NPLATES]; // The convex hulls of each plate.
size_t plhulls_count[NPLATES]; // The number of points in each hull.
for (int i = 0; i < NPLATES; i++) {
size_t count;
plhulls[i] = cvx_hull(plareas[i], plareas_count[i], &count);
plhulls_count[i] = count;
}
Plate pls[NPLATES];
for (int i = 0; i < NPLATES; i++) {
Tri* tris = malloc(plhulls_count[i] / 3 + 2);
tris[0] = (Tri){plhulls[i][0], plhulls[i][1], plhulls[i][2]};
}
Tri* tri1 = tri_init((Pt){0, 0}, (Pt){0, 7}, (Pt){7, 0});
Tri* tri2 = tri_init((Pt){7, 0}, (Pt){0, 7}, (Pt){7, 7});