Commit cd2ad1d2 authored by guillaume's avatar guillaume

edges

parent de8a42ef
......@@ -43,6 +43,16 @@ void Panel_draw(Panel * self)
{
BeginDrawing();
ClearBackground(RAYWHITE);
// Draw the edges for each nodes:
for(int i= 0 ; i < self->world->size ; ++i )
{
Node * n= &(self->world->nodes[i]);
for(int j= 0 ; j < n->card ; ++j )
Panel_drawEdge( self, &(n->edges[j]) );
}
// Draw the nodes
for(int i= 0 ; i < self->world->size ; ++i )
{
Panel_drawNode( self, &(self->world->nodes[i]) );
......@@ -67,10 +77,18 @@ void Panel_drawBasis(Panel * self)
void Panel_drawNode(Panel * self, Node * n)
{
Vector2 screenPosition= Panel_pixelFromPosition(self, &(n->position) );
DrawCircleV(screenPosition, 24, MAROON);
DrawCircleV(screenPosition, 24, n->color);
DrawCircleV(screenPosition, 20, RAYWHITE);
}
void Panel_drawEdge(Panel * self, Edge * e)
{
Vector2 source= Panel_pixelFromPosition(self, &(e->_source->position) );
Vector2 target= Panel_pixelFromPosition(self, &(e->_target->position) );
DrawLineV(source, target, BLUE);
}
Vector2 Panel_pixelFromPosition(Panel * self, Vector2 * p)
{
Vector2 pixel= {
......
......@@ -25,6 +25,7 @@ void Panel_print(Panel * self);
void Panel_draw(Panel * self);
void Panel_drawBasis(Panel * self);
void Panel_drawNode(Panel * self, Node * n);
void Panel_drawEdge(Panel * self, Edge * e);
Vector2 Panel_pixelFromPosition(Panel * self, Vector2 * p);
Vector2 Panel_positionFromPixel(Panel * self, Vector2 * p);
......
......@@ -31,9 +31,13 @@ int main(int nbArg, char ** arg)
//--------------------
game_end= false;
NetWorld * world= NetWorld_new(3);
NetWorld_initNodePosition( world, 0, 1.4f, 1.28f );
NetWorld_initNodePosition( world, 1, 11.0f, 32.8f );
NetWorld_initNodePosition( world, 2, 18.4f, -12.2f );
Node_set( &(world->nodes[0]), (Vector2){1.4f, 1.28f}, RED );
Node_set( &(world->nodes[1]), (Vector2){21.0f, 22.8f}, MAGENTA );
Node_set( &(world->nodes[2]), (Vector2){18.4f, -12.2f}, GRAY );
NetWorld_connect(world, 0, 1);
NetWorld_connect(world, 1, 2);
NetWorld_connect(world, 2, 0);
Panel * panel= Panel_new();
Panel_initialize(panel, world);
......
#include "networld.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
//-----------------------------------//
//-- Node --//
//-----------------------------------//
// Constructor / Destructor
Node * Node_new()
{
Node * p= malloc( sizeof(Node) );
Node_set( p, (Vector2){0.f, 0.f}, GRAY );
p->card= 0;
p->edges= Edge_newArray( 0 );
return p;
}
Node * Node_newArray(int size)
{
size= fmaxf(1, size);
Node * p= malloc( sizeof(Node)*size );
for(int i=0 ; i < size ; ++i )
{
Node_set( p, (Vector2){0.f, 0.f}, GRAY );
p[i].edges= Edge_newArray( 0 );
p[i].card= 0;
}
return p;
}
void Node_delete( Node * node )
{
Edge_deleteArray( node->card, node->edges );
free( node );
}
/**
* @brief Destructor of an array of nodes.
*/
void Node_deleteArray( int n, Node * array )
{
for(int i= 0 ; i < n ; ++i )
Edge_deleteArray( array[i].card, array[i].edges );
free( array );
}
/**
* @brief initialize a node with a number of edges
*/
void Node_initialize( Node * self, int card )
{
for(int i= 0 ; i < self->card ; ++i )
Edge_deleteArray( self->card, self->edges );
self->edges= Edge_newArray( card );
self->card= card;
}
/**
* @brief Resize
*/
void Node_resize( Node * self, int card )
{
Edge * newEdges= Edge_newArray(card);
int minCard= fminf(self->card, card);
for( int i = 0 ; i < minCard ; ++i )
{
Edge_copy( &newEdges[i], &(self->edges[i]) );
}
Edge_deleteArray( self->card, self->edges );
self->edges= newEdges;
self->card= card;
}
void Node_set( Node * self, Vector2 position, Color color )
{
self->position= position;
self->color= color;
}
int Node_connect( Node * self, Node * target )
{
int i= self->card;
Node_resize( self, i+1 );
self->edges[i]._source= self;
self->edges[i]._target= target;
self->edges[i]._twin= NULL;
return i;
}
int Node_biconnect( Node * node1, Node * node2 )
{
int i1= Node_connect(node1, node2);
int i2= Node_connect(node2, node1);
node1->edges[i1]._twin= &(node2->edges[i2]);
node2->edges[i2]._twin= &(node1->edges[i1]);
return i1;
}
//-----------------------------------//
//-- Edge --//
//-----------------------------------//
// Constructor / Destructor
Edge * Edge_new( Node * source, Node * target )
{
Edge * p = malloc( sizeof(Edge) );
p->_source= source;
p->_target= target;
p->_twin= NULL;
return p;
}
/**
* @brief Destructor.
*/
void Edge_delete( Edge * self )
{
free( self );
}
/**
* @brief Allocate 'size' Edges (with NULL target)
* @return The pointer to the new array of 'size' Edges.
*/
Edge * Edge_newArray(int size)
{
size= fmaxf(1, size);
Edge * p = malloc( sizeof(Edge)*size );
for( int i = 0 ; i < size ; ++i )
{
p[i]._source= NULL;
p[i]._target= NULL;
p[i]._twin= NULL;
}
return p;
}
/**
* @brief Destructor.
*/
void Edge_deleteArray( int n, Edge * array )
{
free(array);
}
/**
* @brief Copy an edge from another
*/
void Edge_copy( Edge * copy, Edge * model )
{
copy->_source= model->_source;
copy->_target= model->_target;
copy->_twin= model->_twin;
}
//-----------------------------------//
//-- NetWorld --//
//-----------------------------------//
// Constructor / Destructor
NetWorld * NetWorld_new(int size)
{
NetWorld * p = malloc( sizeof(NetWorld) );
p->size= size;
p->nodes= malloc( p->size * sizeof(Node) );
p->nodes= Node_newArray( p->size );
return p;
}
void NetWorld_delete(NetWorld * self)
{
free( self->nodes );
Node_deleteArray( self->size, self->nodes );
free( self );
}
// Initialization
void NetWorld_initNodePosition(
NetWorld * self, int iNode,
float x, float y)
{
self->nodes[iNode].position.x= x;
self->nodes[iNode].position.y= y;
}
// To String
void NetWorld_print(NetWorld * self)
{
for(int i= 0 ; i < self->size ; ++i )
printf("[%.2f, %.2f]\n", self->nodes[i].position.x, self->nodes[i].position.y);
printf("[%.2f, %.2f]%d\n",
self->nodes[i].position.x,
self->nodes[i].position.y,
self->nodes[i].card);
}
// Managment :
int NetWorld_connect( NetWorld * self, int source, int target )
{
return Node_connect( &(self->nodes[source]), &(self->nodes[target]) );
}
int NetWorld_biconnect( NetWorld * self, int source, int target )
{
return Node_biconnect( &(self->nodes[source]), &(self->nodes[target]) );
}
......@@ -3,33 +3,251 @@
#include "raylib.h"
//-----------------------------------//
//-- Node --//
//-----------------------------------//
struct Str_Edge;
struct Str_Node {
//! position (x, y) of the node
Vector2 position;
//! color (r, g, b, a) of the node
Color color;
//! cardinality of the node (i.e. number of edges)
int card;
struct Str_Edge * edges;
};
/**
* @brief NetWorld Node.
*/
typedef struct Str_Node Node;
// Constructor / Destructor
/**
* @brief Allocate the memory to store a Node
* @return The pointer to the new NetWorld.
*/
Node * Node_new();
/**
* @brief Allocate 'size' Nodes.
* @return The pointer to the new array of 'size' Nodes.
*/
Node * Node_newArray(int size);
/**
* @brief Destructor.
*/
void Node_delete(
//! the NetWorld to delete
Node * self
);
/**
* @brief Destructor of an array of nodes.
*/
void Node_deleteArray(
//! the number of nodes to delete
int n,
//! the Nodes
Node * array
);
/**
* @brief initialize a node with a number of edges
*/
void Node_initialize(
//! the node to initialize
Node * self,
//! the number of outgoing edges
int card
);
/**
* @brief Resize
*/
void Node_resize(
//! the node to resize
Node * self,
//! the new number of outgoing edges
int card
);
// Managment :
/**
* @brief set
*/
void Node_set(
//! the node to set
Node * self,
//! position
Vector2 position,
//! color
Color color
);
/**
* @brief connect tow node together
* @return the index of the edge in the node edges' array
*/
int Node_connect(
//! the source Node to modify
Node * self,
//! the target node
Node * target
);
/**
* @brief connect tow node together (bidirectional)
* @return the index of the edge in the node edges' array
*/
int Node_biconnect(
//! the source Node to modify
Node * node1,
//! the target node
Node * node2
);
//-----------------------------------//
//-- Edge --//
//-----------------------------------//
struct Str_Edge {
//! pointer to the source node.
Node * _source;
//! pointer to the connected node.
Node * _target;
//! pointer to the twin edge in case of bidirectional edge.
struct Str_Edge * _twin;
};
/**
* @brief NetWorld Edge.
*/
typedef struct Str_Edge Edge;
// Constructor / Destructor
/**
* @brief Allocate the memory to store a NetWorld.
* @return The pointer to the new NetWorld.
*/
Edge * Edge_new(
//! the source Node to connect
Node * source,
//! the target Node to connect (NULL if unknown)
Node * target
);
/**
* @brief Destructor.
*/
void Edge_delete(
//! the NetWorld to delete
Edge * self
);
/**
* @brief Allocate 'size' Edges (with NULL target)
* @return The pointer to the new array of 'size' Edges.
*/
Edge * Edge_newArray(int size);
/**
* @brief Destructor.
*/
void Edge_deleteArray(
//! the number of edges
int n,
//! the NetWorld to delete
Edge * array
);
// Copying
/**
* @brief Copy an edge from another
*/
void Edge_copy(
//! the edge to modify
Edge * copy,
//! the edge to copy
Edge * model
);
//-----------------------------------//
//-- NetWorld --//
//-----------------------------------//
struct Str_NetWorld {
//! number of nodes composing the NetWorld
int size;
//! Array of 'size' nodes
Node * nodes;
};
typedef struct Str_NetWorld NetWorld;
// Constructor / Destructor
/**
* @brief Allocate the memory to store a NetWorld.
* @return The pointer to the new NetWorld.
* @brief Allocate the memory to store a NetWorld.
* @return The pointer to the new NetWorld.
*/
NetWorld * NetWorld_new(
//! the number of Nodess
//! the number of Nodes composing the new NetWorld
int aSize
);
void NetWorld_delete(NetWorld * self);
// Initialization
void NetWorld_initNodePosition(NetWorld * self, int iNode, float x, float y); // position must be an float[size][2] array...
/**
* @brief Destructor.
*/
void NetWorld_delete(
//! the NetWorld to delete
NetWorld * self
);
// To String
void NetWorld_print(NetWorld * self);
/**
* @brief print
*/
void NetWorld_print(
//! the NetWorld to modify
NetWorld * self
);
// Managment :
/**
* @brief connect tow node together
* @return the index of the edge in the node edges' array
*/
int NetWorld_connect(
//! the NetWorld to modify
NetWorld * self,
//! index of the source node
int source,
//! index of the target node
int target
);
/**
* @brief connect tow node together (bidirectional)
* @return the index of the edge in the node edges' array
*/
int NetWorld_biconnect(
//! the NetWorld to modify
NetWorld * self,
//! index of the source node
int source,
//! index of the target node
int target
);
#endif //NETWORLD_H
\ 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