10

I came across this interview question

Many irregularly shaped objects are moving in random directions. Provide a data structure and algorithm to detect collisions. Remember that the number of objects is in the millions.

I am assuming that every object would have an x and y coordinate. Other assumptions are most welcome. Also a certain kind of tree should be used, I suppose, but I am clueless about the algorithm.

Any suggestions?

Josh
  • 189
  • 12
garima
  • 5,154
  • 11
  • 46
  • 77
  • 3
    I would expect these objects to have more than one x and y coordinate, not just one as you mention/expect. Did you post the question verbatim? I guess not, since quite some details are missing, IMO. For example, what is an _"irregular shape"_ exactly? – Bart Kiers Mar 22 '11 at 08:57

4 Answers4

3

I would have a look at the Plane Sweep Algorithm or the Bently-Ottmann Algorithm. It uses plane sweep to determine in O(n log(n)) time (and O(n) space) the intersection of lines on a euclidian plane.

Nico Huysamen
  • 10,217
  • 9
  • 62
  • 88
  • "irregularly shaped" vs. line intersection? It could be a solution in 2D but you can do better than O(n log n). In 3D it does not work. – knivil Mar 22 '11 at 09:32
  • @knivil - The algorithm can be extended to work in 3D space, that's why it's called PLANE sweep and not line sweep. The algorithms were for reference. – Nico Huysamen Mar 22 '11 at 09:56
2

Most likely what you want is to sub-divide the plane with a space-filling-curve like a z-curve or a hilbert-curve and thus reducing the complexity of a 2D problem to a 1D problem. Look for quadtree.

Link: http://dmytry.com/texts/collision_detection_using_z_order_curve_aka_Morton_order.html

Micromega
  • 12,486
  • 7
  • 35
  • 72
1

There are many solutions to this problem. First: Use bounding boxes or circles (balls in 3D). If the bounding boxes do not intersect then no further tests are needed. Second: Subdivide your space. You do not have to test every object against all other objects (that is O(n^2)). You can have an average complexity of O(n) with quadtrees.

knivil
  • 916
  • 5
  • 10
-2

I guess there should be a loop which takes reference of 1 object find co-ordinates and then checks with rest of all other objects to see if there is any collision. I am not sure how good my solution is for millions of objects. Psuedo-code:

For each irregular shaped object1    

int left1, left2;
int right1, right2;
int top1, top2;
int bottom1, bottom2;
bool bRet = 1; // No collision

left1 = object1->x;
right1 = object1->x + object1->width;
top1 = object1->y;
bottom1 = object1->y + object1->height;

For each irregular shaped object2
{
    left2 = object2->x;
    right2 = object2->x + object2->width;
    top2 = object2->y;
    bottom2 = object2->y + object2->height;

    if (bottom1 < top2) bRet =0;
    if (top1 > bottom2) bRet = 0;

    if (right1 < left2) bRet = 0;
    if (left1 > right2) bRet = 0;
}

return  bRet;
Hiren
  • 341
  • 1
  • 4
  • 17
  • Don't know how 'irregular' these shapes are. Sounds more like rectangles. – gablin Mar 22 '11 at 09:10
  • the above algo by NatashaD is completely wrong don't follow it. You need to give a range of objects space and check if obj1 range is in obj2 rang and vice versa then collision occurred else not. – Jawad Amjad Sep 27 '11 at 12:13