I'm developing a game in which the player can draw a line. I want to detect if that line intersects with itself. How would I do this?
|show 7 more comments|
Anyway, any representation should be possible to convert into a list of points that represent the curve to some precision. It might look something like this:
Go through adjacent point pairs and check if they intersect with another point pair that isn't the same.
In the innermost loop, the function
Your pictorial example would suggest the curves are quite simple. However, if you find that curves have to be long or their precision quite high, the polynomial time naive algorithm may be too slow. More advanced algorithms exist and some of them can solve the problem in logarithmic time by performing a sweep test. These are, of course, much more complex to implement.