0

At present, I encounter a line erasure scenario in my project. I hope to seek some suggestions from there. The following is to abstract the specific problem:

A two-dimensional region of m * n with many irregular curves, each of which is recorded in many points: [(x1, y1), (x2, y2), (x3, y3) ...], stored as arrays: array1, array2 ..., now there is a circle (x, y) with a radius r, I need to confirm whether the circle and these lines have intersections, and select the lines with intersections. In addition, the coordinates and radius of the circle are constantly changing, and the irregular curve is changing, but the change is not very fast, so I need to consider using cache to improve performance.

A pic to show the case:

enter image description here

Some of my thoughts: In fact, even if It traverse all the points once a time, the complexity is O(N), but here N is a bit big, and because the circle is changing in real time, so the performance is still relatively poor. I can choose to use a quadtree, but it is not a typical two-dimensional collision scenario, and I should also consider that the current storage method is not satisfied so that I need to do real-time synchronous update of two data structures, but the overall look should be better than the first method, but I don't know if this is the right way. In addition, I am thinking that there should be a better solution here.

are there any good ideas or relevant experiences to share? Thank you very much

聂小涛
  • 503
  • 3
  • 16

1 Answers1

2

From personal experience making unconventional 2D environments, all I can say is to try different things and see what works. I can't discern from your description if you'd be able to just brute-force the collision detection or if you'd need a quadtree; There's a possibility you may need to multithread this if you have an extraordinary number of curves.

I think an appropriate thing to do would be to make a Segment class for each of your polylines (because they're essentially a collection of segments, not a curve), and use a quadtree to discriminate which segments are nearest to the circle, which would also be placed into the quadtree. That way whenever you trigger a collision, you can mark each overlapping segment as "selected" and do whatever it is you need to do. I think only having them as an array of points makes life harder, and is only really something the graphics pipeline would find useful.

If you're finding that your lines are too dense with points, I would suggest looking at Polyline Decimation algorithms to reduce their complexity; if that would still suit your purposes, of course.

alteredinstance
  • 587
  • 3
  • 17
  • I'm assuming you're doing this for some graphical application - if you want the application to animate or be responsive, you may need to calculate the collisions in a separate thread to maintain that responsiveness. – alteredinstance Oct 14 '19 at 15:40
  • That would likely lead to all sorts of complex synchronisation problems - i very much doubt hit testing for a few 2d shapes would be taxing for modern hardware but first the OP needs to get it working first and then worry about performance if it indeed an issue. – auburg Oct 14 '19 at 15:44
  • Furthermore i would think doing the calculations for 2d collision detection in a separate thread would be overkill since they wouldn't be that computationally heavy so you could be making things slower. – auburg Oct 14 '19 at 15:49
  • You're probably right; in my use case it was upwards of 10,000 polygons and so my application simply wouldn't be usable without multithreading, but I don't remember it being significantly more difficult to implement. I'll change the original post. – alteredinstance Oct 14 '19 at 15:51