Skip to content

Commit

Permalink
Add static point simulation
Browse files Browse the repository at this point in the history
  • Loading branch information
leonmavr committed Sep 13, 2024
1 parent 37bcb78 commit 2eb56da
Showing 1 changed file with 53 additions and 75 deletions.
128 changes: 53 additions & 75 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,83 +1,61 @@
#include "quad.h"
#include <stdio.h>
#include "viz.h"
#include <limits.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
// TODO: this should be less than what the visualiser can handle
#define NPARTICLES 100

int main() {
#if 0
quadtree_t qtree;
rect_t region = (rect_t) {0, 0, 200, 200};
qtree.root = node_new(&region);
point_t p1 = {10, 20}, p2 = {15, 190}, p3 = {30, 120}, p4 = {12, 20}, p5 = {48, 10};
node_insert(qtree.root, &p1);
node_insert(qtree.root, &p2);
node_insert(qtree.root, &p3);
node_insert(qtree.root, &p4);
node_insert(qtree.root, &p5);
printf("nw->nw->ne: (%d, %d)\n", qtree.root->nw->nw->ne->points[0].x, qtree.root->nw->nw->ne->points[0].y);

rect_t r1 = {10, 10, 20, 20}, r2 = {21, 21, 24, 24}, r3 = {2, 2, 11, 11}, r4 = {19, 19, 23, 23};
bool i1 = rect_intersect(&r1, &r2), i2 = rect_intersect(&r1, &r3), i3 = rect_intersect(&r1, &r4);
printf("%d, %d, %d\n", i1, i2, i3);
int count = 0;
rect_t search = {11, 19, 200, 200};
quadtree_query(qtree.root, &search, &count);
printf("search: %d\n", count);
point_t pnew = {42, 42};
qtree_update_point(&qtree, &p3, &pnew);
count = 0;
quadtree_query(qtree.root, &search, &count);
printf("search: %d\n", count);
qtree_delete(qtree.root);
return 0;
#else
#if 0
quadtree_t qtree;
rect_t region = {0, 0, 200, 200};
qtree.root = node_new(&region);
point_t p1 = {10, 20}, p2 = {15, 190}, p3 = {30, 120}, p4 = {12, 20}, p5 = {48, 10};
node_insert(qtree.root, &p1);
node_insert(qtree.root, &p2);
node_insert(qtree.root, &p3);
node_insert(qtree.root, &p4);
node_insert(qtree.root, &p5);
node_remove_point(qtree.root, &p4);
static size_t id_particle = 0;

typedef struct particle_t {
float accelerationx, accelerationy;
float velx, vely;
point_t point;
} particle_t;

point_t query_point = {25, 30};
point_t nearest_point;
double best_dist = INT_MAX;
quadtree_nearest_neighbor(qtree.root, &query_point, &nearest_point, &best_dist);
void reflect(particle_t* particle, rect_t* boundary, float dt) {
const int w = boundary->x1;
const int h = boundary->y1;
if (particle->point.x + particle->velx * dt > w) {
particle->point.x -= w - particle->velx * dt;
particle->velx *= -1;
} else if (particle->point.x + particle->velx * dt < boundary->x0) {
particle->point.x = -particle->velx * dt - particle->point.x;
particle->velx *= -1;
}
if (particle->point.y + particle->vely * dt > h) {
particle->point.y -= h - particle->vely * dt;
particle->vely *= -1;
} else if (particle->point.y + particle->vely * dt < boundary->y0) {
particle->point.y = -particle->vely * dt - particle->point.y;
particle->vely *= -1;
}
}

printf("nearest point to (%d, %d): (%d, %d) with distance: %.2f\n",
query_point.x, query_point.y, nearest_point.x, nearest_point.y, sqrt(best_dist));
#else
quadtree_t qtree;
rect_t region = {0, 0, 200, 200};
qtree.root = node_new(&region);
point_t p1 = {30, 30}, p2 = {31, 31};
rect_t rects[4];
rect_divide(&region, rects);
qtree.root->nw = node_new(&rects[IND_NW]);
qtree.root->ne = node_new(&rects[IND_NE]);
qtree.root->se = node_new(&rects[IND_SE]);
qtree.root->sw = node_new(&rects[IND_SE]);
rect_divide(&qtree.root->nw->boundary, rects);
qtree.root->nw->nw = node_new(&rects[IND_NW]);
qtree.root->nw->ne = node_new(&rects[IND_NE]);
qtree.root->nw->nw->points[0] = p1;
qtree.root->nw->nw->count++;

qtree.root->nw->ne->points[0] = p2;
qtree.root->nw->ne->count++;
qtree.root->nw->se = node_new(&rects[IND_SE]);
qtree.root->nw->sw = node_new(&rects[IND_SE]);
node_merge(qtree.root);
printf("merged: %d, %d\n", qtree.root->nw->points[0].x, qtree.root->nw->points[0].y);
printf("merged: %d, %d\n", qtree.root->nw->points[1].x, qtree.root->nw->points[1].y);
printf("merged: %d, %d\n", qtree.root->nw->points[2].x, qtree.root->nw->points[2].y);
int i = 0;
static particle_t particles[NPARTICLES];

#endif
return 0;
#endif
static size_t ids = 0;

int main() {
srand(time(NULL));
rect_t boundary = {.x0=0, .y0=0, .x1=400, .y1=400};
point_t points[NPARTICLES];
particle_t particles[NPARTICLES];
for (int i = 0; i < NPARTICLES; ++i) {
points[i].x = random() % boundary.x1;
points[i].y = random() % boundary.y1;
points[i].id = ids++;
}
quadtree_t qtree;
viz_init(boundary.x1, boundary.y1);
qtree.root = node_new(&boundary);
for (int i = 0; i < NPARTICLES; ++i) {
node_insert(qtree.root, &points[i]);
}
qtree_graph(qtree.root);
viz_flush();
sleep(3);
viz_close();
}

0 comments on commit 2eb56da

Please sign in to comment.