P
P
Perzh2014-08-06 23:33:44
C++ / C#
Perzh, 2014-08-06 23:33:44

What data structure to choose?

Hello.
There is a set of non-overlapping intervals (hereinafter referred to as layers) to which the textures correspond. The drawing function receives some interval that needs to be drawn. Thus, you need to find all the intersections of the given interval with each layer and draw each intersection with your own texture. Example:

Слои:
0.00 10.00 white
10.00 20.00 blue
20.00 30.00 red

Нужно отрисовать интервал: 5.00 45.00

В результате:
5.00 до 10.00 белым цветом
10.00 до 20.00 синим цветом
20.00 до 30.00 красным цветом
30.00 45.00 дефолтным цветом

At the moment, I store the layers in an ordered sequence (ascending left border). When drawing, I quickly go down to the first layer that intersects the specified interval, and then I draw until I get layers that do not intersect the interval. In principle, it works quite quickly (unnecessary intervals are quickly discarded), but I would like to know if there is some interesting data structure for storing layers that allows you to quickly find all intersections with a given interval? Please tell me, if not difficult, such a structure or an algorithm for finding intersections. Thanks in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Starkov, 2014-08-07
@Perzh

if you have a search for the first interval, something like a binary search, then you can already well not fool your head further and yes there is such a structure here
en.m.wikipedia.org/wiki/Interval_tree
If there are several thousand intervals in total, then you will be surprised but naive O (n ) search will be faster than any tree structures.
(processor architecture features)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question