In my iOS app, I have many boxes (Quadrilaterals). I wonder what is the best data structure to detect if a touch on the screen is inside which box.
I have read about GKRTree and GKQuadTree in the GameplayKit. It appears that none of them are exactly what I need. GKRTree allows flexible quads but does not support query by a point. GKQuadTree supports query by a point, but does not seems allow flexible shapes/sizes.
EDIT: Add a sample code to demonstrate that the query with a box inside target box does not return the target box.
import UIKit
import GameplayKit
// target rec.
let rec1v1 = vector_float2(0.0, 0.0)
let rec1v2 = vector_float2(10.0, 10.0)
// outside target rec
let rec2v1 = vector_float2(11.0, 11.0)
let rec2v2 = vector_float2(12.0, 12.0)
//small box inside target rec
let rec3v1 = vector_float2(1.0, 1.0)
let rec3v2 = vector_float2(2.0, 2.0)
// small box intersect with target rec
let rec4v1 = vector_float2(9.0, 9.0)
let rec4v2 = vector_float2(11.0, 11.0)
var mytree = GKRTree<NSString>(maxNumberOfChildren: 3)
mytree.addElement("rec1v1",
boundingRectMin: rec1v1,
boundingRectMax: rec1v2,
splitStrategy: GKRTreeSplitStrategy.linear)
let t1 = mytree.elements(inBoundingRectMin: rec2v1, rectMax: rec2v2) // return [] (outside)
let t2 = mytree.elements(inBoundingRectMin: rec3v1, rectMax: rec3v2) // return [] (inside target box but smaller)
let t3 = mytree.elements(inBoundingRectMin: rec1v1, rectMax: rec1v2) // return ["rec1v1"] (same box)
let t4 = mytree.elements(inBoundingRectMin: rec4v1, rectMax: rec4v2) // return [] (intersect with target box but not containing)
EDIT2: I came across this post, it is very much the same problem. I would like to find a good swift/objective-c implementation.