0

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 in 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.

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

4 Comments

Thanks for your early reply.some times wordRect array contains 2500 rectangles.if the user tapped point contains in the [wordRects objectAtIndex:1] then my code is good.if the user tapped point not contains in the 2500 rectangles(wordRects) then all my iterations have waste so i need to reduce my iterations.For this i am looking for some algo for Searching
Can you make use of CFArrayBSearchValues. You can have the reference in my updated answer
i seen CFArrayBSearchValues that is available in the Mac osx but not in the ios
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.