Thread: Quadtree in C/OpenGL help?

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    3

    Quadtree in C/OpenGL help?

    I'm having trouble with my deleteNode function... I need the program to be able to delete only one node when you right click on it, but not the rest of the nodes in the quadtree.

    Here's the current deleteNode function... let me know if you see the problem or the logic that I am missing!

    Code:
    void deleteNode(qnode *root, int mousex, int mousey)
    {
    int i, dist;
    
    if ( root == NULL ) return; // Sub-tree is empty. This situation will generally not occur
    
    dist = sqrt(pow((mousex - root->px),2) + pow((mousey - root->py),2));
    
    printf("distance = %d\n", dist);
    
    if (dist<5)
    {
    
    printf("delete this point\n");
    deleteNode(root->children[i], mousex, mousey);
    free(root->children[i]);
    root->children[i] = NULL;
    
    return;
    }
    else {
    for (i=0; i<4; i++) 
    {
    if ( root->children[i] != NULL )
    {
    deleteNode(root->children[i], mousex, mousey);
    }
    }
    }
    
    }
    Last edited by Seraphim0240; 10-02-2010 at 11:35 AM.

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    3

    no responses?

    does anyone have the slightest clue what to do next? i attached my .c file down at the bottom...

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Okay: so have you done a quad tree before, or are you learning to do it in the context of your openGL project?

    If the latter is true, start thinking about some of the reasons why that is not a good plan. For example, now you are stucking posting masses of code no one is interested in.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, I'm not really clear what your problem is. There are two branches in your code:
    (a) if dist is less than 5 you remove the node you are on and all of it's descendants
    (b) if dist is 5 or more you call the function recursively on all the children.

    If you don't want to delete all of the descendants, then don't do that, I guess; otherwise I'm not sure what problem you're having.

  5. #5
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    In the first if block, "i" is not initialized. I'm guessing that should be in a loop.

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    3
    Quote Originally Posted by tabstop View Post
    Well, I'm not really clear what your problem is. There are two branches in your code:
    (a) if dist is less than 5 you remove the node you are on and all of it's descendants
    (b) if dist is 5 or more you call the function recursively on all the children.

    If you don't want to delete all of the descendants, then don't do that, I guess; otherwise I'm not sure what problem you're having.
    Yeah, I can't figure out how to not delete all of the descendants and only the current node that the user right-clicked on... That's where I'm stuck. I have no idea how to begin coding that.

    And to the guy who said this isn't the best way to learn quadtrees and I shouldn't be posting code no one is interested in, you can tell that to my professor. He assigned us a project over spring break that none of us know how to do since he didn't teach us how quadtrees work and/or the syntax that goes with them... and since the university is shut down for the week, there are no tutors/teaching assistants to help us. So here I am on a computer programming forum. Thanks for your input though.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Seraphim0240 View Post
    Yeah, I can't figure out how to not delete all of the descendants and only the current node that the user right-clicked on... That's where I'm stuck. I have no idea how to begin coding that.
    Really? You don't see the line that calls "delete" on the children? And "free" on the children? You don't know how to remove those lines?

    Now, you do have the question -- where are those nodes supposed to go? You can't just leave them hanging as children of a nonexistent node -- you'll need to hook them back to the tree.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    to the guy who said this isn't the best way to learn quadtrees and I shouldn't be posting code no one is interested in, you can tell that to my professor. He assigned us a project over spring break that none of us know how to do since he didn't teach us how quadtrees work and/or the syntax that goes with them... and since the university is shut down for the week, there are no tutors/teaching assistants to help us. So here I am on a computer programming forum. Thanks for your input though.
    In other words, you are stuck having to figure out quad trees yourself. My point was, you are only making it harder on yourself by doing that within your openGL project. Your life will be much simpler and easier if you take a few deep breaths and try and write a short quad tree "demo" program for yourself, sans any other concerns.

    And, chances are, the implementation you transplant (OMG: tree pun) into the GL code will probably be the better for it too.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quadtree and Frustum in OpenGL
    By sarah22 in forum Game Programming
    Replies: 2
    Last Post: 05-13-2009, 10:51 PM
  2. Quadtree Node -> pointers to the children node
    By yahn in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2009, 06:38 PM
  3. using Scanline or Quadtree?
    By Darkinyuasha1 in forum Game Programming
    Replies: 1
    Last Post: 01-02-2009, 01:20 AM
  4. Class with a pointer to itself
    By yahn in forum C++ Programming
    Replies: 10
    Last Post: 09-21-2008, 12:43 PM
  5. Quadtree algo
    By VirtualAce in forum Game Programming
    Replies: 2
    Last Post: 04-21-2004, 07:11 PM

Tags for this Thread