0

I think I'm facing a pretty common issue but cannot remember which solution is the most suitable one.

Let's put it that way: I can, in a, certain, given, language get some events anytime the user is moving the mouse. I therefore get the position the current cursor position in let's say in absolute coordinates based system.

Now considering that this system is also capable of containing up to thousands of thousands of shape objects (with a certain location point and size). In a first rough approximation, we can think about them as their related bounding boxes in order to make the lookup operation and classification a bit more simpler, let's also add another simplification by omitting the fact that shapes can overlap each other.

What can be the best data structures to retrieve the shape(s) under the cursor position and allow relative fast insertion / deletion / modification operations?

Thanks,

ps: I was thinking about some sort of sorted trees but I think they are pretty demanding when it comes about maintaining their internal sort.

Additional curiosity: how it works in Winforms? I mean the Control class in the .NET Framework is related to the underlying HWANDLE in windows and capable of firing events when cursor is entering or leaving a Control. I do not really think that there is a thread allocated to perform some dummy hit tests... and trigger the related events.

1 Answer 1

1

if space is not of concern and your application can afford to use as many space as you want, I'm thinking of 3-dimensional array (or to be more precise, 2-dimensional array of list) structure 2-dimensional array will represent the screen. Each pixel is an element in the array. Each location in array contains a pointer to a list of shape identifier

e.g. there are 2 shapes with id 4, 5 at location {10, 12}, so the element {10, 12} of the 2-dimensional array contains a list of 2 elements (4 and 5). Please note that shape id is a number that uniquely identify that shape instance. Even if 2 shapes are both square, they will have different shape id

  • To find at a specific mouse location, what shapes are present, just need 1 array lookup + list travel to get all shapes at that location
  • To insert: it will depend on how big is the shape, will need to go through all the pixels in the shape, add that shape id into the structure
  • To delete: for faster delete, I think can keep a lookup that has key is shape id, value is location of one pixel within that shape. when you want to delete the shape, look up the location of the pixel based on shape id, then use flood fill algorithm to remove the shape id from the structure

I hope I describe it well, here's an example of the 3x3 array. There are 2 squares of size 2x2 in the array with id 1, 2. THese 2 squares overlapping in the middle of the array

[1] - [ 1 ] - []

[1] - [1,2] - [2]

[] - [ 2 ] - [2]

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.