Add absolute position finding

This commit is contained in:
Dendy 2021-02-07 08:41:57 +01:00
parent fcd3e90b8b
commit 3e98521bd2
Signed by: dendy
GPG Key ID: 0168B35FFD7F608F
4 changed files with 49 additions and 4 deletions

View File

@ -25,10 +25,23 @@ int main(int argc, char* *argv){
NOD_Node2D *point2 = NOD_CreateNode2D("Point2D2"); NOD_Node2D *point2 = NOD_CreateNode2D("Point2D2");
point->pos.x = 2; point->pos.x = 2;
printf("The X value of '%s' is %d!\n", point->node.name, 3); point->pos.y = 10;
printf("The X value of '%s' is %d!\n", point->node.name, point->pos.x);
printf("The Y value of '%s' is %d!\n", point->node.name, point->pos.y);
printf("\n");
point2->pos.x = 10;
point2->pos.y = 20;
printf("The X value of '%s' is %d!\n", point2->node.name, point2->pos.x);
printf("The Y value of '%s' is %d!\n", point2->node.name, point2->pos.y);
printf("\n");
NOD_NodeAddChild(point, point2); NOD_NodeAddChild(point, point2);
printf("The true X value of '%s' is %d!\n", point2->node.name, NOD_GetAbsPosNode2D(point2).x);
printf("The true Y value of '%s' is %d!\n", point2->node.name, NOD_GetAbsPosNode2D(point2).y);
printf("\n");
printf("The parent of '%s' is '%s'.\n", NOD(point2)->name, NOD(NOD(point2)->parent)->name); printf("The parent of '%s' is '%s'.\n", NOD(point2)->name, NOD(NOD(point2)->parent)->name);
NOD_PrintNodeTree(point); NOD_PrintNodeTree(point);

View File

@ -30,11 +30,13 @@ void NOD_DestroyNodeBranch(NOD_Node *node);
#define NOD_NodeAddChild(x, y) _nod_add_child(NOD(x), NOD(y)) #define NOD_NodeAddChild(x, y) _nod_add_child(NOD(x), NOD(y))
#define NOD_NodeUnparent(x) _nod_unparent(NOD(x)) #define NOD_NodeUnparent(x) _nod_unparent(NOD(x))
#define NOD_PrintNodeTree(x) _nod_print_tree(NOD(x))
#define NOD_NodeChildCount(x) _nod_child_count(NOD(x));
void _nod_add_child(NOD_Node *parent, NOD_Node *child); void _nod_add_child(NOD_Node *parent, NOD_Node *child);
void _nod_unparent(NOD_Node *child); void _nod_unparent(NOD_Node *child);
#define NOD_PrintNodeTree(x) _nod_print_tree(NOD(x))
#define NOD_NodeChildCount(x) _nod_child_count(NOD(x));
void _nod_print_tree(NOD_Node *node); void _nod_print_tree(NOD_Node *node);
int _nod_child_count(NOD_Node *node); int _nod_child_count(NOD_Node *node);

View File

@ -4,7 +4,7 @@ NOD_Node2D *NOD_CreateNode2D(char *name){
NOD_Node2D *node = NULL; NOD_Node2D *node = NULL;
NOD_Node *plain_node = NULL; NOD_Node *plain_node = NULL;
plain_node = NOD_CreateNodeEX("Node2D", name); plain_node = NOD_CreateNodeEX(_NOD_2D_TYPE, name);
if(plain_node != NULL){ if(plain_node != NULL){
plain_node = realloc(plain_node, sizeof(NOD_Node2D)); plain_node = realloc(plain_node, sizeof(NOD_Node2D));
@ -19,3 +19,28 @@ NOD_Node2D *NOD_CreateNode2D(char *name){
return node; return node;
} }
vector2Di NOD_GetAbsPosNode2D(NOD_Node2D *node){
vector2Di final_pos = {0, 0};
if(node != NULL){
final_pos = node->pos;
NOD_Node *parent = NOD(node)->parent;
/* Skip non Node2D nodes */
while(parent != NULL && strcmp(parent->type, _NOD_2D_TYPE) != 0){
parent = parent->parent;
}
/* Recurse to get the pos of the rest of the ascendence */
if(parent != NULL){
vector2Di parent_pos = NOD_GetAbsPosNode2D((NOD_Node2D *)parent);
final_pos.x += parent_pos.x;
final_pos.y += parent_pos.y;
}
}
return final_pos;
}

View File

@ -3,6 +3,9 @@
#include<stdio.h> #include<stdio.h>
#include<stdlib.h> #include<stdlib.h>
#include<string.h>
#define _NOD_2D_TYPE "NODE_2D"
typedef struct node_2d{ typedef struct node_2d{
NOD_Node node; NOD_Node node;
@ -11,3 +14,5 @@ typedef struct node_2d{
NOD_Node2D *NOD_CreateNode2D(char *name); NOD_Node2D *NOD_CreateNode2D(char *name);
void NOD_DestroyNode2D(NOD_Node *node); void NOD_DestroyNode2D(NOD_Node *node);
vector2Di NOD_GetAbsPosNode2D(NOD_Node2D *node);