Commit 5063ae63 authored by ewen.madec's avatar ewen.madec

Gabriel Graph + minimum distance btw nodes

parent f6ac9d3d
...@@ -87,7 +87,7 @@ void Panel_drawNode(Panel * self, Node * n) ...@@ -87,7 +87,7 @@ void Panel_drawNode(Panel * self, Node * n)
Vector2 screenPosition= Panel_pixelFromPosition(self, &(n->position) ); Vector2 screenPosition= Panel_pixelFromPosition(self, &(n->position) );
DrawCircleV(screenPosition, 24, n->color); DrawCircleV(screenPosition, 24, n->color);
DrawCircleV(screenPosition, 20, RAYWHITE); DrawCircleV(screenPosition, 20, RAYWHITE);
char * soldierText = malloc(sizeof(int)); char * soldierText = malloc(20*sizeof(char));
sprintf(soldierText, "%d", n->soldiers + n->soldiersToAdd); sprintf(soldierText, "%d", n->soldiers + n->soldiersToAdd);
DrawText(soldierText, (int)screenPosition.x - MeasureText(soldierText, 20)/2, (int)screenPosition.y, 20, n->color); DrawText(soldierText, (int)screenPosition.x - MeasureText(soldierText, 20)/2, (int)screenPosition.y, 20, n->color);
} }
......
...@@ -11,57 +11,95 @@ ...@@ -11,57 +11,95 @@
#include "raylib.h" #include "raylib.h"
double randfrom(double min, double max)
double randfrom(double min, double max)
{ {
double range = (max - min); double range = (max - min);
double div = RAND_MAX / range; double div = RAND_MAX / range;
return min + (rand() / div); return min + (rand() / div);
} }
double dist(Node node1, Node node2){ double dist(Vector2 vector1, Vector2 vector2)
return(sqrt( (node2.position.x - node1.position.x)*(node2.position.x - node1.position.x) + (node2.position.y - node1.position.y)*(node2.position.y - node1.position.y) )); {
return (sqrt((vector2.x - vector1.x) * (vector2.x - vector1.x) + (vector2.y - vector1.y) * (vector2.y - vector1.y)));
}
double distNode(Node node1, Node node2)
{
return (dist(node1.position, node2.position));
} }
void Random_map(NetWorld * world) void Random_map(NetWorld *world)
{ {
int nbNode = world->size; int nbNode = world->size;
float randomX, randomY; float randomX, randomY;
/* Intializes random number generator */ /* Intializes random number generator */
srand(time(NULL)); srand(time(NULL));
/* Print 5 random numbers from -15.00 to 15.00 */ /* Generate nbNode*2 random numbers from -15.00 to 15.00 */
for( int i = 0 ; i < nbNode ; i++ ) { for (int i = 0; i < nbNode; i++)
double randomX = randfrom(-15.0, 15.0); {
double randomY = randfrom(-15.0, 15.0); if (i > 0)
char * name; {
name= malloc(sizeof(int)); bool continueLoop = true;
sprintf(name, "name : %d", i); while (continueLoop)
Node_set( &(world->nodes[i]), (Vector2){randomX, randomY}, RED, name); {
printf("%f\n", randomX); continueLoop = false;
printf("%f\n", randomY); randomX = randfrom(-30.0, 30.0);
} randomY = randfrom(-30.0, 30.0);
Vector2 newPosition = (Vector2){randomX, randomY};
for (int j = 0; j < i; j++)
{
printf("positionComparee %d %f\n", j, dist(world->nodes[j].position, newPosition));
if (dist(world->nodes[j].position, newPosition) < 8.0)
{
continueLoop = true;
}
}
}
//randomX = randfrom(-15.0, 15.0);
//randomY = randfrom(-15.0, 15.0);
char *name;
name = malloc(20*sizeof(char));
sprintf(name, "name : %d", i);
Node_set(&(world->nodes[i]), (Vector2){randomX, randomY}, RED, name);
}
else
{
randomX = randfrom(-15.0, 15.0);
randomY = randfrom(-15.0, 15.0);
char *name;
name = malloc(20*sizeof(char));
sprintf(name, "name : %d", i);
Node_set(&(world->nodes[i]), (Vector2){randomX, randomY}, RED, name);
printf("%f\n", randomX);
printf("%f\n", randomY);
}
}
//Gabriel graph sort //Gabriel graph sort
for( int i = 0; i <nbNode-1 ; i++){ for (int i = 0; i < nbNode - 1; i++)
for( int j = i+1; j <nbNode ; j++){ {
for (int j = i + 1; j < nbNode; j++)
{
int compteurPointDansLeCercle = 0; int compteurPointDansLeCercle = 0;
float xm = 0.5*(world->nodes[i].position.x + world->nodes[j].position.x); float xm = 0.5 * (world->nodes[i].position.x + world->nodes[j].position.x);
float ym = 0.5*(world->nodes[i].position.y + world->nodes[j].position.y); float ym = 0.5 * (world->nodes[i].position.y + world->nodes[j].position.y);
float distCenterOfij = sqrt((xm - world->nodes[i].position.x)*(xm - world->nodes[i].position.x) + (ym - world->nodes[i].position.y)*(ym - world->nodes[i].position.y)); Vector2 M = {xm, ym};
printf("xm : %f\n", xm); float distNodeCenterOfij = dist(world->nodes[i].position, M);
printf("ym : %f\n", ym); //printf("xm : %f\n", xm);
printf("distCenterOfij : %f\n", distCenterOfij); //printf("ym : %f\n", ym);
printf("dist %d%d : %f\n", i ,j ,dist((world->nodes[i]),world->nodes[j])); //printf("distNodeCenterOfij : %f\n", distNodeCenterOfij);
for(int k = 0 ; k < nbNode ; k++){ printf("distNode %d%d : %f\n", i, j, distNode((world->nodes[i]), world->nodes[j]));
printf(" dist center - Node k %f\n", sqrt((world->nodes[k].position.x - xm)*(world->nodes[k].position.x - xm) + (world->nodes[k].position.y - ym)*(world->nodes[k].position.y - ym))); for (int k = 0; k < nbNode; k++)
if( sqrt((world->nodes[k].position.x - xm)*(world->nodes[k].position.x - xm) + (world->nodes[k].position.y - ym)*(world->nodes[k].position.y - ym)) < distCenterOfij -0.05){ {
compteurPointDansLeCercle ++; //printf(" distNode center - Node k %f\n", dist(world->nodes[k].position, M));
if (dist(world->nodes[k].position, M) < distNodeCenterOfij - 0.05)
{
compteurPointDansLeCercle++;
} }
} }
printf("compteurPointDansLeCercle : %i\n", compteurPointDansLeCercle); //printf("compteurPointDansLeCercle : %i\n", compteurPointDansLeCercle);
if(compteurPointDansLeCercle == 0){ if (compteurPointDansLeCercle == 0)
{
NetWorld_biconnect(world, i, j); NetWorld_biconnect(world, i, j);
} }
} }
} }
} }
\ No newline at end of file
...@@ -8,6 +8,7 @@ void Random_map(NetWorld * world); ...@@ -8,6 +8,7 @@ void Random_map(NetWorld * world);
double randfrom(double min, double max); double randfrom(double min, double max);
double dist(Node node1, Node node2); double dist(Vector2 vector1, Vector2 vector2);
double distNode(Node node1, Node node2);
#endif #endif
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment