so for a class I am supposed to create a function that gets a pixel given an x and y value from a node in my Quadtree, and in this case it represents and PNG image. This function needs to be O(logn). I am just looking for suggestions on how I could possibly improve my run time. Thank you!
RGBAPixel Quadtree::getPixel(int x, int y) const{
if(root != NULL && x <= root->resolution && y <= root->resolution){
return getPixel(x, y, root);
}
return RGBAPixel();
}
//getPixel helper function
RGBAPixel Quadtree::getPixel(int x, int y, QuadtreeNode * root) const{
//base case, resolution reaches 1 it returns element
if((root->x == x && root->y == y && root->resolution ==1) || root->nwChild == NULL){
return root->element;
}
//if statements to see if x and y are within a specific child
if(range(x, y, root->nwChild)){
return getPixel(x, y, root->nwChild);
}
else if(range(x, y, root->neChild)){
return getPixel(x, y, root->neChild);
}
else if(range(x, y, root->swChild)){
return getPixel(x, y, root->swChild);
}
else{
return getPixel(x , y, root->seChild);
}
}
//getPixel helper function (returns weather or not x and y lies within a specific child passed by previous helper fucntion
bool Quadtree::range(int x, int y, QuadtreeNode * root) const{
int xr = root->x + root->resolution;
int yr = root->y + root->resolution;
return ((x >= root->x && x < xr) && (y >= root->y && y < yr));
}