We have a (w times h) canvas (width w and height h), which is used as a drawing area. We can define ‘maps’ or regions in the drawing area, on which the user can click to perform some pre-defined tasks. Each region is defined by a bounding rectangle. An image-map is activated when the user clicks inside it. Two regions may have overlapping rectangles. Whenever the user clicks on a point the canvas, we are required to find the find the image-map(s) to which the point belongs and start the execution of the corresponding task. We can always use a linear list to find the image-maps. But is there a better way, a data structure that could be used to store the image maps so that we can figure out efficiently (in less than O(n) time) which image-maps are activated on user click?
Share
Yes – use any type of 2D spatial index. The most common is the quad-tree, which has O(log(n)) lookup complexity and is also quite quick to build. Implementations are available in all major languages; it is extensively used for all types of mapping applications.