开发者

need suggestion to implement BINARY SEARCH for array which contains cgrect values

i am trying to modify my code little bit for the searching rect. my worked code is below

for(int i=0;i<[wordRects count];i++){

     if(CGRectContainsPoint([[wordRects objectAtIndex:i] CGRectValue], tapedPoint)){

        lineImage=[[UIImageView alloc] initWithFrame:[[wordRects objectAtIndex:i]CGRectValue]];
        lineImage.backgroundColor=[[UIColor blueColor] colorWithAlphaComponent:0.3f];
        [textSelectionView addSubview:lineImage];
        break;    
    }
}

In the above code the wordRects array contains approximately 500 rects and tapedpoint is user tap point i开发者_运维技巧n the view. if the user taped point not in the array then in the worst case 500 iterations happen. *My Requirement * I want to reduce the iterations by using the binary search algo.Is there any use to implement that if yes can any one please modify my code according to binary search or give me some idea to implement this.Thanks in advance.


One of the simplest and amongst most powerful ways to get good performance on bounds checking is to use spatial partitioning trees or otherwise called spatial indexing trees, more specifically a QuadTree ,since rects are 2D structures.


If you got true for your if statement, do whatever u like and just return;. That will stop the unnecessary iterations.

If you want you can try CFArrayBSearchValues


To improve performance you should use spatial indexing. You may use a single grid, where each cell contains pointers to all rectangles that are over that cell. That should be simple to implement, but sufficient for your purposes. You may also look at other spatial indexing techniques.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜