Commit d606d893 authored by ewen.madec's avatar ewen.madec

Gabriel Graph generation

parent 8b397725
...@@ -28,10 +28,18 @@ bool game_end; ...@@ -28,10 +28,18 @@ bool game_end;
int main(int nbArg, char ** arg) int main(int nbArg, char ** arg)
{ {
int n = 10; int n = 5;
NetWorld * world= NetWorld_new(n); NetWorld * world= NetWorld_new(n);
world = Random_map(world, n); world = Random_map(world, n);
//Node_set(&(world->nodes[0]), (Vector2){1.4f, 1.28f}, ORANGE);
printf("positionX %f\n", world->nodes[0].position.x);
printf("positionY %f\n", world->nodes[0].position.y);
printf("positionX %f\n", world->nodes[1].position.x);
printf("positionY %f\n", world->nodes[1].position.y);
printf("distXY %f\n", dist(world->nodes[0], world->nodes[1]));
// Game Initialization // Game Initialization
//-------------------- //--------------------
...@@ -50,8 +58,10 @@ int main(int nbArg, char ** arg) ...@@ -50,8 +58,10 @@ int main(int nbArg, char ** arg)
InitWindow(screenWidth, screenHeight, "NetWorld basic viewer"); InitWindow(screenWidth, screenHeight, "NetWorld basic viewer");
SetTargetFPS(targetFPS); SetTargetFPS(targetFPS);
// Some verificcations // Some verificcations
//-------------------- //--------------------
/*
puts("world variable:"); puts("world variable:");
NetWorld_print(world); NetWorld_print(world);
puts("world expected:\n[10.4, 12.8]\n[110.4, 52.8]\n[384.5, 422.2]"); puts("world expected:\n[10.4, 12.8]\n[110.4, 52.8]\n[384.5, 422.2]");
...@@ -59,6 +69,7 @@ int main(int nbArg, char ** arg) ...@@ -59,6 +69,7 @@ int main(int nbArg, char ** arg)
Vector2 position= {1.f, 1.f}; Vector2 position= {1.f, 1.f};
position= Panel_pixelFromPosition( panel, &position ); position= Panel_pixelFromPosition( panel, &position );
printf("[%.2f,%.2f]\n", position.x, position.y); printf("[%.2f,%.2f]\n", position.x, position.y);
*/
// Main game loop // Main game loop
while (!game_end && !WindowShouldClose()) // Detect window close button or ESC key while (!game_end && !WindowShouldClose()) // Detect window close button or ESC key
......
...@@ -12,12 +12,12 @@ ...@@ -12,12 +12,12 @@
// Constructor / Destructor // Constructor / Destructor
void Node_construct(Node * self) void Node_construct(Node * self)
{ {
Node_set( self, (Vector2){0.f, 0.f}, GRAY ); Node_set( self, (Vector2){0.f, 0.f}, GRAY, "" );
self->card= 0; self->card= 0;
self->edges= Edge_newArray( 0 ); self->edges= Edge_newArray( 0 );
self->name= malloc( sizeof(char)*32 ); self->name= malloc( sizeof(char)*32 );
self->continent= malloc( sizeof(char)*32 ); self->continent= malloc( sizeof(char)*32 );
self->soldiers= 1; self->soldiers= 5;
strcpy(self->continent, "None"); strcpy(self->continent, "None");
strcpy( self->name, "Node" ); strcpy( self->name, "Node" );
} }
...@@ -85,10 +85,11 @@ void Node_resize( Node * self, int card ) ...@@ -85,10 +85,11 @@ void Node_resize( Node * self, int card )
self->card= card; self->card= card;
} }
void Node_set( Node * self, Vector2 position, Color color ) void Node_set( Node * self, Vector2 position, Color color, char * name )
{ {
self->position= position; self->position= position;
self->color= color; self->color= color;
self->name = name;
} }
int Node_connect( Node * self, Node * target ) int Node_connect( Node * self, Node * target )
......
...@@ -108,7 +108,10 @@ void Node_set( ...@@ -108,7 +108,10 @@ void Node_set(
//! position //! position
Vector2 position, Vector2 position,
//! color //! color
Color color Color color,
//!
char * name
); );
/** /**
......
...@@ -11,27 +11,68 @@ ...@@ -11,27 +11,68 @@
#include "raylib.h" #include "raylib.h"
double randfrom(double min, double max)
{
double range = (max - min);
double div = RAND_MAX / range;
return min + (rand() / div);
}
double dist(Node node1, Node node2){
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) ));
}
NetWorld * Random_map(NetWorld * world, int nbNode) NetWorld * Random_map(NetWorld * world, int nbNode)
{ {
int i ; int i ;
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 0 to 15.00 */ /* Print 5 random numbers from -15.00 to 15.00 */
for( i = 0 ; i < nbNode ; i++ ) { for( i = 0 ; i < nbNode ; i++ ) {
randomX = (rand() % 1500)/100.0; double randomX = randfrom(-15.0, 15.0);
randomY = (rand() % 1500)/100.0; double randomY = randfrom(-15.0, 15.0);
Node_set( &(world->nodes[i]), (Vector2){randomX, randomY}, RED ); char * name;
printf("%f\nbNode", randomX); name= malloc(sizeof(int));
printf("%f\nbNode", randomY); sprintf(name, "name : %d", i);
Node_set( &(world->nodes[i]), (Vector2){randomX, randomY}, RED, name);
printf("%f\n", randomX);
printf("%f\n", randomY);
if(i!= nbNode-1){ /* if(i!= nbNode-1){
NetWorld_connect(world, i, i+1); NetWorld_connect(world, i, i+1);
} }
else{ else{
NetWorld_connect(world, i, 0); NetWorld_connect(world, i, 0);
} } */
} }
//Gabriel graph sort
for( int i = 0; i <nbNode-1 ; i++){
for( int j = i+1; j <nbNode ; j++){
int compteurPointDansLeCercle = 0;
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 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));
printf("xm : %f\n", xm);
printf("ym : %f\n", ym);
printf("distCenterOfij : %f\n", distCenterOfij);
printf("dist %d%d : %f\n", i ,j ,dist((world->nodes[i]),world->nodes[j]));
//printf("dist sqrt : %f\n", sqrt(dist((world->nodes[i]),world->nodes[p])*dist((world->nodes[i]),world->nodes[p]) + dist((world->nodes[j]),world->nodes[p])*dist((world->nodes[j]),world->nodes[p])));
for(int k = 0 ; k < nbNode ; k++){
/* if((xm - distCenterOfij < world->nodes[k].position.x < xm + distCenterOfij) && (ym - distCenterOfij < world->nodes[k].position.y < ym + distCenterOfij) ){
compteurPointDansLeCercle ++;
} */
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)));
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("compteurPointDansLeCercle : %i\n", compteurPointDansLeCercle);
if(compteurPointDansLeCercle == 0){
NetWorld_connect(world, i, j);
}
}
}
return world; return world;
} }
\ No newline at end of file
...@@ -2,4 +2,8 @@ ...@@ -2,4 +2,8 @@
#include "networld.h" #include "networld.h"
NetWorld * Random_map(NetWorld * world, int nbNode); NetWorld * Random_map(NetWorld * world, int nbNode);
\ No newline at end of file
double randfrom(double min, double max);
double dist(Node node1, Node node2);
\ 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